@@ -15,6 +15,20 @@ import { PowerSyncCore } from '../plugin/PowerSyncCore';
1515import { messageForErrorCode } from '../plugin/PowerSyncPlugin' ;
1616import { CapacitorSQLiteOpenFactoryOptions , DEFAULT_SQLITE_OPTIONS } from './CapacitorSQLiteOpenFactory' ;
1717
18+ /**
19+ * Monitors the execution time of a query and logs it to the performance timeline.
20+ */
21+ async function monitorQuery ( sql : string , executor : ( ) => Promise < QueryResult > ) : Promise < QueryResult > {
22+ const start = performance . now ( ) ;
23+ try {
24+ const r = await executor ( ) ;
25+ performance . measure ( `[SQL] ${ sql } ` , { start } ) ;
26+ return r ;
27+ } catch ( e : any ) {
28+ performance . measure ( `[SQL] [ERROR: ${ e . message } ] ${ sql } ` , { start } ) ;
29+ throw e ;
30+ }
31+ }
1832/**
1933 * An implementation of {@link DBAdapter} using the Capacitor Community SQLite [plugin](https://github.com/capacitor-community/sqlite).
2034 *
@@ -92,7 +106,7 @@ export class CapacitorSQLiteAdapter extends BaseObserver<DBAdapterListener> impl
92106 }
93107
94108 protected generateLockContext ( db : SQLiteDBConnection ) : LockContext {
95- const execute = async ( query : string , params : any [ ] = [ ] ) : Promise < QueryResult > => {
109+ const _execute = async ( query : string , params : any [ ] = [ ] ) : Promise < QueryResult > => {
96110 // This driver does not support returning results for execute methods
97111 if ( query . toLowerCase ( ) . trim ( ) . startsWith ( 'select' ) ) {
98112 let result = await db . query ( query , params ) ;
@@ -119,7 +133,11 @@ export class CapacitorSQLiteAdapter extends BaseObserver<DBAdapterListener> impl
119133 }
120134 } ;
121135
122- const executeQuery = async ( query : string , params ?: any [ ] ) : Promise < QueryResult > => {
136+ const execute = this . options . debugMode
137+ ? ( sql : string , params ?: any [ ] ) => monitorQuery ( sql , ( ) => _execute ( sql , params ) )
138+ : _execute ;
139+
140+ const _executeQuery = async ( query : string , params ?: any [ ] ) : Promise < QueryResult > => {
123141 let result = await db . query ( query , params ) ;
124142
125143 let arrayResult = result . values ?? [ ] ;
@@ -134,6 +152,10 @@ export class CapacitorSQLiteAdapter extends BaseObserver<DBAdapterListener> impl
134152 } ;
135153 } ;
136154
155+ const executeQuery = this . options . debugMode
156+ ? ( sql : string , params ?: any [ ] ) => monitorQuery ( sql , ( ) => _executeQuery ( sql , params ) )
157+ : _executeQuery ;
158+
137159 const getAll = async < T > ( query : string , params ?: any [ ] ) : Promise < T [ ] > => {
138160 const result = await executeQuery ( query , params ) ;
139161 return result . rows ?. _array ?? ( [ ] as T [ ] ) ;
0 commit comments