@@ -4,8 +4,6 @@ export { hex } from './text.js'
44import { decode } from './text.js'
55import { Version } from './version.js'
66
7- type Row < T = Record < string , any > | any [ ] > = T
8-
97interface VitessError {
108 message : string
119 code : string
@@ -24,10 +22,10 @@ export class DatabaseError extends Error {
2422
2523type Types = Record < string , string >
2624
27- export interface ExecutedQuery < T = any > {
25+ export interface ExecutedQuery < T > {
2826 headers : string [ ]
2927 types : Types
30- rows : Row < T > [ ]
28+ rows : T [ ]
3129 fields : Field [ ]
3230 size : number
3331 statement : string
@@ -100,18 +98,9 @@ interface QueryResult {
10098 rows ?: QueryResultRow [ ]
10199}
102100
103- type ExecuteAs = 'array' | 'object'
104-
105- type ExecuteOptions = {
106- as ?: ExecuteAs
107- cast ?: Cast
108- }
109-
110101type ExecuteArgs = object | any [ ] | null
111102
112- const defaultExecuteOptions : ExecuteOptions = {
113- as : 'object'
114- }
103+ type ExecuteAs = 'array' | 'object'
115104
116105export class Client {
117106 private config : Config
@@ -124,12 +113,22 @@ export class Client {
124113 return this . connection ( ) . transaction ( fn )
125114 }
126115
127- async execute (
116+ async execute < T = Record < string , any > > (
117+ query : string ,
118+ args ?: ExecuteArgs ,
119+ options ?: { as ?: 'object' ; cast ?: Cast }
120+ ) : Promise < ExecutedQuery < T > >
121+ async execute < T = any [ ] > (
122+ query : string ,
123+ args : ExecuteArgs ,
124+ options : { as : 'array' ; cast ?: Cast }
125+ ) : Promise < ExecutedQuery < T > >
126+ async execute < T = any > (
128127 query : string ,
129128 args : ExecuteArgs = null ,
130- options : ExecuteOptions = defaultExecuteOptions
131- ) : Promise < ExecutedQuery > {
132- return this . connection ( ) . execute ( query , args , options )
129+ options : any = { as : 'object' }
130+ ) : Promise < ExecutedQuery < T > > {
131+ return this . connection ( ) . execute < T > ( query , args , options )
133132 }
134133
135134 connection ( ) : Connection {
@@ -146,12 +145,22 @@ class Tx {
146145 this . conn = conn
147146 }
148147
149- async execute (
148+ async execute < T = Record < string , any > > (
149+ query : string ,
150+ args ?: ExecuteArgs ,
151+ options ?: { as ?: 'object' ; cast ?: Cast }
152+ ) : Promise < ExecutedQuery < T > >
153+ async execute < T = any [ ] > (
154+ query : string ,
155+ args : ExecuteArgs ,
156+ options : { as : 'array' ; cast ?: Cast }
157+ ) : Promise < ExecutedQuery < T > >
158+ async execute < T = any > (
150159 query : string ,
151160 args : ExecuteArgs = null ,
152- options : ExecuteOptions = defaultExecuteOptions
153- ) : Promise < ExecutedQuery > {
154- return this . conn . execute ( query , args , options )
161+ options : any = { as : 'object' }
162+ ) : Promise < ExecutedQuery < any > > {
163+ return this . conn . execute < T > ( query , args , options )
155164 }
156165}
157166
@@ -209,11 +218,21 @@ export class Connection {
209218 await this . createSession ( )
210219 }
211220
212- async execute (
221+ async execute < T = Record < string , any > > (
222+ query : string ,
223+ args ?: ExecuteArgs ,
224+ options ?: { as ?: 'object' ; cast ?: Cast }
225+ ) : Promise < ExecutedQuery < T > >
226+ async execute < T = any [ ] > (
227+ query : string ,
228+ args : ExecuteArgs ,
229+ options : { as : 'array' ; cast ?: Cast }
230+ ) : Promise < ExecutedQuery < T > >
231+ async execute < T = any > (
213232 query : string ,
214233 args : ExecuteArgs = null ,
215- options : ExecuteOptions = defaultExecuteOptions
216- ) : Promise < ExecutedQuery > {
234+ options : any = { as : 'object' }
235+ ) : Promise < ExecutedQuery < T > > {
217236 const url = new URL ( '/psdb.v1alpha1.Database/Execute' , this . url )
218237
219238 const formatter = this . config . format || format
@@ -244,7 +263,7 @@ export class Connection {
244263 }
245264
246265 const castFn = options . cast || this . config . cast || cast
247- const rows = result ? parse ( result , castFn , options . as || 'object' ) : [ ]
266+ const rows = result ? parse < T > ( result , castFn , options . as || 'object' ) : [ ]
248267 const headers = fields . map ( ( f ) => f . name )
249268
250269 const typeByName = ( acc , { name, type } ) => ( { ...acc , [ name ] : type } )
@@ -307,28 +326,28 @@ export function connect(config: Config): Connection {
307326 return new Connection ( config )
308327}
309328
310- function parseArrayRow ( fields : Field [ ] , rawRow : QueryResultRow , cast : Cast ) : Row {
329+ function parseArrayRow < T = any [ ] > ( fields : Field [ ] , rawRow : QueryResultRow , cast : Cast ) : T {
311330 const row = decodeRow ( rawRow )
312331
313332 return fields . map ( ( field , ix ) => {
314333 return cast ( field , row [ ix ] )
315- } )
334+ } ) as T
316335}
317336
318- function parseObjectRow ( fields : Field [ ] , rawRow : QueryResultRow , cast : Cast ) : Row {
337+ function parseObjectRow < T = Record < string , any > > ( fields : Field [ ] , rawRow : QueryResultRow , cast : Cast ) : T {
319338 const row = decodeRow ( rawRow )
320339
321340 return fields . reduce ( ( acc , field , ix ) => {
322341 acc [ field . name ] = cast ( field , row [ ix ] )
323342 return acc
324- } , { } as Row )
343+ } , { } as T )
325344}
326345
327- function parse ( result : QueryResult , cast : Cast , returnAs : ExecuteAs ) : Row [ ] {
346+ function parse < T > ( result : QueryResult , cast : Cast , returnAs : ExecuteAs ) : T [ ] {
328347 const fields = result . fields
329348 const rows = result . rows ?? [ ]
330349 return rows . map ( ( row ) =>
331- returnAs === 'array' ? parseArrayRow ( fields , row , cast ) : parseObjectRow ( fields , row , cast )
350+ returnAs === 'array' ? parseArrayRow < T > ( fields , row , cast ) : parseObjectRow < T > ( fields , row , cast )
332351 )
333352}
334353
0 commit comments