@@ -8,6 +8,7 @@ export interface DBMConstructorOptions {
88 fileManager : FileManagerType ;
99 logger : DBMLogger ;
1010 onEvent ?: ( event : DBMEvent ) => void ;
11+ onDuckdbShutdown ?: ( ) => void ;
1112 instanceManager : InstanceManagerType ;
1213 options ?: {
1314 /**
@@ -16,6 +17,18 @@ export interface DBMConstructorOptions {
1617 shutdownInactiveTime ?: number ;
1718 } ;
1819}
20+
21+ type BeforeQueryHook = (
22+ data : {
23+ tableName : string ;
24+ files : string [ ] ;
25+ } [ ]
26+ ) => Promise < void > ;
27+
28+ type QueryOptions = {
29+ beforeQuery : BeforeQueryHook ;
30+ } ;
31+
1932export class DBM {
2033 private fileManager : FileManagerType ;
2134 private instanceManager : InstanceManagerType ;
@@ -31,25 +44,39 @@ export class DBM {
3144 * Timestamp when the query was added to the queue
3245 */
3346 timestamp : number ;
47+ options ?: { beforeQuery : BeforeQueryHook } ;
3448 } [ ] = [ ] ;
49+ private beforeQuery ?: ( {
50+ query,
51+ tableByFiles,
52+ } : {
53+ query : string ;
54+ tableByFiles : {
55+ tableName : string ;
56+ files : string [ ] ;
57+ } [ ] ;
58+ } ) => Promise < void > ;
3559 private queryQueueRunning = false ;
3660 private logger : DBMLogger ;
3761 private onEvent ?: ( event : DBMEvent ) => void ;
3862 private options : DBMConstructorOptions [ 'options' ] ;
3963 private terminateDBTimeout : NodeJS . Timeout | null = null ;
64+ private onDuckdbShutdown ?: ( ) => void ;
4065
4166 constructor ( {
4267 fileManager,
4368 logger,
4469 onEvent,
4570 options,
4671 instanceManager,
72+ onDuckdbShutdown,
4773 } : DBMConstructorOptions ) {
4874 this . fileManager = fileManager ;
4975 this . logger = logger ;
5076 this . onEvent = onEvent ;
5177 this . options = options ;
5278 this . instanceManager = instanceManager ;
79+ this . onDuckdbShutdown = onDuckdbShutdown ;
5380 }
5481
5582 private async _shutdown ( ) {
@@ -58,6 +85,9 @@ export class DBM {
5885 this . connection = null ;
5986 }
6087 this . logger . debug ( 'Shutting down the DB' ) ;
88+ if ( this . onDuckdbShutdown ) {
89+ this . onDuckdbShutdown ( ) ;
90+ }
6191 await this . instanceManager . terminateDB ( ) ;
6292 }
6393
@@ -97,7 +127,11 @@ export class DBM {
97127 return this . connection ;
98128 }
99129
100- private async _queryWithTableNames ( query : string , tableNames : string [ ] ) {
130+ private async _queryWithTableNames (
131+ query : string ,
132+ tableNames : string [ ] ,
133+ options ?: QueryOptions
134+ ) {
101135 /**
102136 * Load all the files into the database
103137 */
@@ -117,6 +151,17 @@ export class DBM {
117151 duration : endMountTime - startMountTime ,
118152 } ) ;
119153
154+ const tablesFileData = await this . fileManager . getFilesNameForTables (
155+ tableNames
156+ ) ;
157+
158+ /**
159+ * Execute the beforeQuery hook
160+ */
161+ if ( options ?. beforeQuery ) {
162+ await options . beforeQuery ( tablesFileData ) ;
163+ }
164+
120165 /**
121166 * Execute the query
122167 */
@@ -189,11 +234,11 @@ export class DBM {
189234 /**
190235 * Get the first query
191236 */
192- const query = this . queriesQueue . shift ( ) ;
237+ const queueElement = this . queriesQueue . shift ( ) ;
193238 /**
194239 * If there is no query, stop the queue
195240 */
196- if ( ! query ) {
241+ if ( ! queueElement ) {
197242 await this . _stopQueryQueue ( ) ;
198243 return ;
199244 }
@@ -202,41 +247,42 @@ export class DBM {
202247 const startTime = Date . now ( ) ;
203248 this . logger . debug (
204249 'Time since query was added to the queue:' ,
205- startTime - query . timestamp ,
250+ startTime - queueElement . timestamp ,
206251 'ms' ,
207- query . query
252+ queueElement . query
208253 ) ;
209254
210255 this . _emitEvent ( {
211256 event_name : 'query_queue_duration' ,
212- duration : startTime - query . timestamp ,
257+ duration : startTime - queueElement . timestamp ,
213258 } ) ;
214259
215260 /**
216261 * Execute the query
217262 */
218263 const result = await this . _queryWithTableNames (
219- query . query ,
220- query . tableNames
264+ queueElement . query ,
265+ queueElement . tableNames ,
266+ queueElement . options
221267 ) ;
222268 const endTime = Date . now ( ) ;
223269
224270 this . logger . debug (
225271 'Total time spent along with queue time' ,
226- endTime - query . timestamp ,
272+ endTime - queueElement . timestamp ,
227273 'ms' ,
228- query . query
274+ queueElement . query
229275 ) ;
230276 /**
231277 * Resolve the promise
232278 */
233- query . promise . resolve ( result ) ;
279+ queueElement . promise . resolve ( result ) ;
234280 } catch ( error ) {
235- this . logger . warn ( 'Error while executing query:' , query . query ) ;
281+ this . logger . warn ( 'Error while executing query:' , queueElement . query ) ;
236282 /**
237283 * Reject the promise, so the caller can catch the error
238284 */
239- query . promise . reject ( error ) ;
285+ queueElement . promise . reject ( error ) ;
240286 }
241287
242288 /**
@@ -266,7 +312,11 @@ export class DBM {
266312 return this . queryQueueRunning ;
267313 }
268314
269- public async queryWithTableNames ( query : string , tableNames : string [ ] ) {
315+ public async queryWithTableNames (
316+ query : string ,
317+ tableNames : string [ ] ,
318+ options ?: { beforeQuery : BeforeQueryHook }
319+ ) {
270320 const promise = new Promise ( ( resolve , reject ) => {
271321 this . queriesQueue . push ( {
272322 query,
@@ -276,6 +326,7 @@ export class DBM {
276326 reject,
277327 } ,
278328 timestamp : Date . now ( ) ,
329+ options,
279330 } ) ;
280331 } ) ;
281332 this . _startQueryQueue ( ) ;
0 commit comments