-
Notifications
You must be signed in to change notification settings - Fork 70
Expand file tree
/
Copy pathWASQLiteConnection.ts
More file actions
420 lines (372 loc) · 12.7 KB
/
WASQLiteConnection.ts
File metadata and controls
420 lines (372 loc) · 12.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
import * as SQLite from '@journeyapps/wa-sqlite';
import { BaseObserver, BatchedUpdateNotification } from '@powersync/common';
import { Mutex } from 'async-mutex';
import { AsyncDatabaseConnection, OnTableChangeCallback, ProxiedQueryResult } from '../AsyncDatabaseConnection';
import { ResolvedWASQLiteOpenFactoryOptions } from './WASQLiteOpenFactory';
/**
* List of currently tested virtual filesystems
*/
export enum WASQLiteVFS {
IDBBatchAtomicVFS = 'IDBBatchAtomicVFS',
OPFSCoopSyncVFS = 'OPFSCoopSyncVFS',
AccessHandlePoolVFS = 'AccessHandlePoolVFS'
}
/**
* @internal
*/
export type WASQLiteBroadCastTableUpdateEvent = {
changedTables: Set<string>;
connectionId: number;
};
/**
* @internal
*/
export type WASQLiteConnectionListener = {
tablesUpdated: (event: BatchedUpdateNotification) => void;
};
/**
* @internal
*/
export type SQLiteModule = Parameters<typeof SQLite.Factory>[0];
/**
* @internal
*/
export type WASQLiteModuleFactoryOptions = { dbFileName: string; encryptionKey?: string };
/**
* @internal
*/
export type WASQLiteModuleFactory = (
options: WASQLiteModuleFactoryOptions
) => Promise<{ module: SQLiteModule; vfs: SQLiteVFS }>;
/**
* @internal
*/
export const AsyncWASQLiteModuleFactory = async () => {
const { default: factory } = await import('@journeyapps/wa-sqlite/dist/wa-sqlite-async.mjs');
return factory();
};
/**
* @internal
*/
export const MultiCipherAsyncWASQLiteModuleFactory = async () => {
const { default: factory } = await import('@journeyapps/wa-sqlite/dist/mc-wa-sqlite-async.mjs');
return factory();
};
/**
* @internal
*/
export const SyncWASQLiteModuleFactory = async () => {
const { default: factory } = await import('@journeyapps/wa-sqlite/dist/wa-sqlite.mjs');
return factory();
};
/**
* @internal
*/
export const MultiCipherSyncWASQLiteModuleFactory = async () => {
const { default: factory } = await import('@journeyapps/wa-sqlite/dist/mc-wa-sqlite.mjs');
return factory();
};
/**
* @internal
*/
export const DEFAULT_MODULE_FACTORIES = {
[WASQLiteVFS.IDBBatchAtomicVFS]: async (options: WASQLiteModuleFactoryOptions) => {
let module;
if (options.encryptionKey) {
module = await MultiCipherAsyncWASQLiteModuleFactory();
} else {
module = await AsyncWASQLiteModuleFactory();
}
const { IDBBatchAtomicVFS } = await import('@journeyapps/wa-sqlite/src/examples/IDBBatchAtomicVFS.js');
return {
module,
// @ts-expect-error The types for this static method are missing upstream
vfs: await IDBBatchAtomicVFS.create(options.dbFileName, module, { lockPolicy: 'exclusive' })
};
},
[WASQLiteVFS.AccessHandlePoolVFS]: async (options: WASQLiteModuleFactoryOptions) => {
let module;
if (options.encryptionKey) {
module = await MultiCipherSyncWASQLiteModuleFactory();
} else {
module = await SyncWASQLiteModuleFactory();
}
// @ts-expect-error The types for this static method are missing upstream
const { AccessHandlePoolVFS } = await import('@journeyapps/wa-sqlite/src/examples/AccessHandlePoolVFS.js');
return {
module,
vfs: await AccessHandlePoolVFS.create(options.dbFileName, module)
};
},
[WASQLiteVFS.OPFSCoopSyncVFS]: async (options: WASQLiteModuleFactoryOptions) => {
let module;
if (options.encryptionKey) {
module = await MultiCipherSyncWASQLiteModuleFactory();
} else {
module = await SyncWASQLiteModuleFactory();
}
// @ts-expect-error The types for this static method are missing upstream
const { OPFSCoopSyncVFS } = await import('@journeyapps/wa-sqlite/src/examples/OPFSCoopSyncVFS.js');
return {
module,
vfs: await OPFSCoopSyncVFS.create(options.dbFileName, module)
};
}
};
/**
* @internal
* WA-SQLite connection which directly interfaces with WA-SQLite.
* This is usually instantiated inside a worker.
*/
export class WASqliteConnection
extends BaseObserver<WASQLiteConnectionListener>
implements AsyncDatabaseConnection<ResolvedWASQLiteOpenFactoryOptions>
{
private _sqliteAPI: SQLiteAPI | null = null;
private _dbP: number | null = null;
private _moduleFactory: WASQLiteModuleFactory;
protected updatedTables: Set<string>;
protected updateTimer: ReturnType<typeof setTimeout> | null;
protected statementMutex: Mutex;
protected broadcastChannel: BroadcastChannel | null;
/**
* Unique id for this specific connection. This is used to prevent broadcast table change
* notification loops.
*/
protected connectionId: number;
constructor(protected options: ResolvedWASQLiteOpenFactoryOptions) {
super();
this.updatedTables = new Set();
this.updateTimer = null;
this.broadcastChannel = null;
this.connectionId = new Date().valueOf() + Math.random();
this.statementMutex = new Mutex();
this._moduleFactory = DEFAULT_MODULE_FACTORIES[this.options.vfs];
}
protected get sqliteAPI() {
if (!this._sqliteAPI) {
throw new Error(`Initialization has not completed`);
}
return this._sqliteAPI;
}
protected get dbP() {
if (!this._dbP) {
throw new Error(`Initialization has not completed`);
}
return this._dbP;
}
protected async openDB() {
this._dbP = await this.sqliteAPI.open_v2(this.options.dbFilename);
return this._dbP;
}
protected async executeEncryptionPragma(): Promise<void> {
if (this.options.encryptionKey) {
await this.executeSingleStatement(`PRAGMA key = "${this.options.encryptionKey}"`);
}
return;
}
protected async openSQLiteAPI(): Promise<SQLiteAPI> {
const { module, vfs } = await this._moduleFactory({
dbFileName: this.options.dbFilename,
encryptionKey: this.options.encryptionKey
});
const sqlite3 = SQLite.Factory(module);
sqlite3.vfs_register(vfs, true);
/**
* Register the PowerSync core SQLite extension
*/
module.ccall('powersync_init_static', 'int', []);
/**
* Create the multiple cipher vfs if an encryption key is provided
*/
if (this.options.encryptionKey) {
const createResult = module.ccall('sqlite3mc_vfs_create', 'int', ['string', 'int'], [this.options.dbFilename, 1]);
if (createResult !== 0) {
throw new Error('Failed to create multiple cipher vfs, Database encryption will not work');
}
}
return sqlite3;
}
protected registerBroadcastListeners() {
this.broadcastChannel = new BroadcastChannel(`${this.options.dbFilename}-table-updates`);
this.broadcastChannel.addEventListener('message', (event) => {
const data: WASQLiteBroadCastTableUpdateEvent = event.data;
if (this.connectionId == data.connectionId) {
// Ignore messages from the same connection
return;
}
// Ensuring that we don't rebroadcast the same message
this.queueTableUpdate(data.changedTables, false);
});
}
protected queueTableUpdate(tableNames: Set<string>, shouldBroadcast = true) {
tableNames.forEach((tableName) => this.updatedTables.add(tableName));
if (this.updateTimer == null) {
this.updateTimer = setTimeout(() => this.fireUpdates(shouldBroadcast), 0);
}
}
async init() {
this._sqliteAPI = await this.openSQLiteAPI();
await this.openDB();
this.registerBroadcastListeners();
await this.executeSingleStatement(`PRAGMA temp_store = ${this.options.temporaryStorage};`);
await this.executeSingleStatement(`PRAGMA cache_size = -${this.options.cacheSizeKb};`);
await this.executeEncryptionPragma();
this.sqliteAPI.update_hook(this.dbP, (updateType: number, dbName: string | null, tableName: string | null) => {
if (!tableName) {
return;
}
const changedTables = new Set([tableName]);
this.queueTableUpdate(changedTables);
});
}
async getConfig(): Promise<ResolvedWASQLiteOpenFactoryOptions> {
return this.options;
}
fireUpdates(shouldBroadcast = true) {
this.updateTimer = null;
const event: BatchedUpdateNotification = { tables: [...this.updatedTables], groupedUpdates: {}, rawUpdates: [] };
// Share to other connections
if (shouldBroadcast) {
this.broadcastChannel!.postMessage({
changedTables: this.updatedTables,
connectionId: this.connectionId
} satisfies WASQLiteBroadCastTableUpdateEvent);
}
this.updatedTables.clear();
this.iterateListeners((cb) => cb.tablesUpdated?.(event));
}
/**
* This executes SQL statements in a batch.
*/
async executeBatch(sql: string, bindings?: any[][]): Promise<ProxiedQueryResult> {
return this.acquireExecuteLock(async (): Promise<ProxiedQueryResult> => {
let affectedRows = 0;
try {
await this.executeSingleStatement('BEGIN TRANSACTION');
const wrappedBindings = bindings ? bindings : [];
for await (const stmt of this.sqliteAPI.statements(this.dbP, sql)) {
if (stmt === null) {
return {
rowsAffected: 0,
rows: { _array: [], length: 0 }
};
}
//Prepare statement once
for (const binding of wrappedBindings) {
// TODO not sure why this is needed currently, but booleans break
for (let i = 0; i < binding.length; i++) {
const b = binding[i];
if (typeof b == 'boolean') {
binding[i] = b ? 1 : 0;
}
}
if (bindings) {
this.sqliteAPI.bind_collection(stmt, binding);
}
const result = await this.sqliteAPI.step(stmt);
if (result === SQLite.SQLITE_DONE) {
//The value returned by sqlite3_changes() immediately after an INSERT, UPDATE or DELETE statement run on a view is always zero.
affectedRows += this.sqliteAPI.changes(this.dbP);
}
this.sqliteAPI.reset(stmt);
}
}
await this.executeSingleStatement('COMMIT');
} catch (err) {
await this.executeSingleStatement('ROLLBACK');
return {
rowsAffected: 0,
rows: { _array: [], length: 0 }
};
}
const result = {
rowsAffected: affectedRows,
rows: { _array: [], length: 0 }
};
return result;
});
}
/**
* This executes single SQL statements inside a requested lock.
*/
async execute(sql: string | TemplateStringsArray, bindings?: any[]): Promise<ProxiedQueryResult> {
// Running multiple statements on the same connection concurrently should not be allowed
return this.acquireExecuteLock(async () => {
return this.executeSingleStatement(sql, bindings);
});
}
async close() {
this.broadcastChannel?.close();
await this.sqliteAPI.close(this.dbP);
}
async registerOnTableChange(callback: OnTableChangeCallback) {
return this.registerListener({
tablesUpdated: (event) => callback(event)
});
}
/**
* This requests a lock for executing statements.
* Should only be used internally.
*/
protected acquireExecuteLock = <T>(callback: () => Promise<T>): Promise<T> => {
return this.statementMutex.runExclusive(callback);
};
/**
* This executes a single statement using SQLite3.
*/
protected async executeSingleStatement(
sql: string | TemplateStringsArray,
bindings?: any[]
): Promise<ProxiedQueryResult> {
const results = [];
for await (const stmt of this.sqliteAPI.statements(this.dbP, sql as string)) {
let columns;
const wrappedBindings = bindings ? [bindings] : [[]];
for (const binding of wrappedBindings) {
// TODO not sure why this is needed currently, but booleans break
binding.forEach((b, index, arr) => {
if (typeof b == 'boolean') {
arr[index] = b ? 1 : 0;
}
});
this.sqliteAPI.reset(stmt);
if (bindings) {
this.sqliteAPI.bind_collection(stmt, binding);
}
const rows = [];
while ((await this.sqliteAPI.step(stmt)) === SQLite.SQLITE_ROW) {
const row = this.sqliteAPI.row(stmt);
rows.push(row);
}
columns = columns ?? this.sqliteAPI.column_names(stmt);
if (columns.length) {
results.push({ columns, rows });
}
}
// When binding parameters, only a single statement is executed.
if (bindings) {
break;
}
}
const rows: Record<string, any>[] = [];
for (const resultSet of results) {
for (const row of resultSet.rows) {
const outRow: Record<string, any> = {};
resultSet.columns.forEach((key, index) => {
outRow[key] = row[index];
});
rows.push(outRow);
}
}
const result = {
insertId: this.sqliteAPI.last_insert_id(this.dbP),
rowsAffected: this.sqliteAPI.changes(this.dbP),
rows: {
_array: rows,
length: rows.length
}
};
return result;
}
}