Skip to content

Commit 48e0f89

Browse files
Add debugMode
1 parent 4b52a14 commit 48e0f89

File tree

2 files changed

+25
-3
lines changed

2 files changed

+25
-3
lines changed

packages/capacitor/src/PowerSyncDatabase.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ export class PowerSyncDatabase extends WebPowerSyncDatabase {
3636
}
3737
options.logger?.debug(`Using CapacitorSQLiteAdapter for platform: ${platform}`);
3838
return new CapacitorSQLiteAdapter({
39-
dbFilename: options.database.dbFilename
39+
...options.database
4040
});
4141
} else {
4242
options.logger?.debug(`Using default web adapter for web platform`);

packages/capacitor/src/adapter/CapacitorSQLiteAdapter.ts

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,20 @@ import { PowerSyncCore } from '../plugin/PowerSyncCore';
1515
import { messageForErrorCode } from '../plugin/PowerSyncPlugin';
1616
import { 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

Comments
 (0)