Merge branch 'kysely-log' into 'main'

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

See merge request soapbox-pub/ditto!234
This commit is contained in:
Alex Gleason 2024-05-08 17:59:41 +00:00
commit b712986d37
5 changed files with 23 additions and 26 deletions

View File

@ -46,7 +46,6 @@
"nostr-relaypool": "npm:nostr-relaypool2@0.6.34",
"nostr-tools": "npm:nostr-tools@^2.5.1",
"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",
"tseep": "npm:tseep@^1.2.1",
"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 { DittoTables } from '@/db/DittoTables.ts';
import { KyselyLogger } from '@/db/KyselyLogger.ts';
export class DittoPostgres {
static db: Kysely<DittoTables> | undefined;
@ -29,6 +30,7 @@ export class DittoPostgres {
return new PostgresQueryCompiler();
},
},
log: KyselyLogger,
});
}

View File

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

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();