|
1 | 1 | import * as path from 'node:path'; |
2 | | -import BetterSQLite3Database, { Database } from '@powersync/better-sqlite3'; |
3 | 2 | import * as Comlink from 'comlink'; |
4 | | -import { parentPort, threadId } from 'node:worker_threads'; |
| 3 | +import { parentPort } from 'node:worker_threads'; |
5 | 4 | import OS from 'node:os'; |
6 | 5 | import url from 'node:url'; |
7 | | -import { AsyncDatabase, AsyncDatabaseOpener } from './AsyncDatabase.js'; |
8 | | - |
9 | | -class BlockingAsyncDatabase implements AsyncDatabase { |
10 | | - private readonly db: Database; |
11 | | - |
12 | | - private readonly uncommittedUpdatedTables = new Set<string>(); |
13 | | - private readonly committedUpdatedTables = new Set<string>(); |
14 | | - |
15 | | - constructor(db: Database) { |
16 | | - this.db = db; |
17 | | - |
18 | | - db.function('node_thread_id', () => threadId); |
19 | | - } |
20 | | - |
21 | | - collectCommittedUpdates() { |
22 | | - const resolved = Promise.resolve([...this.committedUpdatedTables]); |
23 | | - this.committedUpdatedTables.clear(); |
24 | | - return resolved; |
25 | | - } |
26 | | - |
27 | | - installUpdateHooks() { |
28 | | - this.db.updateHook((_op: string, _dbName: string, tableName: string, _rowid: bigint) => { |
29 | | - this.uncommittedUpdatedTables.add(tableName); |
30 | | - }); |
31 | | - |
32 | | - this.db.commitHook(() => { |
33 | | - for (const tableName of this.uncommittedUpdatedTables) { |
34 | | - this.committedUpdatedTables.add(tableName); |
35 | | - } |
36 | | - this.uncommittedUpdatedTables.clear(); |
37 | | - return true; |
38 | | - }); |
39 | | - |
40 | | - this.db.rollbackHook(() => { |
41 | | - this.uncommittedUpdatedTables.clear(); |
42 | | - }); |
43 | | - } |
44 | | - |
45 | | - async close() { |
46 | | - this.db.close(); |
47 | | - } |
48 | | - |
49 | | - async execute(query: string, params: any[]) { |
50 | | - const stmt = this.db.prepare(query); |
51 | | - if (stmt.reader) { |
52 | | - const rows = stmt.all(params); |
53 | | - return { |
54 | | - rowsAffected: 0, |
55 | | - rows: { |
56 | | - _array: rows, |
57 | | - length: rows.length |
58 | | - } |
59 | | - }; |
60 | | - } else { |
61 | | - const info = stmt.run(params); |
62 | | - return { |
63 | | - rowsAffected: info.changes, |
64 | | - insertId: Number(info.lastInsertRowid) |
65 | | - }; |
66 | | - } |
67 | | - } |
68 | | - |
69 | | - async executeRaw(query: string, params: any[]) { |
70 | | - const stmt = this.db.prepare(query); |
71 | | - |
72 | | - if (stmt.reader) { |
73 | | - return stmt.raw().all(params); |
74 | | - } else { |
75 | | - stmt.raw().run(params); |
76 | | - return []; |
77 | | - } |
78 | | - } |
79 | | - |
80 | | - async executeBatch(query: string, params: any[][]) { |
81 | | - params = params ?? []; |
82 | | - |
83 | | - let rowsAffected = 0; |
84 | | - |
85 | | - const stmt = this.db.prepare(query); |
86 | | - for (const paramSet of params) { |
87 | | - const info = stmt.run(paramSet); |
88 | | - rowsAffected += info.changes; |
89 | | - } |
90 | | - |
91 | | - return { rowsAffected }; |
92 | | - } |
93 | | -} |
94 | | - |
95 | | -class BetterSqliteWorker implements AsyncDatabaseOpener { |
96 | | - options: PowerSyncWorkerOptions; |
97 | | - |
98 | | - constructor(options: PowerSyncWorkerOptions) { |
99 | | - this.options = options; |
100 | | - } |
101 | | - |
102 | | - async open(path: string, isWriter: boolean): Promise<AsyncDatabase> { |
103 | | - const baseDB = new BetterSQLite3Database(path); |
104 | | - baseDB.pragma('journal_mode = WAL'); |
105 | | - baseDB.loadExtension(this.options.extensionPath(), 'sqlite3_powersync_init'); |
106 | | - if (!isWriter) { |
107 | | - baseDB.pragma('query_only = true'); |
108 | | - } |
109 | | - |
110 | | - const asyncDb = new BlockingAsyncDatabase(baseDB); |
111 | | - asyncDb.installUpdateHooks(); |
112 | | - |
113 | | - return Comlink.proxy(asyncDb); |
114 | | - } |
115 | | -} |
| 6 | +import { openDatabase as openBetterSqliteDatabase } from './BetterSqliteWorker.js'; |
| 7 | +import { openDatabase as openNodeDatabase } from './NodeSqliteWorker.js'; |
| 8 | +import { AsyncDatabase, AsyncDatabaseOpener, AsyncDatabaseOpenOptions } from './AsyncDatabase.js'; |
116 | 9 |
|
117 | 10 | export interface PowerSyncWorkerOptions { |
118 | 11 | /** |
@@ -152,5 +45,30 @@ export function startPowerSyncWorker(options?: Partial<PowerSyncWorkerOptions>) |
152 | 45 | ...options |
153 | 46 | }; |
154 | 47 |
|
155 | | - Comlink.expose(new BetterSqliteWorker(resolvedOptions), parentPort! as Comlink.Endpoint); |
| 48 | + Comlink.expose(new DatabaseOpenHelper(resolvedOptions), parentPort! as Comlink.Endpoint); |
| 49 | +} |
| 50 | + |
| 51 | +class DatabaseOpenHelper implements AsyncDatabaseOpener { |
| 52 | + private options: PowerSyncWorkerOptions; |
| 53 | + |
| 54 | + constructor(options: PowerSyncWorkerOptions) { |
| 55 | + this.options = options; |
| 56 | + } |
| 57 | + |
| 58 | + async open(options: AsyncDatabaseOpenOptions): Promise<AsyncDatabase> { |
| 59 | + let database: AsyncDatabase; |
| 60 | + |
| 61 | + switch (options.implementation) { |
| 62 | + case 'better-sqlite3': |
| 63 | + database = await openBetterSqliteDatabase(this.options, options); |
| 64 | + break; |
| 65 | + case 'node': |
| 66 | + database = await openNodeDatabase(this.options, options); |
| 67 | + break; |
| 68 | + default: |
| 69 | + throw new Error(`Unknown database implementation: ${options.implementation}.`); |
| 70 | + } |
| 71 | + |
| 72 | + return Comlink.proxy(database); |
| 73 | + } |
156 | 74 | } |
0 commit comments