diff --git a/deno.json b/deno.json index 5debcfd..5d45588 100644 --- a/deno.json +++ b/deno.json @@ -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", diff --git a/src/db/KyselyLogger.ts b/src/db/KyselyLogger.ts new file mode 100644 index 0000000..e39cbd0 --- /dev/null +++ b/src/db/KyselyLogger.ts @@ -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`, + ); + } +}; diff --git a/src/db/adapters/DittoPostgres.ts b/src/db/adapters/DittoPostgres.ts index 541c1d7..d0abbf9 100644 --- a/src/db/adapters/DittoPostgres.ts +++ b/src/db/adapters/DittoPostgres.ts @@ -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 | undefined; @@ -29,6 +30,7 @@ export class DittoPostgres { return new PostgresQueryCompiler(); }, }, + log: KyselyLogger, }); } diff --git a/src/db/adapters/DittoSQLite.ts b/src/db/adapters/DittoSQLite.ts index 6848aee..fe225a2 100644 --- a/src/db/adapters/DittoSQLite.ts +++ b/src/db/adapters/DittoSQLite.ts @@ -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. diff --git a/src/workers/sqlite.worker.ts b/src/workers/sqlite.worker.ts index a3ff1d6..68c70d6 100644 --- a/src/workers/sqlite.worker.ts +++ b/src/workers/sqlite.worker.ts @@ -1,14 +1,11 @@ /// 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({ sql, parameters }: CompiledQuery): QueryResult { 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();