File tree Expand file tree Collapse file tree 3 files changed +26
-1
lines changed
web/src/db/adapters/wa-sqlite
tools/diagnostics-app/src/library/powersync Expand file tree Collapse file tree 3 files changed +26
-1
lines changed Original file line number Diff line number Diff line change @@ -9,6 +9,14 @@ export interface SQLOpenOptions {
99 * Directory where the database file is located.
1010 */
1111 dbLocation ?: string ;
12+
13+ /**
14+ * Enable debugMode to log queries to the performance timeline.
15+ *
16+ * Defaults to false if `process.env.NODE_ENV == 'production'`,
17+ * true otherwise.
18+ */
19+ debugMode ?: boolean ;
1220}
1321
1422export interface SQLOpenFactory {
Original file line number Diff line number Diff line change @@ -39,12 +39,28 @@ export class WASQLiteDBAdapter extends BaseObserver<DBAdapterListener> implement
3939 private logger : ILogger ;
4040 private dbGetHelpers : DBGetUtils | null ;
4141 private methods : DBFunctionsInterface | null ;
42+ private debugMode : boolean ;
4243
4344 constructor ( protected options : WASQLiteDBAdapterOptions ) {
4445 super ( ) ;
4546 this . logger = Logger . get ( 'WASQLite' ) ;
4647 this . dbGetHelpers = null ;
4748 this . methods = null ;
49+ this . debugMode = options . debugMode ?? process . env . NODE_ENV !== 'production' ;
50+ if ( this . debugMode ) {
51+ const originalExecute = this . _execute . bind ( this ) ;
52+ this . _execute = async ( sql , bindings ) => {
53+ const start = performance . now ( ) ;
54+ try {
55+ const r = await originalExecute ( sql , bindings ) ;
56+ performance . measure ( `SQL: ${ sql } ` , { start } ) ;
57+ return r ;
58+ } catch ( e : any ) {
59+ performance . measure ( `SQL ERROR: ${ e . message } | ${ sql } ` , { start } ) ;
60+ throw e ;
61+ }
62+ } ;
63+ }
4864 this . initialized = this . init ( ) ;
4965 this . dbGetHelpers = this . generateDBHelpers ( {
5066 execute : ( query , params ) => this . acquireLock ( ( ) => this . _execute ( query , params ) )
Original file line number Diff line number Diff line change @@ -25,7 +25,8 @@ export const schemaManager = new DynamicSchemaManager();
2525
2626export const db = new PowerSyncDatabase ( {
2727 database : {
28- dbFilename : 'example.db'
28+ dbFilename : 'example.db' ,
29+ debugMode : true
2930 } ,
3031 schema : schemaManager . buildSchema ( )
3132} ) ;
You can’t perform that action at this time.
0 commit comments