Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@planetscale/database

Package Overview
Dependencies
Maintainers
4
Versions
31
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@planetscale/database - npm Package Compare versions

Comparing version 0.1.1 to 0.2.0

dist/sanitization.d.ts

8

dist/index.d.ts

@@ -0,1 +1,3 @@

export { format } from './sanitization.js';
export { hex } from './text.js';
declare type ReqInit = Pick<RequestInit, 'method' | 'headers'> & {

@@ -9,4 +11,6 @@ body: string;

}
declare type Types = Record<string, string>;
export interface ExecutedQuery {
headers: string[];
types: Types;
rows: Row[];

@@ -25,2 +29,3 @@ size: number;

fetch?: (input: string, init?: ReqInit) => Promise<Pick<Response, 'ok' | 'json' | 'status' | 'statusText' | 'text'>>;
format?: (query: string, args: any) => string;
}

@@ -40,5 +45,4 @@ export declare class Client {

private postJSON;
execute(query: string, args?: object | any[]): Promise<ExecutedQuery>;
execute(query: string, args?: any): Promise<ExecutedQuery>;
}
export declare function connect(config: Config): Connection;
export {};

@@ -1,3 +0,5 @@

import SqlString from 'sqlstring';
import { utf8Encode } from './text.js';
import { format } from './sanitization.js';
export { format } from './sanitization.js';
export { hex } from './text.js';
import { decode } from './text.js';
export class Client {

@@ -56,10 +58,8 @@ constructor(config) {

async execute(query, args) {
const startTime = Date.now();
const url = new URL('/psdb.v1alpha1.Database/Execute', `https://${this.config.host}`);
query = SqlString.format(query, args, false, 'UTC');
const saved = await this.postJSON(url, {
query: query,
session: this.session
});
const time = Date.now() - startTime;
const formatter = this.config.format || format;
const sql = args ? formatter(query, args) : query;
const start = Date.now();
const saved = await this.postJSON(url, { query: sql, session: this.session });
const time = Date.now() - start;
const { result, session, error } = saved;

@@ -71,4 +71,7 @@ const rowsAffected = result?.rowsAffected ? parseInt(result.rowsAffected, 10) : null;

const headers = result ? result.fields?.map((f) => f.name) ?? [] : [];
const typeByName = (acc, { name, type }) => ({ ...acc, [name]: type });
const types = result ? result.fields?.reduce(typeByName, {}) ?? {} : {};
return {
headers,
types,
rows,

@@ -79,3 +82,3 @@ rowsAffected,

size: rows.length,
statement: query,
statement: sql,
time

@@ -132,5 +135,12 @@ };

return parseFloat(value);
case 'BLOB':
case 'BIT':
case 'VARBINARY':
case 'BINARY':
return value;
case 'JSON':
return JSON.parse(decode(value));
default:
return utf8Encode(value);
return decode(value);
}
}

@@ -1,1 +0,2 @@

export declare function utf8Encode(str: string | null): string;
export declare function decode(text: string | null): string;
export declare function hex(text: string): string;

@@ -1,14 +0,13 @@

export function utf8Encode(str) {
if (str === '' || str === null) {
export function decode(text) {
if (!text)
return '';
}
return binaryToHex(str);
}
function binaryToHex(str) {
const decoder = new TextDecoder('utf-8');
const arr = [];
str.split('').forEach(function (c) {
arr.push(c.charCodeAt(0));
});
return decoder.decode(Uint8Array.from(arr));
return decoder.decode(Uint8Array.from(bytes(text)));
}
export function hex(text) {
const digits = bytes(text).map((b) => b.toString(16).padStart(2, '0'));
return `0x${digits.join('')}`;
}
function bytes(text) {
return text.split('').map((c) => c.charCodeAt(0));
}
{
"name": "@planetscale/database",
"version": "0.1.1",
"version": "0.2.0",
"description": "A JavaScript client for PlanetScale databases.",

@@ -51,5 +51,2 @@ "main": "dist/index.js",

"homepage": "https://github.com/planetscale/database-js#readme",
"dependencies": {
"sqlstring": ">=2.3.3"
},
"devDependencies": {

@@ -65,2 +62,3 @@ "@types/jest": "^28.1.6",

"prettier": "^2.7.1",
"sqlstring": "^2.3.3",
"ts-jest": "^28.0.7",

@@ -82,2 +80,3 @@ "ts-node": "^10.9.1",

"rules": {
"no-control-regex": "off",
"prettier/prettier": [

@@ -84,0 +83,0 @@ "error",

@@ -23,3 +23,3 @@ # PlanetScale database client

const conn = await connect(config)
const results = await conn.execute('select 1 from dual')
const results = await conn.execute('select 1 from dual where 1=?', [1])
console.log(results)

@@ -68,2 +68,28 @@ ```

### Custom query parameter format function
Query replacement parameters identified with `?` are replaced with escaped values. Providing a custom format function overrides the built-in escaping with an external library, like [`sqlstring`](https://github.com/mysqljs/sqlstring).
```ts
import { connect } from '@planetscale/database'
import SqlString from 'sqlstring'
const config = {
format: SqlString.format,
host: 'aws.connect.psdb.cloud',
username: '<user>',
password: '<password>'
}
const conn = await connect(config)
const results = await conn.execute('select 1 from dual where 1=?', [42])
console.log(results)
```
Named replacement parameters are supported with a colon prefix.
```ts
const results = await conn.execute('select 1 from dual where 1=:id', { id: 42 })
```
## Development

@@ -70,0 +96,0 @@

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc