@planetscale/database
Advanced tools
Comparing version 1.14.0 to 1.15.0
@@ -0,1 +1,2 @@ | ||
import { format } from './sanitization.js'; | ||
export { format } from './sanitization.js'; | ||
@@ -25,2 +26,3 @@ export { hex } from './text.js'; | ||
} | ||
type Fetch = (input: string, init?: Req) => Promise<Res>; | ||
type Req = { | ||
@@ -40,2 +42,3 @@ method: string; | ||
export type Cast = typeof cast; | ||
type Format = typeof format; | ||
export interface Config { | ||
@@ -46,4 +49,4 @@ url?: string; | ||
host?: string; | ||
fetch?: (input: string, init?: Req) => Promise<Res>; | ||
format?: (query: string, args: any) => string; | ||
fetch?: Fetch; | ||
format?: Format; | ||
cast?: Cast; | ||
@@ -64,3 +67,10 @@ } | ||
type ExecuteAs = 'array' | 'object'; | ||
type ExecuteArgs = object | any[] | null; | ||
type ExecuteArgs = Record<string, any> | any[] | null; | ||
type ExecuteOptions<T extends ExecuteAs = 'object'> = T extends 'array' ? { | ||
as?: 'object'; | ||
cast?: Cast; | ||
} : T extends 'object' ? { | ||
as: 'array'; | ||
cast?: Cast; | ||
} : never; | ||
export declare class Client { | ||
@@ -70,10 +80,4 @@ readonly config: Config; | ||
transaction<T>(fn: (tx: Transaction) => Promise<T>): Promise<T>; | ||
execute<T = Row<'object'>>(query: string, args?: ExecuteArgs, options?: { | ||
as?: 'object'; | ||
cast?: Cast; | ||
}): Promise<ExecutedQuery<T>>; | ||
execute<T = Row<'array'>>(query: string, args: ExecuteArgs, options: { | ||
as: 'array'; | ||
cast?: Cast; | ||
}): Promise<ExecutedQuery<T>>; | ||
execute<T = Row<'object'>>(query: string, args?: ExecuteArgs, options?: ExecuteOptions<'object'>): Promise<ExecutedQuery<T>>; | ||
execute<T = Row<'array'>>(query: string, args: ExecuteArgs, options: ExecuteOptions<'array'>): Promise<ExecutedQuery<T>>; | ||
connection(): Connection; | ||
@@ -85,13 +89,8 @@ } | ||
constructor(conn: Connection); | ||
execute<T = Row<'object'>>(query: string, args?: ExecuteArgs, options?: { | ||
as?: 'object'; | ||
cast?: Cast; | ||
}): Promise<ExecutedQuery<T>>; | ||
execute<T = Row<'array'>>(query: string, args: ExecuteArgs, options: { | ||
as: 'array'; | ||
cast?: Cast; | ||
}): Promise<ExecutedQuery<T>>; | ||
execute<T = Row<'object'>>(query: string, args?: ExecuteArgs, options?: ExecuteOptions<'object'>): Promise<ExecutedQuery<T>>; | ||
execute<T = Row<'array'>>(query: string, args: ExecuteArgs, options: ExecuteOptions<'array'>): Promise<ExecutedQuery<T>>; | ||
} | ||
export declare class Connection { | ||
readonly config: Config; | ||
private fetch; | ||
private session; | ||
@@ -102,10 +101,4 @@ private url; | ||
refresh(): Promise<void>; | ||
execute<T = Row<'object'>>(query: string, args?: ExecuteArgs, options?: { | ||
as?: 'object'; | ||
cast?: Cast; | ||
}): Promise<ExecutedQuery<T>>; | ||
execute<T = Row<'array'>>(query: string, args: ExecuteArgs, options: { | ||
as: 'array'; | ||
cast?: Cast; | ||
}): Promise<ExecutedQuery<T>>; | ||
execute<T = Row<'object'>>(query: string, args?: ExecuteArgs, options?: ExecuteOptions<'object'>): Promise<ExecutedQuery<T>>; | ||
execute<T = Row<'array'>>(query: string, args: ExecuteArgs, options: ExecuteOptions<'array'>): Promise<ExecutedQuery<T>>; | ||
private createSession; | ||
@@ -112,0 +105,0 @@ } |
@@ -7,5 +7,5 @@ "use strict"; | ||
Object.defineProperty(exports, "format", { enumerable: true, get: function () { return sanitization_js_2.format; } }); | ||
var text_js_1 = require("./text.js"); | ||
Object.defineProperty(exports, "hex", { enumerable: true, get: function () { return text_js_1.hex; } }); | ||
const text_js_2 = require("./text.js"); | ||
const text_js_1 = require("./text.js"); | ||
var text_js_2 = require("./text.js"); | ||
Object.defineProperty(exports, "hex", { enumerable: true, get: function () { return text_js_2.hex; } }); | ||
const version_js_1 = require("./version.js"); | ||
@@ -53,8 +53,5 @@ class DatabaseError extends Error { | ||
constructor(config) { | ||
var _a; | ||
this.config = config; | ||
this.fetch = config.fetch || fetch; | ||
this.session = null; | ||
this.config = { ...config }; | ||
if (typeof fetch !== 'undefined') { | ||
(_a = this.config).fetch || (_a.fetch = fetch); | ||
} | ||
if (config.url) { | ||
@@ -92,3 +89,6 @@ const url = new URL(config.url); | ||
const sql = args ? formatter(query, args) : query; | ||
const saved = await postJSON(this.config, url, { query: sql, session: this.session }); | ||
const saved = await postJSON(this.config, this.fetch, url, { | ||
query: sql, | ||
session: this.session | ||
}); | ||
const { result, session, error, timing } = saved; | ||
@@ -127,3 +127,3 @@ if (session) { | ||
const url = new URL('/psdb.v1alpha1.Database/CreateSession', this.url); | ||
const { session } = await postJSON(this.config, url); | ||
const { session } = await postJSON(this.config, this.fetch, url); | ||
this.session = session; | ||
@@ -134,5 +134,4 @@ return session; | ||
exports.Connection = Connection; | ||
async function postJSON(config, url, body = {}) { | ||
async function postJSON(config, fetch, url, body = {}) { | ||
const auth = btoa(`${config.username}:${config.password}`); | ||
const { fetch } = config; | ||
const response = await fetch(url.toString(), { | ||
@@ -184,3 +183,3 @@ method: 'POST', | ||
function parse(result, cast, returnAs) { | ||
const fields = result.fields; | ||
const fields = result.fields ?? []; | ||
const rows = result.rows ?? []; | ||
@@ -202,3 +201,3 @@ return rows.map((row) => returnAs === 'array' ? parseArrayRow(fields, row, cast) : parseObjectRow(fields, row, cast)); | ||
function cast(field, value) { | ||
if (value === '' || value == null) { | ||
if (value == null) { | ||
return value; | ||
@@ -227,2 +226,3 @@ } | ||
case 'TIMESTAMP': | ||
return value; | ||
case 'BLOB': | ||
@@ -233,9 +233,9 @@ case 'BIT': | ||
case 'GEOMETRY': | ||
return value; | ||
return (0, text_js_1.uint8Array)(value); | ||
case 'JSON': | ||
return JSON.parse((0, text_js_2.decode)(value)); | ||
return value ? JSON.parse((0, text_js_1.decode)(value)) : value; | ||
default: | ||
return (0, text_js_2.decode)(value); | ||
return (0, text_js_1.decode)(value); | ||
} | ||
} | ||
exports.cast = cast; |
@@ -1,6 +0,1 @@ | ||
type Stringable = { | ||
toString: () => string; | ||
}; | ||
type Value = null | undefined | number | boolean | string | Array<Value> | Date | Stringable; | ||
export declare function format(query: string, values: Value[] | Record<string, Value>): string; | ||
export {}; | ||
export declare function format(query: string, values: Record<string, any> | any[]): string; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.format = void 0; | ||
const text_js_1 = require("./text.js"); | ||
function format(query, values) { | ||
@@ -41,2 +42,5 @@ return Array.isArray(values) ? replacePosition(query, values) : replaceNamed(query, values); | ||
} | ||
if (value instanceof Uint8Array) { | ||
return (0, text_js_1.uint8ArrayToHex)(value); | ||
} | ||
return quote(value.toString()); | ||
@@ -43,0 +47,0 @@ } |
@@ -1,2 +0,4 @@ | ||
export declare function decode(text: string | null): string; | ||
export declare function decode(text: string | null | undefined): string; | ||
export declare function hex(text: string): string; | ||
export declare function uint8Array(text: string): Uint8Array; | ||
export declare function uint8ArrayToHex(uint8: Uint8Array): string; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.hex = exports.decode = void 0; | ||
exports.uint8ArrayToHex = exports.uint8Array = exports.hex = exports.decode = void 0; | ||
const decoder = new TextDecoder('utf-8'); | ||
function decode(text) { | ||
return text ? decoder.decode(Uint8Array.from(bytes(text))) : ''; | ||
return text ? decoder.decode(uint8Array(text)) : ''; | ||
} | ||
@@ -14,4 +14,13 @@ exports.decode = decode; | ||
exports.hex = hex; | ||
function uint8Array(text) { | ||
return Uint8Array.from(bytes(text)); | ||
} | ||
exports.uint8Array = uint8Array; | ||
function uint8ArrayToHex(uint8) { | ||
const digits = Array.from(uint8).map((i) => i.toString(16).padStart(2, '0')); | ||
return `0x${digits.join('')}`; | ||
} | ||
exports.uint8ArrayToHex = uint8ArrayToHex; | ||
function bytes(text) { | ||
return text.split('').map((c) => c.charCodeAt(0)); | ||
} |
@@ -1,1 +0,1 @@ | ||
export declare const Version = "1.14.0"; | ||
export declare const Version = "1.15.0"; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.Version = void 0; | ||
exports.Version = '1.14.0'; | ||
exports.Version = '1.15.0'; |
@@ -0,1 +1,2 @@ | ||
import { format } from './sanitization.js'; | ||
export { format } from './sanitization.js'; | ||
@@ -25,2 +26,3 @@ export { hex } from './text.js'; | ||
} | ||
type Fetch = (input: string, init?: Req) => Promise<Res>; | ||
type Req = { | ||
@@ -40,2 +42,3 @@ method: string; | ||
export type Cast = typeof cast; | ||
type Format = typeof format; | ||
export interface Config { | ||
@@ -46,4 +49,4 @@ url?: string; | ||
host?: string; | ||
fetch?: (input: string, init?: Req) => Promise<Res>; | ||
format?: (query: string, args: any) => string; | ||
fetch?: Fetch; | ||
format?: Format; | ||
cast?: Cast; | ||
@@ -64,3 +67,10 @@ } | ||
type ExecuteAs = 'array' | 'object'; | ||
type ExecuteArgs = object | any[] | null; | ||
type ExecuteArgs = Record<string, any> | any[] | null; | ||
type ExecuteOptions<T extends ExecuteAs = 'object'> = T extends 'array' ? { | ||
as?: 'object'; | ||
cast?: Cast; | ||
} : T extends 'object' ? { | ||
as: 'array'; | ||
cast?: Cast; | ||
} : never; | ||
export declare class Client { | ||
@@ -70,10 +80,4 @@ readonly config: Config; | ||
transaction<T>(fn: (tx: Transaction) => Promise<T>): Promise<T>; | ||
execute<T = Row<'object'>>(query: string, args?: ExecuteArgs, options?: { | ||
as?: 'object'; | ||
cast?: Cast; | ||
}): Promise<ExecutedQuery<T>>; | ||
execute<T = Row<'array'>>(query: string, args: ExecuteArgs, options: { | ||
as: 'array'; | ||
cast?: Cast; | ||
}): Promise<ExecutedQuery<T>>; | ||
execute<T = Row<'object'>>(query: string, args?: ExecuteArgs, options?: ExecuteOptions<'object'>): Promise<ExecutedQuery<T>>; | ||
execute<T = Row<'array'>>(query: string, args: ExecuteArgs, options: ExecuteOptions<'array'>): Promise<ExecutedQuery<T>>; | ||
connection(): Connection; | ||
@@ -85,13 +89,8 @@ } | ||
constructor(conn: Connection); | ||
execute<T = Row<'object'>>(query: string, args?: ExecuteArgs, options?: { | ||
as?: 'object'; | ||
cast?: Cast; | ||
}): Promise<ExecutedQuery<T>>; | ||
execute<T = Row<'array'>>(query: string, args: ExecuteArgs, options: { | ||
as: 'array'; | ||
cast?: Cast; | ||
}): Promise<ExecutedQuery<T>>; | ||
execute<T = Row<'object'>>(query: string, args?: ExecuteArgs, options?: ExecuteOptions<'object'>): Promise<ExecutedQuery<T>>; | ||
execute<T = Row<'array'>>(query: string, args: ExecuteArgs, options: ExecuteOptions<'array'>): Promise<ExecutedQuery<T>>; | ||
} | ||
export declare class Connection { | ||
readonly config: Config; | ||
private fetch; | ||
private session; | ||
@@ -102,10 +101,4 @@ private url; | ||
refresh(): Promise<void>; | ||
execute<T = Row<'object'>>(query: string, args?: ExecuteArgs, options?: { | ||
as?: 'object'; | ||
cast?: Cast; | ||
}): Promise<ExecutedQuery<T>>; | ||
execute<T = Row<'array'>>(query: string, args: ExecuteArgs, options: { | ||
as: 'array'; | ||
cast?: Cast; | ||
}): Promise<ExecutedQuery<T>>; | ||
execute<T = Row<'object'>>(query: string, args?: ExecuteArgs, options?: ExecuteOptions<'object'>): Promise<ExecutedQuery<T>>; | ||
execute<T = Row<'array'>>(query: string, args: ExecuteArgs, options: ExecuteOptions<'array'>): Promise<ExecutedQuery<T>>; | ||
private createSession; | ||
@@ -112,0 +105,0 @@ } |
import { format } from './sanitization.js'; | ||
export { format } from './sanitization.js'; | ||
import { decode, uint8Array } from './text.js'; | ||
export { hex } from './text.js'; | ||
import { decode } from './text.js'; | ||
import { Version } from './version.js'; | ||
@@ -45,8 +45,5 @@ export class DatabaseError extends Error { | ||
constructor(config) { | ||
var _a; | ||
this.config = config; | ||
this.fetch = config.fetch || fetch; | ||
this.session = null; | ||
this.config = { ...config }; | ||
if (typeof fetch !== 'undefined') { | ||
(_a = this.config).fetch || (_a.fetch = fetch); | ||
} | ||
if (config.url) { | ||
@@ -84,3 +81,6 @@ const url = new URL(config.url); | ||
const sql = args ? formatter(query, args) : query; | ||
const saved = await postJSON(this.config, url, { query: sql, session: this.session }); | ||
const saved = await postJSON(this.config, this.fetch, url, { | ||
query: sql, | ||
session: this.session | ||
}); | ||
const { result, session, error, timing } = saved; | ||
@@ -119,3 +119,3 @@ if (session) { | ||
const url = new URL('/psdb.v1alpha1.Database/CreateSession', this.url); | ||
const { session } = await postJSON(this.config, url); | ||
const { session } = await postJSON(this.config, this.fetch, url); | ||
this.session = session; | ||
@@ -125,5 +125,4 @@ return session; | ||
} | ||
async function postJSON(config, url, body = {}) { | ||
async function postJSON(config, fetch, url, body = {}) { | ||
const auth = btoa(`${config.username}:${config.password}`); | ||
const { fetch } = config; | ||
const response = await fetch(url.toString(), { | ||
@@ -174,3 +173,3 @@ method: 'POST', | ||
function parse(result, cast, returnAs) { | ||
const fields = result.fields; | ||
const fields = result.fields ?? []; | ||
const rows = result.rows ?? []; | ||
@@ -192,3 +191,3 @@ return rows.map((row) => returnAs === 'array' ? parseArrayRow(fields, row, cast) : parseObjectRow(fields, row, cast)); | ||
export function cast(field, value) { | ||
if (value === '' || value == null) { | ||
if (value == null) { | ||
return value; | ||
@@ -217,2 +216,3 @@ } | ||
case 'TIMESTAMP': | ||
return value; | ||
case 'BLOB': | ||
@@ -223,5 +223,5 @@ case 'BIT': | ||
case 'GEOMETRY': | ||
return value; | ||
return uint8Array(value); | ||
case 'JSON': | ||
return JSON.parse(decode(value)); | ||
return value ? JSON.parse(decode(value)) : value; | ||
default: | ||
@@ -228,0 +228,0 @@ return decode(value); |
@@ -1,6 +0,1 @@ | ||
type Stringable = { | ||
toString: () => string; | ||
}; | ||
type Value = null | undefined | number | boolean | string | Array<Value> | Date | Stringable; | ||
export declare function format(query: string, values: Value[] | Record<string, Value>): string; | ||
export {}; | ||
export declare function format(query: string, values: Record<string, any> | any[]): string; |
@@ -0,1 +1,2 @@ | ||
import { uint8ArrayToHex } from './text.js'; | ||
export function format(query, values) { | ||
@@ -37,2 +38,5 @@ return Array.isArray(values) ? replacePosition(query, values) : replaceNamed(query, values); | ||
} | ||
if (value instanceof Uint8Array) { | ||
return uint8ArrayToHex(value); | ||
} | ||
return quote(value.toString()); | ||
@@ -39,0 +43,0 @@ } |
@@ -1,2 +0,4 @@ | ||
export declare function decode(text: string | null): string; | ||
export declare function decode(text: string | null | undefined): string; | ||
export declare function hex(text: string): string; | ||
export declare function uint8Array(text: string): Uint8Array; | ||
export declare function uint8ArrayToHex(uint8: Uint8Array): string; |
const decoder = new TextDecoder('utf-8'); | ||
export function decode(text) { | ||
return text ? decoder.decode(Uint8Array.from(bytes(text))) : ''; | ||
return text ? decoder.decode(uint8Array(text)) : ''; | ||
} | ||
@@ -9,4 +9,11 @@ export function hex(text) { | ||
} | ||
export function uint8Array(text) { | ||
return Uint8Array.from(bytes(text)); | ||
} | ||
export function uint8ArrayToHex(uint8) { | ||
const digits = Array.from(uint8).map((i) => i.toString(16).padStart(2, '0')); | ||
return `0x${digits.join('')}`; | ||
} | ||
function bytes(text) { | ||
return text.split('').map((c) => c.charCodeAt(0)); | ||
} |
@@ -1,1 +0,1 @@ | ||
export declare const Version = "1.14.0"; | ||
export declare const Version = "1.15.0"; |
@@ -1,1 +0,1 @@ | ||
export const Version = '1.14.0'; | ||
export const Version = '1.15.0'; |
{ | ||
"name": "@planetscale/database", | ||
"version": "1.14.0", | ||
"version": "1.15.0", | ||
"description": "A Fetch API-compatible PlanetScale database driver", | ||
@@ -5,0 +5,0 @@ "files": [ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
48264
864
4