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
No known key found for this signature in database
GPG Key ID: 7211D1F99744FBB7
5 changed files with 23 additions and 26 deletions

View File

@ -46,7 +46,6 @@
"nostr-relaypool": "npm:nostr-relaypool2@0.6.34", "nostr-relaypool": "npm:nostr-relaypool2@0.6.34",
"nostr-tools": "npm:nostr-tools@^2.5.1", "nostr-tools": "npm:nostr-tools@^2.5.1",
"nostr-wasm": "npm:nostr-wasm@^0.1.0", "nostr-wasm": "npm:nostr-wasm@^0.1.0",
"scoped_performance" :"https://deno.land/x/scoped_performance@v2.0.0/mod.ts",
"tldts": "npm:tldts@^6.0.14", "tldts": "npm:tldts@^6.0.14",
"tseep": "npm:tseep@^1.2.1", "tseep": "npm:tseep@^1.2.1",
"type-fest": "npm:type-fest@^4.3.0", "type-fest": "npm:type-fest@^4.3.0",

18
src/db/KyselyLogger.ts Normal file
View File

@ -0,0 +1,18 @@
import { Stickynotes } from '@soapbox/stickynotes';
import { Logger } from 'kysely';
/** Log the SQL for queries. */
export const KyselyLogger: Logger = (event) => {
if (event.level === 'query') {
const console = new Stickynotes('ditto:sql');
const { query, queryDurationMillis } = event;
const { sql, parameters } = query;
console.debug(
sql,
JSON.stringify(parameters),
`\x1b[90m(${(queryDurationMillis / 1000).toFixed(2)}s)\x1b[0m`,
);
}
};

View File

@ -3,6 +3,7 @@ import { PostgreSQLDriver } from 'kysely_deno_postgres';
import { Conf } from '@/config.ts'; import { Conf } from '@/config.ts';
import { DittoTables } from '@/db/DittoTables.ts'; import { DittoTables } from '@/db/DittoTables.ts';
import { KyselyLogger } from '@/db/KyselyLogger.ts';
export class DittoPostgres { export class DittoPostgres {
static db: Kysely<DittoTables> | undefined; static db: Kysely<DittoTables> | undefined;
@ -29,6 +30,7 @@ export class DittoPostgres {
return new PostgresQueryCompiler(); return new PostgresQueryCompiler();
}, },
}, },
log: KyselyLogger,
}); });
} }

View File

@ -3,6 +3,7 @@ import { Kysely, sql } from 'kysely';
import { Conf } from '@/config.ts'; import { Conf } from '@/config.ts';
import { DittoTables } from '@/db/DittoTables.ts'; import { DittoTables } from '@/db/DittoTables.ts';
import { KyselyLogger } from '@/db/KyselyLogger.ts';
import SqliteWorker from '@/workers/sqlite.ts'; import SqliteWorker from '@/workers/sqlite.ts';
export class DittoSQLite { export class DittoSQLite {
@ -17,6 +18,7 @@ export class DittoSQLite {
dialect: new PolySqliteDialect({ dialect: new PolySqliteDialect({
database: sqliteWorker, database: sqliteWorker,
}), }),
log: KyselyLogger,
}); });
// Set PRAGMA values. // Set PRAGMA values.

View File

@ -1,14 +1,11 @@
/// <reference lib="webworker" /> /// <reference lib="webworker" />
import { Database as SQLite } from '@db/sqlite'; import { Database as SQLite } from '@db/sqlite';
import { Stickynotes } from '@soapbox/stickynotes';
import * as Comlink from 'comlink'; import * as Comlink from 'comlink';
import { CompiledQuery, QueryResult } from 'kysely'; import { CompiledQuery, QueryResult } from 'kysely';
import { ScopedPerformance } from 'scoped_performance';
import '@/sentry.ts'; import '@/sentry.ts';
let db: SQLite | undefined; let db: SQLite | undefined;
const console = new Stickynotes('ditto:sqlite.worker');
export const SqliteWorker = { export const SqliteWorker = {
open(path: string): void { open(path: string): void {
@ -17,32 +14,11 @@ 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');
const perf = (console.enabled && console.level >= 4) ? new ScopedPerformance() : undefined; return {
if (perf) {
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),
}; };
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() { destroy() {
db?.close(); db?.close();