@planetscale/database
Advanced tools
Comparing version 1.3.0 to 1.4.0
export { format } from './sanitization.js'; | ||
export { hex } from './text.js'; | ||
declare type Row = Record<string, any>; | ||
declare type Row = Record<string, any> | any[]; | ||
interface VitessError { | ||
@@ -18,2 +18,3 @@ message: string; | ||
rows: Row[]; | ||
fields: Field[]; | ||
size: number; | ||
@@ -59,2 +60,7 @@ statement: string; | ||
} | ||
declare type ExecuteAs = 'array' | 'object'; | ||
declare type ExecuteOptions = { | ||
as?: ExecuteAs; | ||
}; | ||
declare type ExecuteArgs = object | any[] | null; | ||
export declare class Client { | ||
@@ -64,3 +70,3 @@ private config; | ||
transaction<T>(fn: (tx: Transaction) => Promise<T>): Promise<T>; | ||
execute(query: string, args?: object | any[]): Promise<ExecutedQuery>; | ||
execute(query: string, args?: ExecuteArgs, options?: ExecuteOptions): Promise<ExecutedQuery>; | ||
connection(): Connection; | ||
@@ -72,3 +78,3 @@ } | ||
constructor(conn: Connection); | ||
execute(query: string, args?: object | any[]): Promise<ExecutedQuery>; | ||
execute(query: string, args?: ExecuteArgs, options?: ExecuteOptions): Promise<ExecutedQuery>; | ||
} | ||
@@ -81,3 +87,3 @@ export declare class Connection { | ||
refresh(): Promise<void>; | ||
execute(query: string, args?: any): Promise<ExecutedQuery>; | ||
execute(query: string, args?: ExecuteArgs, options?: ExecuteOptions): Promise<ExecutedQuery>; | ||
private createSession; | ||
@@ -84,0 +90,0 @@ } |
@@ -14,2 +14,5 @@ import { format } from './sanitization.js'; | ||
} | ||
const defaultExecuteOptions = { | ||
as: 'object' | ||
}; | ||
export class Client { | ||
@@ -22,4 +25,4 @@ constructor(config) { | ||
} | ||
async execute(query, args) { | ||
return this.connection().execute(query, args); | ||
async execute(query, args = null, options = defaultExecuteOptions) { | ||
return this.connection().execute(query, args, options); | ||
} | ||
@@ -34,4 +37,4 @@ connection() { | ||
} | ||
async execute(query, args) { | ||
return this.conn.execute(query, args); | ||
async execute(query, args = null, options = defaultExecuteOptions) { | ||
return this.conn.execute(query, args, options); | ||
} | ||
@@ -71,3 +74,3 @@ } | ||
} | ||
async execute(query, args) { | ||
async execute(query, args = null, options = defaultExecuteOptions) { | ||
const url = new URL('/psdb.v1alpha1.Database/Execute', `https://${this.config.host}`); | ||
@@ -86,9 +89,11 @@ const formatter = this.config.format || format; | ||
this.session = session; | ||
const rows = result ? parse(result, this.config.cast || cast) : []; | ||
const headers = result ? result.fields?.map((f) => f.name) ?? [] : []; | ||
const fields = result?.fields ?? []; | ||
const rows = result ? parse(result, this.config.cast || cast, options.as || 'object') : []; | ||
const headers = fields.map((f) => f.name); | ||
const typeByName = (acc, { name, type }) => ({ ...acc, [name]: type }); | ||
const types = result ? result.fields?.reduce(typeByName, {}) ?? {} : {}; | ||
const types = fields.reduce(typeByName, {}); | ||
return { | ||
headers, | ||
types, | ||
fields, | ||
rows, | ||
@@ -142,4 +147,10 @@ rowsAffected, | ||
} | ||
function parseRow(fields, rawRow, cast) { | ||
function parseArrayRow(fields, rawRow, cast) { | ||
const row = decodeRow(rawRow); | ||
return fields.map((field, ix) => { | ||
return cast(field, row[ix]); | ||
}); | ||
} | ||
function parseObjectRow(fields, rawRow, cast) { | ||
const row = decodeRow(rawRow); | ||
return fields.reduce((acc, field, ix) => { | ||
@@ -150,6 +161,6 @@ acc[field.name] = cast(field, row[ix]); | ||
} | ||
function parse(result, cast) { | ||
function parse(result, cast, returnAs) { | ||
const fields = result.fields; | ||
const rows = result.rows ?? []; | ||
return rows.map((row) => parseRow(fields, row, cast)); | ||
return rows.map((row) => returnAs === 'array' ? parseArrayRow(fields, row, cast) : parseObjectRow(fields, row, cast)); | ||
} | ||
@@ -156,0 +167,0 @@ function decodeRow(row) { |
@@ -35,3 +35,3 @@ export function format(query, values) { | ||
if (value instanceof Date) { | ||
return quote(value.toISOString()); | ||
return quote(value.toISOString().replace('Z', '')); | ||
} | ||
@@ -38,0 +38,0 @@ return quote(value.toString()); |
@@ -1,1 +0,1 @@ | ||
export declare const Version = "1.3.0"; | ||
export declare const Version = "1.4.0"; |
@@ -1,1 +0,1 @@ | ||
export const Version = '1.3.0'; | ||
export const Version = '1.4.0'; |
{ | ||
"name": "@planetscale/database", | ||
"version": "1.3.0", | ||
"version": "1.4.0", | ||
"description": "A Fetch API-compatible PlanetScale database driver", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
29647
384