@@ -106,6 +106,7 @@ export class DuckDBDataSource extends DataSource<any, DuckDBOptions> {
106106 this . logRequest ( firstDataSQL , parameters , options ) ;
107107 const connection = db . connect ( ) ;
108108 await this . loadExtensions ( connection , configurationParameters ) ;
109+ await this . setExecConfig ( connection ) ;
109110 if ( restDataSQL ) this . logRequest ( restDataSQL , parameters , options ) ;
110111 const [ firstData , restDataStream ] = await this . acquireData (
111112 firstDataSQL ,
@@ -297,27 +298,77 @@ export class DuckDBDataSource extends DataSource<any, DuckDBOptions> {
297298
298299 // The dafault duckdb thread is 16
299300 // Setting thread below your CPU core number may result in enhanced performance, according to our observations.
300- private async setThread ( db : duckdb . Database ) {
301- const thread = process . env [ 'DUCKDB_THREADS' ] ;
301+ // private async setThread(db: duckdb.Database) {
302+ // const thread = process.env['DUCKDB_THREADS'];
302303
303- if ( ! thread ) return ;
304- await new Promise ( ( resolve , reject ) => {
305- db . run ( `SET threads=${ Number ( thread ) } ` , ( err : any ) => {
306- if ( err ) reject ( err ) ;
307- this . logger . debug ( `Set thread to ${ thread } ` ) ;
308- resolve ( true ) ;
309- } ) ;
310- } ) ;
311- }
304+ // if (!thread) return;
305+ // await new Promise((resolve, reject) => {
306+ // db.run(`SET threads=${Number(thread)}`, (err: any) => {
307+ // if (err) reject(err);
308+ // this.logger.debug(`Set thread to ${thread}`);
309+ // resolve(true);
310+ // });
311+ // });
312+ // }
312313
313314 private async initDatabase ( dbPath : string ) {
314- const db = new duckdb . Database ( dbPath ) ;
315+ const readonlyMode = process . env [ 'DUCKDB_READONLY_MODE' ] ;
316+ let db ;
317+ if ( readonlyMode ) {
318+ db = new duckdb . Database ( dbPath , duckdb . OPEN_READONLY ) ;
319+ this . logger . debug ( `Open database in readonly mode` ) ;
320+ } else {
321+ db = new duckdb . Database ( dbPath ) ;
322+ this . logger . debug ( `Open database in automatic mode` ) ;
323+ }
315324 const conn = db . connect ( ) ;
316- await this . setThread ( db ) ;
325+ // await this.setThread(db);
326+ await this . setInitConfig ( conn ) ;
317327 await this . installExtensions ( conn ) ;
328+ await this . getCurrentConfig ( conn ) ;
318329 return db ;
319330 }
320331
332+ private async setConfigList ( conn : duckdb . Connection , configs : string [ ] | [ ] ) {
333+ if ( ! configs . length ) return ;
334+ await Promise . all (
335+ configs . map ( ( config ) => {
336+ return new Promise ( ( resolve , reject ) => {
337+ conn . run ( `SET ${ config } ` , ( err : any ) => {
338+ if ( err ) reject ( err ) ;
339+ this . logger . debug ( `Set config ${ config } ` ) ;
340+ resolve ( true ) ;
341+ } ) ;
342+ } ) ;
343+ } )
344+ ) ;
345+ }
346+
347+ private async setInitConfig ( conn : duckdb . Connection ) {
348+ // threads=1, dir='path',
349+ const configListStr = process . env [ 'DUCKDB_INIT_CONFIG' ] ;
350+ const configs = configListStr ?. split ( ',' ) || [ ] ;
351+ await this . setConfigList ( conn , configs ) ;
352+ }
353+ private async setExecConfig ( conn : duckdb . Connection ) {
354+ // threads=1, dir='path',
355+ const configListStr = process . env [ 'DUCKDB_EXEC_CONFIG' ] ;
356+ const configs = configListStr ?. split ( ',' ) || [ ] ;
357+ await this . setConfigList ( conn , configs ) ;
358+ }
359+
360+ private async getCurrentConfig ( conn : duckdb . Connection ) {
361+ return await new Promise ( ( resolve , reject ) => {
362+ conn . all ( 'select * from duckdb_settings()' , ( err : any , res : any ) => {
363+ if ( err ) reject ( err ) ;
364+ for ( const config of res ) {
365+ this . logger . debug ( `Duckdb config: ${ config . name } = ${ config . value } ` ) ;
366+ }
367+ resolve ( true ) ;
368+ } ) ;
369+ } ) ;
370+ }
371+
321372 private async installExtensions (
322373 connection : duckdb . Connection
323374 ) : Promise < void > {
0 commit comments