Merge branch 'sqlite-worker' into 'main'
SqliteWorker See merge request soapbox-pub/ditto!57
This commit is contained in:
commit
e5d6c44437
|
@ -1,4 +1,4 @@
|
||||||
image: denoland/deno:1.37.1
|
image: denoland/deno:1.38.4
|
||||||
|
|
||||||
default:
|
default:
|
||||||
interruptible: true
|
interruptible: true
|
||||||
|
|
|
@ -1 +1 @@
|
||||||
deno 1.38.3
|
deno 1.38.4
|
||||||
|
|
10
src/db.ts
10
src/db.ts
|
@ -1,9 +1,10 @@
|
||||||
import fs from 'node:fs/promises';
|
import fs from 'node:fs/promises';
|
||||||
import path from 'node:path';
|
import path from 'node:path';
|
||||||
|
|
||||||
import { DenoSqlite3, DenoSqliteDialect, FileMigrationProvider, Kysely, Migrator } from '@/deps.ts';
|
import { FileMigrationProvider, Kysely, Migrator, PolySqliteDialect } from '@/deps.ts';
|
||||||
import { Conf } from '@/config.ts';
|
import { Conf } from '@/config.ts';
|
||||||
import { getPragma, setPragma } from '@/pragma.ts';
|
import { getPragma, setPragma } from '@/pragma.ts';
|
||||||
|
import SqliteWorker from '@/workers/sqlite.ts';
|
||||||
|
|
||||||
interface DittoDB {
|
interface DittoDB {
|
||||||
events: EventRow;
|
events: EventRow;
|
||||||
|
@ -56,9 +57,12 @@ interface UnattachedMediaRow {
|
||||||
uploaded_at: Date;
|
uploaded_at: Date;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const sqliteWorker = new SqliteWorker();
|
||||||
|
await sqliteWorker.open(Conf.dbPath);
|
||||||
|
|
||||||
const db = new Kysely<DittoDB>({
|
const db = new Kysely<DittoDB>({
|
||||||
dialect: new DenoSqliteDialect({
|
dialect: new PolySqliteDialect({
|
||||||
database: new DenoSqlite3(Conf.dbPath),
|
database: sqliteWorker,
|
||||||
}),
|
}),
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
|
@ -60,14 +60,16 @@ export {
|
||||||
export { Database as DenoSqlite3 } from 'https://deno.land/x/sqlite3@0.9.1/mod.ts';
|
export { Database as DenoSqlite3 } from 'https://deno.land/x/sqlite3@0.9.1/mod.ts';
|
||||||
export * as dotenv from 'https://deno.land/std@0.198.0/dotenv/mod.ts';
|
export * as dotenv from 'https://deno.land/std@0.198.0/dotenv/mod.ts';
|
||||||
export {
|
export {
|
||||||
|
type CompiledQuery,
|
||||||
FileMigrationProvider,
|
FileMigrationProvider,
|
||||||
type Insertable,
|
type Insertable,
|
||||||
Kysely,
|
Kysely,
|
||||||
Migrator,
|
Migrator,
|
||||||
type NullableInsertKeys,
|
type NullableInsertKeys,
|
||||||
|
type QueryResult,
|
||||||
sql,
|
sql,
|
||||||
} from 'npm:kysely@^0.25.0';
|
} from 'npm:kysely@^0.26.3';
|
||||||
export { DenoSqliteDialect } from 'https://gitlab.com/soapbox-pub/kysely-deno-sqlite/-/raw/v1.1.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 { 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 * 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';
|
export { S3Client } from 'https://deno.land/x/s3_lite_client@0.6.1/mod.ts';
|
||||||
|
|
|
@ -0,0 +1,41 @@
|
||||||
|
import { Comlink } from '@/deps.ts';
|
||||||
|
|
||||||
|
import type { SqliteWorker as _SqliteWorker } from './sqlite.worker.ts';
|
||||||
|
import type { CompiledQuery, QueryResult } from '@/deps.ts';
|
||||||
|
|
||||||
|
class SqliteWorker {
|
||||||
|
#worker: Worker;
|
||||||
|
#client: ReturnType<typeof Comlink.wrap<typeof _SqliteWorker>>;
|
||||||
|
#ready: Promise<void>;
|
||||||
|
|
||||||
|
constructor() {
|
||||||
|
this.#worker = new Worker(new URL('./sqlite.worker.ts', import.meta.url).href, { type: 'module' });
|
||||||
|
this.#client = Comlink.wrap<typeof _SqliteWorker>(this.#worker);
|
||||||
|
|
||||||
|
this.#ready = new Promise<void>((resolve) => {
|
||||||
|
const handleEvent = (event: MessageEvent) => {
|
||||||
|
if (event.data[0] === 'ready') {
|
||||||
|
this.#worker.removeEventListener('message', handleEvent);
|
||||||
|
resolve();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
this.#worker.addEventListener('message', handleEvent);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
async open(path: string): Promise<void> {
|
||||||
|
await this.#ready;
|
||||||
|
return this.#client.open(path);
|
||||||
|
}
|
||||||
|
|
||||||
|
async executeQuery<R>(query: CompiledQuery): Promise<QueryResult<R>> {
|
||||||
|
await this.#ready;
|
||||||
|
return this.#client.executeQuery(query) as Promise<QueryResult<R>>;
|
||||||
|
}
|
||||||
|
|
||||||
|
destroy(): Promise<void> {
|
||||||
|
return this.#client.destroy();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export default SqliteWorker;
|
|
@ -0,0 +1,26 @@
|
||||||
|
/// <reference lib="webworker" />
|
||||||
|
|
||||||
|
import { Comlink, type CompiledQuery, DenoSqlite3, type QueryResult } from '@/deps.ts';
|
||||||
|
|
||||||
|
let db: DenoSqlite3 | undefined;
|
||||||
|
|
||||||
|
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');
|
||||||
|
return {
|
||||||
|
rows: db.prepare(sql).all(...parameters as any[]) as R[],
|
||||||
|
numAffectedRows: BigInt(db.changes),
|
||||||
|
insertId: BigInt(db.lastInsertRowId),
|
||||||
|
};
|
||||||
|
},
|
||||||
|
destroy() {
|
||||||
|
db?.close();
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
Comlink.expose(SqliteWorker);
|
||||||
|
|
||||||
|
self.postMessage(['ready']);
|
Loading…
Reference in New Issue