11const path = require ( 'node:path' ) ;
22const fs = require ( 'node:fs' )
3+ const { performance } = require ( 'node:perf_hooks' ) ;
34const Database = require ( 'better-sqlite3' ) ;
5+ const { installPerformanceStats, stopPerformanceStats, isRecordingEnabled, recordDbQueryDuration } = require ( './performanceStats' ) ;
46
57if ( ! fs . existsSync ( path . join ( __dirname , '..' , '..' , 'storage' ) ) ) {
68 fs . mkdirSync ( path . join ( __dirname , '..' , '..' , 'storage' ) , { recursive : true } ) ;
@@ -12,26 +14,67 @@ const db = new Database(path.join(__dirname, '..', '..', 'storage', 'application
1214
1315db . pragma ( 'journal_mode = WAL' ) ;
1416
17+ const originalPrepare = db . prepare . bind ( db ) ;
18+ const originalExec = db . exec . bind ( db ) ;
19+
20+ db . prepare = function patchedPrepare ( sql , ...args ) {
21+ const statement = originalPrepare ( sql , ...args ) ;
22+ return new Proxy ( statement , {
23+ get ( target , prop , receiver ) {
24+ const value = Reflect . get ( target , prop , target ) ;
25+
26+ if ( typeof value !== 'function' || ! [ 'run' , 'get' , 'all' , 'values' ] . includes ( prop ) ) {
27+ return value ;
28+ }
29+
30+ return ( ...methodArgs ) => {
31+ const startTime = performance . now ( ) ;
32+ try {
33+ return value . apply ( target , methodArgs ) ;
34+ } finally {
35+ if ( isRecordingEnabled ( ) ) {
36+ recordDbQueryDuration ( performance . now ( ) - startTime ) ;
37+ }
38+ }
39+ } ;
40+ }
41+ } ) ;
42+ } ;
43+
44+ db . exec = function patchedExec ( sql ) {
45+ const startTime = performance . now ( ) ;
46+ try {
47+ return originalExec ( sql ) ;
48+ } finally {
49+ if ( isRecordingEnabled ( ) ) {
50+ recordDbQueryDuration ( performance . now ( ) - startTime ) ;
51+ }
52+ }
53+ } ;
54+
55+ installPerformanceStats ( db ) ;
56+
1557/**
1658 * Closes the database connection gracefully, ensuring all data is flushed to disk and WAL files are cleaned up.
1759 */
1860function closeDatabases ( ) {
1961 if ( db ) {
2062 try {
63+ stopPerformanceStats ( ) ;
2164 db . pragma ( 'wal_checkpoint(TRUNCATE)' ) ;
2265 db . unsafeMode ( false ) ;
2366 db . pragma ( 'journal_mode = DELETE' ) ;
24- process . log . system ( 'WAL successfully merged and deleted.' ) ;
67+ process . log ? .system ?. ( 'WAL successfully merged and deleted.' ) ;
2568
2669 } catch ( err ) {
2770 console . log ( err ) ;
28- process . log . error ( 'Error during WAL merge:' , err ) ;
71+ process . log ? .error ?. ( 'Error during WAL merge:' , err ) ;
2972 } finally {
3073 try {
3174 db . close ( ) ;
32- process . log . system ( 'SQLite database closed cleanly' ) ;
75+ process . log ? .system ?. ( 'SQLite database closed cleanly' ) ;
3376 } catch ( closeErr ) {
34- process . log . error ( 'Failed to close DB handle:' , closeErr ) ;
77+ process . log ? .error ?. ( 'Failed to close DB handle:' , closeErr ) ;
3578 }
3679 }
3780 }
@@ -140,4 +183,4 @@ module.exports = {
140183 getDBSize,
141184 vacuumDB,
142185 closeDatabases
143- } ;
186+ } ;
0 commit comments