Skip to content

Commit c133347

Browse files
committed
Log queries in debugMode on op-sqlite.
1 parent 01f02e7 commit c133347

File tree

3 files changed

+38
-6
lines changed

3 files changed

+38
-6
lines changed

packages/powersync-op-sqlite/src/db/OPSQLiteConnection.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ import {
1010

1111
export type OPSQLiteConnectionOptions = {
1212
baseDB: DB;
13+
debugMode: boolean;
14+
connectionName: string;
1315
};
1416

1517
export type OPSQLiteUpdateNotification = {
@@ -35,6 +37,16 @@ export class OPSQLiteConnection extends BaseObserver<DBAdapterListener> {
3537
this.DB.updateHook((update) => {
3638
this.addTableUpdate(update);
3739
});
40+
41+
if (options.debugMode) {
42+
const c = this.options.connectionName;
43+
this.execute = withDebug(this.execute.bind(this), `[SQL execute ${c}]`);
44+
this.executeRaw = withDebug(this.executeRaw.bind(this), `[SQL executeRaw ${c}]`);
45+
this.executeBatch = withDebug(this.executeBatch.bind(this), `[SQL executeBatch ${c}]`);
46+
this.get = withDebug(this.get.bind(this), `[SQL get ${c}]`);
47+
this.getAll = withDebug(this.getAll.bind(this), `[SQL getAll ${c}]`);
48+
this.getOptional = withDebug(this.getOptional.bind(this), `[SQL getOptional ${c}]`);
49+
}
3850
}
3951

4052
addTableUpdate(update: OPSQLiteUpdateNotification) {
@@ -133,3 +145,19 @@ export class OPSQLiteConnection extends BaseObserver<DBAdapterListener> {
133145
await this.get("PRAGMA table_info('sqlite_master')");
134146
}
135147
}
148+
149+
function withDebug<T extends (sql: string, ...args: any[]) => Promise<any>>(fn: T, name: string): T {
150+
return (async (sql: string, ...args: any[]): Promise<any> => {
151+
const start = performance.now();
152+
try {
153+
const r = await fn(sql, ...args);
154+
const duration = performance.now() - start;
155+
console.log(name, `[${duration.toFixed(1)}ms]`, sql);
156+
return r;
157+
} catch (e: any) {
158+
const duration = performance.now() - start;
159+
console.error(name, `[ERROR: ${e.message}]`, `[${duration.toFixed(1)}ms]`, sql);
160+
throw e;
161+
}
162+
}) as T;
163+
}

packages/powersync-op-sqlite/src/db/OPSqliteAdapter.ts

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ export type OPSQLiteAdapterOptions = {
1212
name: string;
1313
dbLocation?: string;
1414
sqliteOptions?: SqliteOptions;
15+
debugMode?: boolean;
1516
};
1617

1718
enum LockType {
@@ -50,7 +51,7 @@ export class OPSQLiteDBAdapter extends BaseObserver<DBAdapterListener> implement
5051
this.options.sqliteOptions!;
5152
const dbFilename = this.options.name;
5253

53-
this.writeConnection = await this.openConnection(dbFilename);
54+
this.writeConnection = await this.openConnection('w');
5455

5556
const baseStatements = [
5657
`PRAGMA busy_timeout = ${lockTimeoutMs}`,
@@ -89,16 +90,16 @@ export class OPSQLiteDBAdapter extends BaseObserver<DBAdapterListener> implement
8990

9091
this.readConnections = [];
9192
for (let i = 0; i < READ_CONNECTIONS; i++) {
92-
const conn = await this.openConnection(dbFilename);
93+
const conn = await this.openConnection(`r-${i}`);
9394
for (let statement of readConnectionStatements) {
9495
await conn.execute(statement);
9596
}
9697
this.readConnections.push({ busy: false, connection: conn });
9798
}
9899
}
99100

100-
protected async openConnection(filenameOverride?: string): Promise<OPSQLiteConnection> {
101-
const dbFilename = filenameOverride ?? this.options.name;
101+
protected async openConnection(connectionName: string): Promise<OPSQLiteConnection> {
102+
const dbFilename = this.options.name;
102103
const DB: DB = this.openDatabase(dbFilename, this.options.sqliteOptions?.encryptionKey ?? undefined);
103104

104105
//Load extensions for all connections
@@ -108,7 +109,9 @@ export class OPSQLiteDBAdapter extends BaseObserver<DBAdapterListener> implement
108109
await DB.execute('SELECT powersync_init()');
109110

110111
return new OPSQLiteConnection({
111-
baseDB: DB
112+
baseDB: DB,
113+
debugMode: this.options.debugMode ?? false,
114+
connectionName
112115
});
113116
}
114117

packages/powersync-op-sqlite/src/db/OPSqliteDBOpenFactory.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ export class OPSqliteOpenFactory implements SQLOpenFactory {
1919
return new OPSQLiteDBAdapter({
2020
name: this.options.dbFilename,
2121
dbLocation: this.options.dbLocation,
22-
sqliteOptions: this.sqliteOptions
22+
sqliteOptions: this.sqliteOptions,
23+
debugMode: this.options.debugMode
2324
});
2425
}
2526
}

0 commit comments

Comments
 (0)