mirror of
https://github.com/aljazceru/ditto.git
synced 2026-01-27 17:24:19 +01:00
Use a Kysely logger to log SQL regardless of the adapter used
This commit is contained in:
@@ -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();
|
||||
|
||||
Reference in New Issue
Block a user