@@ -14,6 +14,7 @@ import {
1414 OptimizeOptions ,
1515 ColumnDefinition ,
1616 TableOptions ,
17+ QueryConfig ,
1718} from './interfaces' ;
1819import { CrateDBError , DeserializationError , RequestError } from './utils/Error.js' ;
1920import { StatementGenerator } from './StatementGenerator.js' ;
@@ -25,16 +26,17 @@ const defaultConfig: CrateDBConfig = {
2526 jwt : null , // JWT token for Bearer authentication
2627 host : process . env . CRATEDB_HOST || 'localhost' ,
2728 port : process . env . CRATEDB_PORT ? parseInt ( process . env . CRATEDB_PORT , 10 ) : 4200 , // Default CrateDB port
28- defaultSchema : process . env . CRATEDB_DEFAULT_SCHEMA || null , // Default schema for queries
2929 connectionString : null ,
3030 ssl : false ,
31+ defaultSchema : process . env . CRATEDB_DEFAULT_SCHEMA || null , // Default schema for queries
3132 keepAlive : true , // Enable persistent connections by default
3233 maxConnections : 20 ,
3334 deserialization : {
34- long : 'bigint ' ,
35+ long : 'number ' ,
3536 timestamp : 'date' ,
3637 date : 'date' ,
3738 } ,
39+ rowMode : 'array' ,
3840} ;
3941
4042export class CrateDBClient {
@@ -110,7 +112,7 @@ export class CrateDBClient {
110112 }
111113 }
112114
113- async execute ( stmt : string , args ?: unknown [ ] ) : Promise < CrateDBResponse > {
115+ async execute ( stmt : string , args ?: unknown [ ] , config ?: QueryConfig ) : Promise < CrateDBResponse > {
114116 const startRequestTime = Date . now ( ) ;
115117 const payload = args ? { stmt, args } : { stmt } ;
116118 let body : string ;
@@ -121,11 +123,16 @@ export class CrateDBClient {
121123 throw new RequestError ( `Serialization failed: ${ msg } ` ) ;
122124 }
123125
124- const options = { ...this . httpOptions , body } ;
126+ const options = {
127+ ...this . httpOptions ,
128+ ...config ?. httpOptions ,
129+ body,
130+ } ;
125131
126132 try {
127133 const response = await this . _makeRequest ( options ) ;
128- return this . _addDurations ( startRequestTime , response ) as CrateDBResponse ;
134+ const transformedResponse = this . _transformResponse ( response , config ?. rowMode ?? this . cfg . rowMode ) ;
135+ return this . _addDurations ( startRequestTime , transformedResponse ) as CrateDBResponse ;
129136 } catch ( error : unknown ) {
130137 if ( error instanceof CrateDBError || error instanceof DeserializationError ) {
131138 throw error ;
@@ -231,7 +238,7 @@ export class CrateDBClient {
231238 }
232239
233240 async update ( tableName : string , options : Record < string , unknown > , whereClause : string ) : Promise < CrateDBResponse > {
234- const { keys , values , args } = this . _prepareOptions ( options ) ;
241+ const { args } = this . _prepareOptions ( options ) ;
235242 const query = StatementGenerator . update ( tableName , options , whereClause ) ;
236243 return this . execute ( query , args ) ;
237244 }
@@ -362,6 +369,41 @@ export class CrateDBClient {
362369 } ) ;
363370 }
364371
372+ protected _transformResponse (
373+ response : CrateDBBaseResponse ,
374+ rowMode : 'array' | 'object' = 'object'
375+ ) : CrateDBBaseResponse {
376+ // Return early if not transforming to object mode
377+ if ( rowMode !== 'object' ) {
378+ return response ;
379+ }
380+
381+ // Create a shallow copy of the response
382+ const transformedResponse = { ...response } ;
383+
384+ // Only transform if we have both rows and column names
385+ if ( Array . isArray ( transformedResponse . rows ) && Array . isArray ( transformedResponse . cols ) ) {
386+ transformedResponse . rows = transformedResponse . rows . map ( ( row ) => {
387+ // Skip transformation if row is null or not an array
388+ if ( ! Array . isArray ( row ) ) {
389+ return row ;
390+ }
391+
392+ const obj : Record < string , unknown > = { } ;
393+ transformedResponse . cols ?. forEach ( ( col , index ) => {
394+ // Only set property if column name is a string
395+ if ( typeof col === 'string' ) {
396+ // Preserve null/undefined values
397+ obj [ col ] = row [ index ] ;
398+ }
399+ } ) ;
400+ return obj ;
401+ } ) ;
402+ }
403+
404+ return transformedResponse ;
405+ }
406+
365407 public getConfig ( ) : Readonly < CrateDBConfig > {
366408 return this . cfg ;
367409 }
0 commit comments