From 89b74217b62338d67d328333ec5aaa2fd116876d Mon Sep 17 00:00:00 2001 From: Alex Gleason Date: Fri, 1 Dec 2023 18:15:39 -0600 Subject: [PATCH] Use the SqliteWorker with our new Kysely dialect --- src/db.ts | 10 +++++++--- src/deps.ts | 4 +++- src/workers/sqlite.ts | 14 +++++++------- 3 files changed, 17 insertions(+), 11 deletions(-) diff --git a/src/db.ts b/src/db.ts index 6a7778f..c05abe6 100644 --- a/src/db.ts +++ b/src/db.ts @@ -1,9 +1,10 @@ import fs from 'node:fs/promises'; import path from 'node:path'; -import { DenoSqlite3, DenoSqlite3Dialect, FileMigrationProvider, Kysely, Migrator } from '@/deps.ts'; +import { PolySqliteDialect, FileMigrationProvider, Kysely, Migrator } from '@/deps.ts'; import { Conf } from '@/config.ts'; import { getPragma, setPragma } from '@/pragma.ts'; +import { sqliteWorker } from '@/workers.ts'; interface DittoDB { events: EventRow; @@ -56,9 +57,12 @@ interface UnattachedMediaRow { uploaded_at: Date; } +await sqliteWorker.ready; +await sqliteWorker.open(); + const db = new Kysely({ - dialect: new DenoSqlite3Dialect({ - database: new DenoSqlite3(Conf.dbPath), + dialect: new PolySqliteDialect({ + database: sqliteWorker, }), }); diff --git a/src/deps.ts b/src/deps.ts index 4afd54d..6cbfa9c 100644 --- a/src/deps.ts +++ b/src/deps.ts @@ -63,11 +63,13 @@ export { FileMigrationProvider, type Insertable, Kysely, + type QueryResult, + type CompiledQuery, Migrator, type NullableInsertKeys, sql, } from 'npm:kysely@^0.26.3'; -export { DenoSqlite3Dialect } from 'https://gitlab.com/soapbox-pub/kysely-deno-sqlite/-/raw/v2.0.0/mod.ts'; +export { PolySqliteDialect } from 'https://gitlab.com/soapbox-pub/kysely-deno-sqlite/-/raw/v2.0.0/mod.ts'; export { default as tldts } from 'npm:tldts@^6.0.14'; export * as cron from 'https://deno.land/x/deno_cron@v1.0.0/cron.ts'; export { S3Client } from 'https://deno.land/x/s3_lite_client@0.6.1/mod.ts'; diff --git a/src/workers/sqlite.ts b/src/workers/sqlite.ts index 22f39e3..8d23f20 100644 --- a/src/workers/sqlite.ts +++ b/src/workers/sqlite.ts @@ -1,8 +1,4 @@ -interface QueryResult { - rows: unknown[]; - numAffectedRows: bigint; - insertId: bigint; -} +import type { CompiledQuery, QueryResult } from '@/deps.ts'; class SqliteWorker { #path: string; @@ -29,9 +25,9 @@ class SqliteWorker { return this.#call(['open', [this.#path]]); } - async query(sql: string, params?: any): Promise { + async executeQuery({ sql, parameters }: CompiledQuery): Promise> { await this.ready; - return this.#call(['query', [sql, params]]); + return this.#call(['query', [sql, parameters]]); } #call(msg: [string, unknown[]]): Promise { @@ -51,6 +47,10 @@ class SqliteWorker { this.#worker.addEventListener('message', handleEvent); }); } + + async destroy() { + this.#worker.terminate(); + } } export default SqliteWorker;