Use a Kysely logger to log SQL regardless of the adapter used

This commit is contained in:
Alex Gleason
2024-05-08 12:56:42 -05:00
parent dbe22b9710
commit 43e8f2a698
5 changed files with 23 additions and 26 deletions

View File

@@ -1,14 +1,11 @@
/// <reference lib="webworker" />
import { Database as SQLite } from '@db/sqlite';
import { Stickynotes } from '@soapbox/stickynotes';
import * as Comlink from 'comlink';
import { CompiledQuery, QueryResult } from 'kysely';
import { ScopedPerformance } from 'scoped_performance';
import '@/sentry.ts';
let db: SQLite | undefined;
const console = new Stickynotes('ditto:sqlite.worker');
export const SqliteWorker = {
open(path: string): void {
@@ -17,32 +14,11 @@ export const SqliteWorker = {
executeQuery<R>({ sql, parameters }: CompiledQuery): QueryResult<R> {
if (!db) throw new Error('Database not open');
const perf = (console.enabled && console.level >= 4) ? new ScopedPerformance() : undefined;
if (perf) {
perf.mark('start');
}
const result = {
return {
rows: db!.prepare(sql).all(...parameters as any[]) as R[],
numAffectedRows: BigInt(db!.changes),
insertId: BigInt(db!.lastInsertRowId),
};
if (perf) {
const { duration } = perf.measure('end', 'start');
console.debug(
sql.replace(/\s+/g, ' '),
JSON.stringify(parameters),
`\x1b[90m(${(duration / 1000).toFixed(2)}s)\x1b[0m`,
);
perf.clearMarks();
perf.clearMeasures();
}
return result;
},
destroy() {
db?.close();