Skip to content

Add a debugMode flag to log queries to the performance timeline #229

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jul 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .changeset/spicy-poems-complain.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@powersync/common': minor
'@powersync/web': minor
---

Add debugMode flag to log queries on the performance timeline
11 changes: 11 additions & 0 deletions packages/common/src/client/SQLOpenFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,17 @@ export interface SQLOpenOptions {
* Directory where the database file is located.
*/
dbLocation?: string;

/**
* Enable debugMode to log queries to the performance timeline.
*
* Defaults to false.
*
* To enable in development builds, use:
*
* debugMode: process.env.NODE_ENV !== 'production'
*/
debugMode?: boolean;
}

export interface SQLOpenFactory {
Expand Down
16 changes: 16 additions & 0 deletions packages/web/src/db/adapters/wa-sqlite/WASQLiteDBAdapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,28 @@ export class WASQLiteDBAdapter extends BaseObserver<DBAdapterListener> implement
private logger: ILogger;
private dbGetHelpers: DBGetUtils | null;
private methods: DBFunctionsInterface | null;
private debugMode: boolean;

constructor(protected options: WASQLiteDBAdapterOptions) {
super();
this.logger = Logger.get('WASQLite');
this.dbGetHelpers = null;
this.methods = null;
this.debugMode = options.debugMode ?? false;
if (this.debugMode) {
const originalExecute = this._execute.bind(this);
this._execute = async (sql, bindings) => {
const start = performance.now();
try {
const r = await originalExecute(sql, bindings);
performance.measure(`[SQL] ${sql}`, { start });
return r;
} catch (e: any) {
performance.measure(`[SQL] [ERROR: ${e.message}] ${sql}`, { start });
throw e;
}
};
}
this.initialized = this.init();
this.dbGetHelpers = this.generateDBHelpers({
execute: (query, params) => this.acquireLock(() => this._execute(query, params))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ export const schemaManager = new DynamicSchemaManager();

export const db = new PowerSyncDatabase({
database: {
dbFilename: 'example.db'
dbFilename: 'example.db',
debugMode: true
},
schema: schemaManager.buildSchema()
});
Expand Down
Loading