Skip to content

Commit

Permalink
Backwards compatible ExecutedQuery
Browse files Browse the repository at this point in the history
  • Loading branch information
ayrton committed Dec 14, 2023
1 parent c31d98a commit 42b2e80
Showing 1 changed file with 16 additions and 14 deletions.
30 changes: 16 additions & 14 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ export { hex } from './text.js'
import { decode } from './text.js'
import { Version } from './version.js'

type Row<T extends ExecuteAs = 'object'> = T extends 'array' ? any[] : T extends 'object' ? Record<string, any> : never

interface VitessError {
message: string
code: string
Expand All @@ -22,7 +24,7 @@ export class DatabaseError extends Error {

type Types = Record<string, string>

export interface ExecutedQuery<T> {
export interface ExecutedQuery<T = Row<'array'> | Row<'object'>> {
headers: string[]
types: Types
rows: T[]
Expand Down Expand Up @@ -98,10 +100,10 @@ interface QueryResult {
rows?: QueryResultRow[]
}

type ExecuteArgs = object | any[] | null

type ExecuteAs = 'array' | 'object'

type ExecuteArgs = object | any[] | null

export class Client {
private config: Config

Expand All @@ -113,17 +115,17 @@ export class Client {
return this.connection().transaction(fn)
}

async execute<T = Record<string, any>>(
async execute<T = Row<'object'>>(
query: string,
args?: ExecuteArgs,
options?: { as?: 'object'; cast?: Cast }
): Promise<ExecutedQuery<T>>
async execute<T = any[]>(
async execute<T = Row<'array'>>(
query: string,
args: ExecuteArgs,
options: { as: 'array'; cast?: Cast }
): Promise<ExecutedQuery<T>>
async execute<T = any>(
async execute<T = Row<'object'> | Row<'array'>>(
query: string,
args: ExecuteArgs = null,
options: any = { as: 'object' }
Expand All @@ -145,17 +147,17 @@ class Tx {
this.conn = conn
}

async execute<T = Record<string, any>>(
async execute<T = Row<'object'>>(
query: string,
args?: ExecuteArgs,
options?: { as?: 'object'; cast?: Cast }
): Promise<ExecutedQuery<T>>
async execute<T = any[]>(
async execute<T = Row<'array'>>(
query: string,
args: ExecuteArgs,
options: { as: 'array'; cast?: Cast }
): Promise<ExecutedQuery<T>>
async execute<T = any>(
async execute<T = Row<'object'> | Row<'array'>>(
query: string,
args: ExecuteArgs = null,
options: any = { as: 'object' }
Expand Down Expand Up @@ -218,17 +220,17 @@ export class Connection {
await this.createSession()
}

async execute<T = Record<string, any>>(
async execute<T = Row<'object'>>(
query: string,
args?: ExecuteArgs,
options?: { as?: 'object'; cast?: Cast }
): Promise<ExecutedQuery<T>>
async execute<T = any[]>(
async execute<T = Row<'array'>>(
query: string,
args: ExecuteArgs,
options: { as: 'array'; cast?: Cast }
): Promise<ExecutedQuery<T>>
async execute<T = any>(
async execute<T = Row<'object'> | Row<'array'>>(
query: string,
args: ExecuteArgs = null,
options: any = { as: 'object' }
Expand Down Expand Up @@ -326,15 +328,15 @@ export function connect(config: Config): Connection {
return new Connection(config)
}

function parseArrayRow<T = any[]>(fields: Field[], rawRow: QueryResultRow, cast: Cast): T {
function parseArrayRow<T = Row<'array'>>(fields: Field[], rawRow: QueryResultRow, cast: Cast): T {
const row = decodeRow(rawRow)

return fields.map((field, ix) => {
return cast(field, row[ix])
}) as T
}

function parseObjectRow<T = Record<string, any>>(fields: Field[], rawRow: QueryResultRow, cast: Cast): T {
function parseObjectRow<T = Row<'object'>>(fields: Field[], rawRow: QueryResultRow, cast: Cast): T {
const row = decodeRow(rawRow)

return fields.reduce((acc, field, ix) => {
Expand Down

0 comments on commit 42b2e80

Please sign in to comment.