SqliteWorker: log amount of time each query takes

This commit is contained in:
Alex Gleason 2024-03-02 17:40:29 -06:00
parent d83bd463b9
commit dc57415df3
No known key found for this signature in database
GPG Key ID: 7211D1F99744FBB7
1 changed files with 11 additions and 3 deletions

View File

@ -1,5 +1,5 @@
/// <reference lib="webworker" /> /// <reference lib="webworker" />
import { ScopedPerformance } from 'https://deno.land/x/scoped_performance@v2.0.0/mod.ts';
import { Comlink, type CompiledQuery, Debug, DenoSqlite3, type QueryResult } from '@/deps.ts'; import { Comlink, type CompiledQuery, Debug, DenoSqlite3, type QueryResult } from '@/deps.ts';
import '@/sentry.ts'; import '@/sentry.ts';
@ -12,12 +12,20 @@ export const SqliteWorker = {
}, },
executeQuery<R>({ sql, parameters }: CompiledQuery): QueryResult<R> { executeQuery<R>({ sql, parameters }: CompiledQuery): QueryResult<R> {
if (!db) throw new Error('Database not open'); if (!db) throw new Error('Database not open');
debug(sql);
return { const perf = new ScopedPerformance();
perf.mark('start');
const result = {
rows: db!.prepare(sql).all(...parameters as any[]) as R[], rows: db!.prepare(sql).all(...parameters as any[]) as R[],
numAffectedRows: BigInt(db!.changes), numAffectedRows: BigInt(db!.changes),
insertId: BigInt(db!.lastInsertRowId), insertId: BigInt(db!.lastInsertRowId),
}; };
const { duration } = perf.measure('end', 'start');
debug(`${sql} \x1b[90m(${(duration / 1000).toFixed(2)}s)\x1b[0m`);
return result;
}, },
destroy() { destroy() {
db?.close(); db?.close();