mirror of
https://gitlab.com/soapbox-pub/ditto.git
synced 2025-12-06 11:29:46 +00:00
37 lines
1 KiB
TypeScript
37 lines
1 KiB
TypeScript
/// <reference lib="webworker" />
|
|
import { ScopedPerformance } from 'https://deno.land/x/scoped_performance@v2.0.0/mod.ts';
|
|
import { Comlink, type CompiledQuery, Debug, DenoSqlite3, type QueryResult } from '@/deps.ts';
|
|
import '@/sentry.ts';
|
|
|
|
let db: DenoSqlite3 | undefined;
|
|
const debug = Debug('ditto:sqlite.worker');
|
|
|
|
export const SqliteWorker = {
|
|
open(path: string): void {
|
|
db = new DenoSqlite3(path);
|
|
},
|
|
executeQuery<R>({ sql, parameters }: CompiledQuery): QueryResult<R> {
|
|
if (!db) throw new Error('Database not open');
|
|
|
|
const perf = new ScopedPerformance();
|
|
perf.mark('start');
|
|
|
|
const result = {
|
|
rows: db!.prepare(sql).all(...parameters as any[]) as R[],
|
|
numAffectedRows: BigInt(db!.changes),
|
|
insertId: BigInt(db!.lastInsertRowId),
|
|
};
|
|
|
|
const { duration } = perf.measure('end', 'start');
|
|
debug(`${sql} \x1b[90m(${(duration / 1000).toFixed(2)}s)\x1b[0m`);
|
|
|
|
return result;
|
|
},
|
|
destroy() {
|
|
db?.close();
|
|
},
|
|
};
|
|
|
|
Comlink.expose(SqliteWorker);
|
|
|
|
self.postMessage(['ready']);
|