Use the SqliteWorker with our new Kysely dialect
This commit is contained in:
parent
01839fbcbf
commit
89b74217b6
10
src/db.ts
10
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<DittoDB>({
|
||||
dialect: new DenoSqlite3Dialect({
|
||||
database: new DenoSqlite3(Conf.dbPath),
|
||||
dialect: new PolySqliteDialect({
|
||||
database: sqliteWorker,
|
||||
}),
|
||||
});
|
||||
|
||||
|
|
|
@ -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';
|
||||
|
|
|
@ -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<QueryResult> {
|
||||
async executeQuery<R>({ sql, parameters }: CompiledQuery): Promise<QueryResult<R>> {
|
||||
await this.ready;
|
||||
return this.#call(['query', [sql, params]]);
|
||||
return this.#call(['query', [sql, parameters]]);
|
||||
}
|
||||
|
||||
#call<T>(msg: [string, unknown[]]): Promise<T> {
|
||||
|
@ -51,6 +47,10 @@ class SqliteWorker {
|
|||
this.#worker.addEventListener('message', handleEvent);
|
||||
});
|
||||
}
|
||||
|
||||
async destroy() {
|
||||
this.#worker.terminate();
|
||||
}
|
||||
}
|
||||
|
||||
export default SqliteWorker;
|
||||
|
|
Loading…
Reference in New Issue