@powersync/web 0.0.0-dev-20260202162549 → 0.0.0-dev-20260216124709
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/0b19af1befc07ce338dd.wasm +0 -0
- package/dist/2632c3bda9473da74fd5.wasm +0 -0
- package/dist/64f5351ba3784bfe2f3e.wasm +0 -0
- package/dist/9318ca94aac4d0fe0135.wasm +0 -0
- package/dist/_journeyapps_wa-sqlite-_journeyapps_wa-sqlite_src_examples_AccessHandlePoolVFS_js-_journeyapp-89f0ba.index.umd.js +1878 -0
- package/dist/_journeyapps_wa-sqlite-_journeyapps_wa-sqlite_src_examples_AccessHandlePoolVFS_js-_journeyapp-89f0ba.index.umd.js.map +1 -0
- package/dist/_journeyapps_wa-sqlite_src_examples_AccessHandlePoolVFS_js-_journeyapps_wa-sqlite_src_example-2530150.index.umd.js +555 -0
- package/dist/_journeyapps_wa-sqlite_src_examples_AccessHandlePoolVFS_js-_journeyapps_wa-sqlite_src_example-2530150.index.umd.js.map +1 -0
- package/dist/_journeyapps_wa-sqlite_src_examples_AccessHandlePoolVFS_js-_journeyapps_wa-sqlite_src_example-2530151.index.umd.js +555 -0
- package/dist/_journeyapps_wa-sqlite_src_examples_AccessHandlePoolVFS_js-_journeyapps_wa-sqlite_src_example-2530151.index.umd.js.map +1 -0
- package/dist/index.umd.js +8267 -0
- package/dist/index.umd.js.map +1 -0
- package/dist/worker/SharedSyncImplementation.umd.js +19059 -0
- package/dist/worker/SharedSyncImplementation.umd.js.map +1 -0
- package/dist/worker/WASQLiteDB.umd.js +17736 -0
- package/dist/worker/WASQLiteDB.umd.js.map +1 -0
- package/dist/worker/node_modules_pnpm_bson_6_10_4_node_modules_bson_lib_bson_mjs.umd.js +4646 -0
- package/dist/worker/node_modules_pnpm_bson_6_10_4_node_modules_bson_lib_bson_mjs.umd.js.map +1 -0
- package/dist/worker/node_modules_pnpm_journeyapps_wa-sqlite_1_4_1_node_modules_journeyapps_wa-sqlite_dist_mc-wa-s-3a94cf.umd.js +44 -0
- package/dist/worker/node_modules_pnpm_journeyapps_wa-sqlite_1_4_1_node_modules_journeyapps_wa-sqlite_dist_mc-wa-s-3a94cf.umd.js.map +1 -0
- package/dist/worker/node_modules_pnpm_journeyapps_wa-sqlite_1_4_1_node_modules_journeyapps_wa-sqlite_dist_mc-wa-s-868779.umd.js +44 -0
- package/dist/worker/node_modules_pnpm_journeyapps_wa-sqlite_1_4_1_node_modules_journeyapps_wa-sqlite_dist_mc-wa-s-868779.umd.js.map +1 -0
- package/dist/worker/node_modules_pnpm_journeyapps_wa-sqlite_1_4_1_node_modules_journeyapps_wa-sqlite_dist_wa-sqli-f60d0d.umd.js +44 -0
- package/dist/worker/node_modules_pnpm_journeyapps_wa-sqlite_1_4_1_node_modules_journeyapps_wa-sqlite_dist_wa-sqli-f60d0d.umd.js.map +1 -0
- package/dist/worker/node_modules_pnpm_journeyapps_wa-sqlite_1_4_1_node_modules_journeyapps_wa-sqlite_dist_wa-sqlite_mjs.umd.js +44 -0
- package/dist/worker/node_modules_pnpm_journeyapps_wa-sqlite_1_4_1_node_modules_journeyapps_wa-sqlite_dist_wa-sqlite_mjs.umd.js.map +1 -0
- package/dist/worker/node_modules_pnpm_journeyapps_wa-sqlite_1_4_1_node_modules_journeyapps_wa-sqlite_src_examples-0d2437.umd.js +2478 -0
- package/dist/worker/node_modules_pnpm_journeyapps_wa-sqlite_1_4_1_node_modules_journeyapps_wa-sqlite_src_examples-0d2437.umd.js.map +1 -0
- package/dist/worker/node_modules_pnpm_journeyapps_wa-sqlite_1_4_1_node_modules_journeyapps_wa-sqlite_src_examples-1d4e74.umd.js +1820 -0
- package/dist/worker/node_modules_pnpm_journeyapps_wa-sqlite_1_4_1_node_modules_journeyapps_wa-sqlite_src_examples-1d4e74.umd.js.map +1 -0
- package/dist/worker/node_modules_pnpm_journeyapps_wa-sqlite_1_4_1_node_modules_journeyapps_wa-sqlite_src_examples-3622cf.umd.js +1681 -0
- package/dist/worker/node_modules_pnpm_journeyapps_wa-sqlite_1_4_1_node_modules_journeyapps_wa-sqlite_src_examples-3622cf.umd.js.map +1 -0
- package/lib/package.json +95 -0
- package/lib/src/attachments/IndexDBFileSystemAdapter.d.ts +25 -0
- package/lib/src/attachments/IndexDBFileSystemAdapter.js +104 -0
- package/lib/src/db/NavigatorTriggerClaimManager.d.ts +6 -0
- package/lib/src/db/NavigatorTriggerClaimManager.js +20 -0
- package/lib/src/db/PowerSyncDatabase.d.ts +79 -0
- package/lib/src/db/PowerSyncDatabase.js +166 -0
- package/lib/src/db/adapters/AbstractWebPowerSyncDatabaseOpenFactory.d.ts +23 -0
- package/lib/src/db/adapters/AbstractWebPowerSyncDatabaseOpenFactory.js +26 -0
- package/lib/src/db/adapters/AbstractWebSQLOpenFactory.d.ts +17 -0
- package/lib/src/db/adapters/AbstractWebSQLOpenFactory.js +33 -0
- package/lib/src/db/adapters/AsyncDatabaseConnection.d.ts +49 -0
- package/lib/src/db/adapters/AsyncDatabaseConnection.js +1 -0
- package/lib/src/db/adapters/LockedAsyncDatabaseAdapter.d.ts +109 -0
- package/lib/src/db/adapters/LockedAsyncDatabaseAdapter.js +401 -0
- package/lib/src/db/adapters/SSRDBAdapter.d.ts +31 -0
- package/lib/src/db/adapters/SSRDBAdapter.js +69 -0
- package/lib/src/db/adapters/WebDBAdapter.d.ts +20 -0
- package/lib/src/db/adapters/WebDBAdapter.js +1 -0
- package/lib/src/db/adapters/WorkerWrappedAsyncDatabaseConnection.d.ts +59 -0
- package/lib/src/db/adapters/WorkerWrappedAsyncDatabaseConnection.js +147 -0
- package/lib/src/db/adapters/wa-sqlite/InternalWASQLiteDBAdapter.d.ts +12 -0
- package/lib/src/db/adapters/wa-sqlite/InternalWASQLiteDBAdapter.js +19 -0
- package/lib/src/db/adapters/wa-sqlite/WASQLiteConnection.d.ts +155 -0
- package/lib/src/db/adapters/wa-sqlite/WASQLiteConnection.js +401 -0
- package/lib/src/db/adapters/wa-sqlite/WASQLiteDBAdapter.d.ts +32 -0
- package/lib/src/db/adapters/wa-sqlite/WASQLiteDBAdapter.js +49 -0
- package/lib/src/db/adapters/wa-sqlite/WASQLiteOpenFactory.d.ts +23 -0
- package/lib/src/db/adapters/wa-sqlite/WASQLiteOpenFactory.js +96 -0
- package/lib/src/db/adapters/wa-sqlite/WASQLitePowerSyncDatabaseOpenFactory.d.ts +15 -0
- package/lib/src/db/adapters/wa-sqlite/WASQLitePowerSyncDatabaseOpenFactory.js +21 -0
- package/lib/src/db/adapters/web-sql-flags.d.ts +87 -0
- package/lib/src/db/adapters/web-sql-flags.js +36 -0
- package/lib/src/db/sync/SSRWebStreamingSyncImplementation.d.ts +48 -0
- package/lib/src/db/sync/SSRWebStreamingSyncImplementation.js +65 -0
- package/lib/src/db/sync/SharedWebStreamingSyncImplementation.d.ts +57 -0
- package/lib/src/db/sync/SharedWebStreamingSyncImplementation.js +237 -0
- package/lib/src/db/sync/WebRemote.d.ts +9 -0
- package/lib/src/db/sync/WebRemote.js +44 -0
- package/lib/src/db/sync/WebStreamingSyncImplementation.d.ts +13 -0
- package/lib/src/db/sync/WebStreamingSyncImplementation.js +18 -0
- package/lib/src/db/sync/userAgent.d.ts +17 -0
- package/lib/src/db/sync/userAgent.js +64 -0
- package/lib/src/index.d.ts +15 -0
- package/lib/src/index.js +15 -0
- package/lib/src/shared/navigator.d.ts +1 -0
- package/lib/src/shared/navigator.js +6 -0
- package/lib/src/worker/db/SharedWASQLiteConnection.d.ts +42 -0
- package/lib/src/worker/db/SharedWASQLiteConnection.js +90 -0
- package/lib/src/worker/db/WASQLiteDB.worker.d.ts +4 -0
- package/lib/src/worker/db/WASQLiteDB.worker.js +69 -0
- package/lib/src/worker/db/WorkerWASQLiteConnection.d.ts +9 -0
- package/lib/src/worker/db/WorkerWASQLiteConnection.js +12 -0
- package/lib/src/worker/db/open-worker-database.d.ts +14 -0
- package/lib/src/worker/db/open-worker-database.js +52 -0
- package/lib/src/worker/sync/AbstractSharedSyncClientProvider.d.ts +19 -0
- package/lib/src/worker/sync/AbstractSharedSyncClientProvider.js +5 -0
- package/lib/src/worker/sync/BroadcastLogger.d.ts +47 -0
- package/lib/src/worker/sync/BroadcastLogger.js +128 -0
- package/lib/src/worker/sync/SharedSyncImplementation.d.ts +137 -0
- package/lib/src/worker/sync/SharedSyncImplementation.js +495 -0
- package/lib/src/worker/sync/SharedSyncImplementation.worker.d.ts +1 -0
- package/lib/src/worker/sync/SharedSyncImplementation.worker.js +11 -0
- package/lib/src/worker/sync/WorkerClient.d.ts +31 -0
- package/lib/src/worker/sync/WorkerClient.js +84 -0
- package/lib/tsconfig.tsbuildinfo +1 -0
- package/package.json +3 -3
- package/src/index.ts +1 -0
|
@@ -0,0 +1,401 @@
|
|
|
1
|
+
import * as SQLite from '@journeyapps/wa-sqlite';
|
|
2
|
+
import { BaseObserver } from '@powersync/common';
|
|
3
|
+
import { Mutex } from 'async-mutex';
|
|
4
|
+
/**
|
|
5
|
+
* List of currently tested virtual filesystems
|
|
6
|
+
*/
|
|
7
|
+
export var WASQLiteVFS;
|
|
8
|
+
(function (WASQLiteVFS) {
|
|
9
|
+
WASQLiteVFS["IDBBatchAtomicVFS"] = "IDBBatchAtomicVFS";
|
|
10
|
+
WASQLiteVFS["OPFSCoopSyncVFS"] = "OPFSCoopSyncVFS";
|
|
11
|
+
WASQLiteVFS["AccessHandlePoolVFS"] = "AccessHandlePoolVFS";
|
|
12
|
+
})(WASQLiteVFS || (WASQLiteVFS = {}));
|
|
13
|
+
/**
|
|
14
|
+
* @internal
|
|
15
|
+
*/
|
|
16
|
+
export const AsyncWASQLiteModuleFactory = async () => {
|
|
17
|
+
const { default: factory } = await import('@journeyapps/wa-sqlite/dist/wa-sqlite-async.mjs');
|
|
18
|
+
return factory();
|
|
19
|
+
};
|
|
20
|
+
/**
|
|
21
|
+
* @internal
|
|
22
|
+
*/
|
|
23
|
+
export const MultiCipherAsyncWASQLiteModuleFactory = async () => {
|
|
24
|
+
const { default: factory } = await import('@journeyapps/wa-sqlite/dist/mc-wa-sqlite-async.mjs');
|
|
25
|
+
return factory();
|
|
26
|
+
};
|
|
27
|
+
/**
|
|
28
|
+
* @internal
|
|
29
|
+
*/
|
|
30
|
+
export const SyncWASQLiteModuleFactory = async () => {
|
|
31
|
+
const { default: factory } = await import('@journeyapps/wa-sqlite/dist/wa-sqlite.mjs');
|
|
32
|
+
return factory();
|
|
33
|
+
};
|
|
34
|
+
/**
|
|
35
|
+
* @internal
|
|
36
|
+
*/
|
|
37
|
+
export const MultiCipherSyncWASQLiteModuleFactory = async () => {
|
|
38
|
+
const { default: factory } = await import('@journeyapps/wa-sqlite/dist/mc-wa-sqlite.mjs');
|
|
39
|
+
return factory();
|
|
40
|
+
};
|
|
41
|
+
/**
|
|
42
|
+
* @internal
|
|
43
|
+
*/
|
|
44
|
+
export const DEFAULT_MODULE_FACTORIES = {
|
|
45
|
+
[WASQLiteVFS.IDBBatchAtomicVFS]: async (options) => {
|
|
46
|
+
let module;
|
|
47
|
+
if (options.encryptionKey) {
|
|
48
|
+
module = await MultiCipherAsyncWASQLiteModuleFactory();
|
|
49
|
+
}
|
|
50
|
+
else {
|
|
51
|
+
module = await AsyncWASQLiteModuleFactory();
|
|
52
|
+
}
|
|
53
|
+
const { IDBBatchAtomicVFS } = await import('@journeyapps/wa-sqlite/src/examples/IDBBatchAtomicVFS.js');
|
|
54
|
+
return {
|
|
55
|
+
module,
|
|
56
|
+
// @ts-expect-error The types for this static method are missing upstream
|
|
57
|
+
vfs: await IDBBatchAtomicVFS.create(options.dbFileName, module, { lockPolicy: 'exclusive' })
|
|
58
|
+
};
|
|
59
|
+
},
|
|
60
|
+
[WASQLiteVFS.AccessHandlePoolVFS]: async (options) => {
|
|
61
|
+
let module;
|
|
62
|
+
if (options.encryptionKey) {
|
|
63
|
+
module = await MultiCipherSyncWASQLiteModuleFactory();
|
|
64
|
+
}
|
|
65
|
+
else {
|
|
66
|
+
module = await SyncWASQLiteModuleFactory();
|
|
67
|
+
}
|
|
68
|
+
// @ts-expect-error The types for this static method are missing upstream
|
|
69
|
+
const { AccessHandlePoolVFS } = await import('@journeyapps/wa-sqlite/src/examples/AccessHandlePoolVFS.js');
|
|
70
|
+
return {
|
|
71
|
+
module,
|
|
72
|
+
vfs: await AccessHandlePoolVFS.create(options.dbFileName, module)
|
|
73
|
+
};
|
|
74
|
+
},
|
|
75
|
+
[WASQLiteVFS.OPFSCoopSyncVFS]: async (options) => {
|
|
76
|
+
let module;
|
|
77
|
+
if (options.encryptionKey) {
|
|
78
|
+
module = await MultiCipherSyncWASQLiteModuleFactory();
|
|
79
|
+
}
|
|
80
|
+
else {
|
|
81
|
+
module = await SyncWASQLiteModuleFactory();
|
|
82
|
+
}
|
|
83
|
+
// @ts-expect-error The types for this static method are missing upstream
|
|
84
|
+
const { OPFSCoopSyncVFS } = await import('@journeyapps/wa-sqlite/src/examples/OPFSCoopSyncVFS.js');
|
|
85
|
+
const vfs = await OPFSCoopSyncVFS.create(options.dbFileName, module);
|
|
86
|
+
return {
|
|
87
|
+
module,
|
|
88
|
+
vfs
|
|
89
|
+
};
|
|
90
|
+
}
|
|
91
|
+
};
|
|
92
|
+
/**
|
|
93
|
+
* @internal
|
|
94
|
+
* WA-SQLite connection which directly interfaces with WA-SQLite.
|
|
95
|
+
* This is usually instantiated inside a worker.
|
|
96
|
+
*/
|
|
97
|
+
export class WASqliteConnection extends BaseObserver {
|
|
98
|
+
options;
|
|
99
|
+
_sqliteAPI = null;
|
|
100
|
+
_dbP = null;
|
|
101
|
+
_moduleFactory;
|
|
102
|
+
updatedTables;
|
|
103
|
+
updateTimer;
|
|
104
|
+
statementMutex;
|
|
105
|
+
broadcastChannel;
|
|
106
|
+
/**
|
|
107
|
+
* Unique id for this specific connection. This is used to prevent broadcast table change
|
|
108
|
+
* notification loops.
|
|
109
|
+
*/
|
|
110
|
+
connectionId;
|
|
111
|
+
_holdCounter;
|
|
112
|
+
_holdId;
|
|
113
|
+
constructor(options) {
|
|
114
|
+
super();
|
|
115
|
+
this.options = options;
|
|
116
|
+
this.updatedTables = new Set();
|
|
117
|
+
this.updateTimer = null;
|
|
118
|
+
this.broadcastChannel = null;
|
|
119
|
+
this.connectionId = new Date().valueOf() + Math.random();
|
|
120
|
+
this.statementMutex = new Mutex();
|
|
121
|
+
this._moduleFactory = DEFAULT_MODULE_FACTORIES[this.options.vfs];
|
|
122
|
+
this._holdCounter = 0;
|
|
123
|
+
this._holdId = null;
|
|
124
|
+
}
|
|
125
|
+
/**
|
|
126
|
+
* Gets the id for the current hold.
|
|
127
|
+
* This can be used to check for invalid states.
|
|
128
|
+
*/
|
|
129
|
+
get currentHoldId() {
|
|
130
|
+
return this._holdId;
|
|
131
|
+
}
|
|
132
|
+
get sqliteAPI() {
|
|
133
|
+
if (!this._sqliteAPI) {
|
|
134
|
+
throw new Error(`Initialization has not completed`);
|
|
135
|
+
}
|
|
136
|
+
return this._sqliteAPI;
|
|
137
|
+
}
|
|
138
|
+
get dbP() {
|
|
139
|
+
if (!this._dbP) {
|
|
140
|
+
throw new Error(`Initialization has not completed`);
|
|
141
|
+
}
|
|
142
|
+
return this._dbP;
|
|
143
|
+
}
|
|
144
|
+
/**
|
|
145
|
+
* Checks if the database connection is in autocommit mode.
|
|
146
|
+
* @returns true if in autocommit mode, false if in a transaction
|
|
147
|
+
*/
|
|
148
|
+
async isAutoCommit() {
|
|
149
|
+
return this.sqliteAPI.get_autocommit(this.dbP) != 0;
|
|
150
|
+
}
|
|
151
|
+
async markHold() {
|
|
152
|
+
const previousHoldId = this._holdId;
|
|
153
|
+
this._holdId = `${++this._holdCounter}`;
|
|
154
|
+
if (previousHoldId) {
|
|
155
|
+
await this.iterateAsyncListeners(async (cb) => cb.holdOverwritten?.(previousHoldId));
|
|
156
|
+
}
|
|
157
|
+
return this._holdId;
|
|
158
|
+
}
|
|
159
|
+
async releaseHold(holdId) {
|
|
160
|
+
if (holdId != this._holdId) {
|
|
161
|
+
throw new Error(`Invalid hold state, expected ${this._holdId} but got ${holdId}`);
|
|
162
|
+
}
|
|
163
|
+
this._holdId = null;
|
|
164
|
+
}
|
|
165
|
+
async openDB() {
|
|
166
|
+
this._dbP = await this.sqliteAPI.open_v2(this.options.dbFilename);
|
|
167
|
+
return this._dbP;
|
|
168
|
+
}
|
|
169
|
+
async executeEncryptionPragma() {
|
|
170
|
+
if (this.options.encryptionKey) {
|
|
171
|
+
await this.executeSingleStatement(`PRAGMA key = "${this.options.encryptionKey}"`);
|
|
172
|
+
}
|
|
173
|
+
return;
|
|
174
|
+
}
|
|
175
|
+
async openSQLiteAPI() {
|
|
176
|
+
const { module, vfs } = await this._moduleFactory({
|
|
177
|
+
dbFileName: this.options.dbFilename,
|
|
178
|
+
encryptionKey: this.options.encryptionKey
|
|
179
|
+
});
|
|
180
|
+
const sqlite3 = SQLite.Factory(module);
|
|
181
|
+
sqlite3.vfs_register(vfs, true);
|
|
182
|
+
/**
|
|
183
|
+
* Register the PowerSync core SQLite extension
|
|
184
|
+
*/
|
|
185
|
+
module.ccall('powersync_init_static', 'int', []);
|
|
186
|
+
/**
|
|
187
|
+
* Create the multiple cipher vfs if an encryption key is provided
|
|
188
|
+
*/
|
|
189
|
+
if (this.options.encryptionKey) {
|
|
190
|
+
const createResult = module.ccall('sqlite3mc_vfs_create', 'int', ['string', 'int'], [this.options.dbFilename, 1]);
|
|
191
|
+
if (createResult !== 0) {
|
|
192
|
+
throw new Error('Failed to create multiple cipher vfs, Database encryption will not work');
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
return sqlite3;
|
|
196
|
+
}
|
|
197
|
+
registerBroadcastListeners() {
|
|
198
|
+
this.broadcastChannel = new BroadcastChannel(`${this.options.dbFilename}-table-updates`);
|
|
199
|
+
this.broadcastChannel.addEventListener('message', (event) => {
|
|
200
|
+
const data = event.data;
|
|
201
|
+
if (this.connectionId == data.connectionId) {
|
|
202
|
+
// Ignore messages from the same connection
|
|
203
|
+
return;
|
|
204
|
+
}
|
|
205
|
+
// Ensuring that we don't rebroadcast the same message
|
|
206
|
+
this.queueTableUpdate(data.changedTables, false);
|
|
207
|
+
});
|
|
208
|
+
}
|
|
209
|
+
queueTableUpdate(tableNames, shouldBroadcast = true) {
|
|
210
|
+
tableNames.forEach((tableName) => this.updatedTables.add(tableName));
|
|
211
|
+
if (this.updateTimer == null) {
|
|
212
|
+
this.updateTimer = setTimeout(() => this.fireUpdates(shouldBroadcast), 0);
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
async init() {
|
|
216
|
+
this._sqliteAPI = await this.openSQLiteAPI();
|
|
217
|
+
await this.openDB();
|
|
218
|
+
this.registerBroadcastListeners();
|
|
219
|
+
await this.executeSingleStatement(`PRAGMA temp_store = ${this.options.temporaryStorage};`);
|
|
220
|
+
await this.executeEncryptionPragma();
|
|
221
|
+
await this.executeSingleStatement(`PRAGMA cache_size = -${this.options.cacheSizeKb};`);
|
|
222
|
+
this.sqliteAPI.update_hook(this.dbP, (updateType, dbName, tableName) => {
|
|
223
|
+
if (!tableName) {
|
|
224
|
+
return;
|
|
225
|
+
}
|
|
226
|
+
const changedTables = new Set([tableName]);
|
|
227
|
+
this.queueTableUpdate(changedTables);
|
|
228
|
+
});
|
|
229
|
+
}
|
|
230
|
+
async getConfig() {
|
|
231
|
+
return this.options;
|
|
232
|
+
}
|
|
233
|
+
fireUpdates(shouldBroadcast = true) {
|
|
234
|
+
this.updateTimer = null;
|
|
235
|
+
const event = { tables: [...this.updatedTables], groupedUpdates: {}, rawUpdates: [] };
|
|
236
|
+
// Share to other connections
|
|
237
|
+
if (shouldBroadcast) {
|
|
238
|
+
this.broadcastChannel.postMessage({
|
|
239
|
+
changedTables: this.updatedTables,
|
|
240
|
+
connectionId: this.connectionId
|
|
241
|
+
});
|
|
242
|
+
}
|
|
243
|
+
this.updatedTables.clear();
|
|
244
|
+
this.iterateListeners((cb) => cb.tablesUpdated?.(event));
|
|
245
|
+
}
|
|
246
|
+
/**
|
|
247
|
+
* This executes SQL statements in a batch.
|
|
248
|
+
*/
|
|
249
|
+
async executeBatch(sql, bindings) {
|
|
250
|
+
return this.acquireExecuteLock(async () => {
|
|
251
|
+
let affectedRows = 0;
|
|
252
|
+
try {
|
|
253
|
+
await this.executeSingleStatement('BEGIN TRANSACTION');
|
|
254
|
+
const wrappedBindings = bindings ? bindings : [];
|
|
255
|
+
for await (const stmt of this.sqliteAPI.statements(this.dbP, sql)) {
|
|
256
|
+
if (stmt === null) {
|
|
257
|
+
return {
|
|
258
|
+
rowsAffected: 0,
|
|
259
|
+
rows: { _array: [], length: 0 }
|
|
260
|
+
};
|
|
261
|
+
}
|
|
262
|
+
//Prepare statement once
|
|
263
|
+
for (const binding of wrappedBindings) {
|
|
264
|
+
// TODO not sure why this is needed currently, but booleans break
|
|
265
|
+
for (let i = 0; i < binding.length; i++) {
|
|
266
|
+
const b = binding[i];
|
|
267
|
+
if (typeof b == 'boolean') {
|
|
268
|
+
binding[i] = b ? 1 : 0;
|
|
269
|
+
}
|
|
270
|
+
}
|
|
271
|
+
if (bindings) {
|
|
272
|
+
this.sqliteAPI.bind_collection(stmt, binding);
|
|
273
|
+
}
|
|
274
|
+
const result = await this.sqliteAPI.step(stmt);
|
|
275
|
+
if (result === SQLite.SQLITE_DONE) {
|
|
276
|
+
//The value returned by sqlite3_changes() immediately after an INSERT, UPDATE or DELETE statement run on a view is always zero.
|
|
277
|
+
affectedRows += this.sqliteAPI.changes(this.dbP);
|
|
278
|
+
}
|
|
279
|
+
this.sqliteAPI.reset(stmt);
|
|
280
|
+
}
|
|
281
|
+
}
|
|
282
|
+
await this.executeSingleStatement('COMMIT');
|
|
283
|
+
}
|
|
284
|
+
catch (err) {
|
|
285
|
+
await this.executeSingleStatement('ROLLBACK');
|
|
286
|
+
return {
|
|
287
|
+
rowsAffected: 0,
|
|
288
|
+
rows: { _array: [], length: 0 }
|
|
289
|
+
};
|
|
290
|
+
}
|
|
291
|
+
const result = {
|
|
292
|
+
rowsAffected: affectedRows,
|
|
293
|
+
rows: { _array: [], length: 0 }
|
|
294
|
+
};
|
|
295
|
+
return result;
|
|
296
|
+
});
|
|
297
|
+
}
|
|
298
|
+
/**
|
|
299
|
+
* This executes single SQL statements inside a requested lock.
|
|
300
|
+
*/
|
|
301
|
+
async execute(sql, bindings) {
|
|
302
|
+
// Running multiple statements on the same connection concurrently should not be allowed
|
|
303
|
+
return this.acquireExecuteLock(async () => {
|
|
304
|
+
return this.executeSingleStatement(sql, bindings);
|
|
305
|
+
});
|
|
306
|
+
}
|
|
307
|
+
async executeRaw(sql, bindings) {
|
|
308
|
+
return this.acquireExecuteLock(async () => {
|
|
309
|
+
return this.executeSingleStatementRaw(sql, bindings);
|
|
310
|
+
});
|
|
311
|
+
}
|
|
312
|
+
async close() {
|
|
313
|
+
this.broadcastChannel?.close();
|
|
314
|
+
await this.acquireExecuteLock(async () => {
|
|
315
|
+
/**
|
|
316
|
+
* Running the close operation inside the same execute mutex prevents errors like:
|
|
317
|
+
* ```
|
|
318
|
+
* unable to close due to unfinalized statements or unfinished backups
|
|
319
|
+
* ```
|
|
320
|
+
*/
|
|
321
|
+
await this.sqliteAPI.close(this.dbP);
|
|
322
|
+
});
|
|
323
|
+
}
|
|
324
|
+
async registerOnTableChange(callback) {
|
|
325
|
+
return this.registerListener({
|
|
326
|
+
tablesUpdated: (event) => callback(event)
|
|
327
|
+
});
|
|
328
|
+
}
|
|
329
|
+
/**
|
|
330
|
+
* This requests a lock for executing statements.
|
|
331
|
+
* Should only be used internally.
|
|
332
|
+
*/
|
|
333
|
+
acquireExecuteLock = (callback) => {
|
|
334
|
+
return this.statementMutex.runExclusive(callback);
|
|
335
|
+
};
|
|
336
|
+
/**
|
|
337
|
+
* This executes a single statement using SQLite3.
|
|
338
|
+
*/
|
|
339
|
+
async executeSingleStatement(sql, bindings) {
|
|
340
|
+
const results = await this._execute(sql, bindings);
|
|
341
|
+
const rows = [];
|
|
342
|
+
for (const resultSet of results) {
|
|
343
|
+
for (const row of resultSet.rows) {
|
|
344
|
+
const outRow = {};
|
|
345
|
+
resultSet.columns.forEach((key, index) => {
|
|
346
|
+
outRow[key] = row[index];
|
|
347
|
+
});
|
|
348
|
+
rows.push(outRow);
|
|
349
|
+
}
|
|
350
|
+
}
|
|
351
|
+
const result = {
|
|
352
|
+
insertId: this.sqliteAPI.last_insert_id(this.dbP),
|
|
353
|
+
rowsAffected: this.sqliteAPI.changes(this.dbP),
|
|
354
|
+
rows: {
|
|
355
|
+
_array: rows,
|
|
356
|
+
length: rows.length
|
|
357
|
+
}
|
|
358
|
+
};
|
|
359
|
+
return result;
|
|
360
|
+
}
|
|
361
|
+
/**
|
|
362
|
+
* This executes a single statement using SQLite3 and returns the results as an array of arrays.
|
|
363
|
+
*/
|
|
364
|
+
async executeSingleStatementRaw(sql, bindings) {
|
|
365
|
+
const results = await this._execute(sql, bindings);
|
|
366
|
+
return results.flatMap((resultset) => resultset.rows.map((row) => resultset.columns.map((_, index) => row[index])));
|
|
367
|
+
}
|
|
368
|
+
async _execute(sql, bindings) {
|
|
369
|
+
const results = [];
|
|
370
|
+
for await (const stmt of this.sqliteAPI.statements(this.dbP, sql)) {
|
|
371
|
+
let columns;
|
|
372
|
+
const wrappedBindings = bindings ? [bindings] : [[]];
|
|
373
|
+
for (const binding of wrappedBindings) {
|
|
374
|
+
// TODO not sure why this is needed currently, but booleans break
|
|
375
|
+
binding.forEach((b, index, arr) => {
|
|
376
|
+
if (typeof b == 'boolean') {
|
|
377
|
+
arr[index] = b ? 1 : 0;
|
|
378
|
+
}
|
|
379
|
+
});
|
|
380
|
+
this.sqliteAPI.reset(stmt);
|
|
381
|
+
if (bindings) {
|
|
382
|
+
this.sqliteAPI.bind_collection(stmt, binding);
|
|
383
|
+
}
|
|
384
|
+
const rows = [];
|
|
385
|
+
while ((await this.sqliteAPI.step(stmt)) === SQLite.SQLITE_ROW) {
|
|
386
|
+
const row = this.sqliteAPI.row(stmt);
|
|
387
|
+
rows.push(row);
|
|
388
|
+
}
|
|
389
|
+
columns = columns ?? this.sqliteAPI.column_names(stmt);
|
|
390
|
+
if (columns.length) {
|
|
391
|
+
results.push({ columns, rows });
|
|
392
|
+
}
|
|
393
|
+
}
|
|
394
|
+
// When binding parameters, only a single statement is executed.
|
|
395
|
+
if (bindings) {
|
|
396
|
+
break;
|
|
397
|
+
}
|
|
398
|
+
}
|
|
399
|
+
return results;
|
|
400
|
+
}
|
|
401
|
+
}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { type PowerSyncOpenFactoryOptions } from '@powersync/common';
|
|
2
|
+
import { ResolvedWebSQLOpenOptions, TemporaryStorageOption, WebSQLFlags } from '../web-sql-flags.js';
|
|
3
|
+
import { InternalWASQLiteDBAdapter } from './InternalWASQLiteDBAdapter.js';
|
|
4
|
+
import { WASQLiteVFS } from './WASQLiteConnection.js';
|
|
5
|
+
/**
|
|
6
|
+
* These flags are the same as {@link WebSQLFlags}.
|
|
7
|
+
* This export is maintained only for API consistency
|
|
8
|
+
*/
|
|
9
|
+
export type WASQLiteFlags = WebSQLFlags;
|
|
10
|
+
export interface WASQLiteDBAdapterOptions extends Omit<PowerSyncOpenFactoryOptions, 'schema'> {
|
|
11
|
+
flags?: WASQLiteFlags;
|
|
12
|
+
/**
|
|
13
|
+
* Use an existing port to an initialized worker.
|
|
14
|
+
* A worker will be initialized if none is provided
|
|
15
|
+
*/
|
|
16
|
+
workerPort?: MessagePort;
|
|
17
|
+
worker?: string | URL | ((options: ResolvedWebSQLOpenOptions) => Worker | SharedWorker);
|
|
18
|
+
vfs?: WASQLiteVFS;
|
|
19
|
+
temporaryStorage?: TemporaryStorageOption;
|
|
20
|
+
cacheSizeKb?: number;
|
|
21
|
+
/**
|
|
22
|
+
* Encryption key for the database.
|
|
23
|
+
* If set, the database will be encrypted using multiple-ciphers.
|
|
24
|
+
*/
|
|
25
|
+
encryptionKey?: string;
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* Adapter for WA-SQLite SQLite connections.
|
|
29
|
+
*/
|
|
30
|
+
export declare class WASQLiteDBAdapter extends InternalWASQLiteDBAdapter {
|
|
31
|
+
constructor(options: WASQLiteDBAdapterOptions);
|
|
32
|
+
}
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import * as Comlink from 'comlink';
|
|
2
|
+
import { resolveWebPowerSyncFlags } from '../../PowerSyncDatabase.js';
|
|
3
|
+
import { DEFAULT_CACHE_SIZE_KB, TemporaryStorageOption } from '../web-sql-flags.js';
|
|
4
|
+
import { WorkerWrappedAsyncDatabaseConnection } from '../WorkerWrappedAsyncDatabaseConnection.js';
|
|
5
|
+
import { InternalWASQLiteDBAdapter } from './InternalWASQLiteDBAdapter.js';
|
|
6
|
+
import { WASQLiteOpenFactory } from './WASQLiteOpenFactory.js';
|
|
7
|
+
/**
|
|
8
|
+
* Adapter for WA-SQLite SQLite connections.
|
|
9
|
+
*/
|
|
10
|
+
export class WASQLiteDBAdapter extends InternalWASQLiteDBAdapter {
|
|
11
|
+
constructor(options) {
|
|
12
|
+
super({
|
|
13
|
+
name: options.dbFilename,
|
|
14
|
+
openConnection: async () => {
|
|
15
|
+
const { workerPort, temporaryStorage, cacheSizeKb } = options;
|
|
16
|
+
if (workerPort) {
|
|
17
|
+
const remote = Comlink.wrap(workerPort);
|
|
18
|
+
return new WorkerWrappedAsyncDatabaseConnection({
|
|
19
|
+
remote,
|
|
20
|
+
remoteCanCloseUnexpectedly: false,
|
|
21
|
+
identifier: options.dbFilename,
|
|
22
|
+
baseConnection: await remote({
|
|
23
|
+
...options,
|
|
24
|
+
temporaryStorage: temporaryStorage ?? TemporaryStorageOption.MEMORY,
|
|
25
|
+
cacheSizeKb: cacheSizeKb ?? DEFAULT_CACHE_SIZE_KB,
|
|
26
|
+
flags: resolveWebPowerSyncFlags(options.flags),
|
|
27
|
+
encryptionKey: options.encryptionKey
|
|
28
|
+
})
|
|
29
|
+
});
|
|
30
|
+
}
|
|
31
|
+
const openFactory = new WASQLiteOpenFactory({
|
|
32
|
+
dbFilename: options.dbFilename,
|
|
33
|
+
dbLocation: options.dbLocation,
|
|
34
|
+
debugMode: options.debugMode,
|
|
35
|
+
flags: options.flags,
|
|
36
|
+
temporaryStorage,
|
|
37
|
+
cacheSizeKb,
|
|
38
|
+
logger: options.logger,
|
|
39
|
+
vfs: options.vfs,
|
|
40
|
+
encryptionKey: options.encryptionKey,
|
|
41
|
+
worker: options.worker
|
|
42
|
+
});
|
|
43
|
+
return openFactory.openConnection();
|
|
44
|
+
},
|
|
45
|
+
debugMode: options.debugMode,
|
|
46
|
+
logger: options.logger
|
|
47
|
+
});
|
|
48
|
+
}
|
|
49
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { DBAdapter, type ILogLevel } from '@powersync/common';
|
|
2
|
+
import { AbstractWebSQLOpenFactory } from '../AbstractWebSQLOpenFactory.js';
|
|
3
|
+
import { AsyncDatabaseConnection } from '../AsyncDatabaseConnection.js';
|
|
4
|
+
import { ResolvedWebSQLOpenOptions, WebSQLOpenFactoryOptions } from '../web-sql-flags.js';
|
|
5
|
+
import { WASQLiteVFS } from './WASQLiteConnection.js';
|
|
6
|
+
export interface WASQLiteOpenFactoryOptions extends WebSQLOpenFactoryOptions {
|
|
7
|
+
vfs?: WASQLiteVFS;
|
|
8
|
+
}
|
|
9
|
+
export interface ResolvedWASQLiteOpenFactoryOptions extends ResolvedWebSQLOpenOptions {
|
|
10
|
+
vfs: WASQLiteVFS;
|
|
11
|
+
}
|
|
12
|
+
export interface WorkerDBOpenerOptions extends ResolvedWASQLiteOpenFactoryOptions {
|
|
13
|
+
logLevel: ILogLevel;
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* Opens a SQLite connection using WA-SQLite.
|
|
17
|
+
*/
|
|
18
|
+
export declare class WASQLiteOpenFactory extends AbstractWebSQLOpenFactory {
|
|
19
|
+
constructor(options: WASQLiteOpenFactoryOptions);
|
|
20
|
+
get waOptions(): WASQLiteOpenFactoryOptions;
|
|
21
|
+
protected openAdapter(): DBAdapter;
|
|
22
|
+
openConnection(): Promise<AsyncDatabaseConnection>;
|
|
23
|
+
}
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
import * as Comlink from 'comlink';
|
|
2
|
+
import { openWorkerDatabasePort, resolveWorkerDatabasePortFactory } from '../../../worker/db/open-worker-database.js';
|
|
3
|
+
import { AbstractWebSQLOpenFactory } from '../AbstractWebSQLOpenFactory.js';
|
|
4
|
+
import { WorkerWrappedAsyncDatabaseConnection } from '../WorkerWrappedAsyncDatabaseConnection.js';
|
|
5
|
+
import { DEFAULT_CACHE_SIZE_KB, TemporaryStorageOption } from '../web-sql-flags.js';
|
|
6
|
+
import { InternalWASQLiteDBAdapter } from './InternalWASQLiteDBAdapter.js';
|
|
7
|
+
import { WASQLiteVFS, WASqliteConnection } from './WASQLiteConnection.js';
|
|
8
|
+
/**
|
|
9
|
+
* Opens a SQLite connection using WA-SQLite.
|
|
10
|
+
*/
|
|
11
|
+
export class WASQLiteOpenFactory extends AbstractWebSQLOpenFactory {
|
|
12
|
+
constructor(options) {
|
|
13
|
+
super(options);
|
|
14
|
+
assertValidWASQLiteOpenFactoryOptions(options);
|
|
15
|
+
}
|
|
16
|
+
get waOptions() {
|
|
17
|
+
// Cast to extended type
|
|
18
|
+
return this.options;
|
|
19
|
+
}
|
|
20
|
+
openAdapter() {
|
|
21
|
+
return new InternalWASQLiteDBAdapter({
|
|
22
|
+
name: this.options.dbFilename,
|
|
23
|
+
openConnection: () => this.openConnection(),
|
|
24
|
+
debugMode: this.options.debugMode,
|
|
25
|
+
logger: this.logger
|
|
26
|
+
});
|
|
27
|
+
}
|
|
28
|
+
async openConnection() {
|
|
29
|
+
const { enableMultiTabs, useWebWorker } = this.resolvedFlags;
|
|
30
|
+
const { vfs = WASQLiteVFS.IDBBatchAtomicVFS, temporaryStorage = TemporaryStorageOption.MEMORY, cacheSizeKb = DEFAULT_CACHE_SIZE_KB, encryptionKey } = this.waOptions;
|
|
31
|
+
if (!enableMultiTabs) {
|
|
32
|
+
this.logger.warn('Multiple tabs are not enabled in this browser');
|
|
33
|
+
}
|
|
34
|
+
if (useWebWorker) {
|
|
35
|
+
const optionsDbWorker = this.options.worker;
|
|
36
|
+
const workerPort = typeof optionsDbWorker == 'function'
|
|
37
|
+
? resolveWorkerDatabasePortFactory(() => optionsDbWorker({
|
|
38
|
+
...this.options,
|
|
39
|
+
temporaryStorage,
|
|
40
|
+
cacheSizeKb,
|
|
41
|
+
flags: this.resolvedFlags,
|
|
42
|
+
encryptionKey
|
|
43
|
+
}))
|
|
44
|
+
: openWorkerDatabasePort(this.options.dbFilename, enableMultiTabs, optionsDbWorker, this.waOptions.vfs);
|
|
45
|
+
const workerDBOpener = Comlink.wrap(workerPort);
|
|
46
|
+
return new WorkerWrappedAsyncDatabaseConnection({
|
|
47
|
+
remote: workerDBOpener,
|
|
48
|
+
// This tab owns the worker, so we're guaranteed to outlive it.
|
|
49
|
+
remoteCanCloseUnexpectedly: false,
|
|
50
|
+
baseConnection: await workerDBOpener({
|
|
51
|
+
dbFilename: this.options.dbFilename,
|
|
52
|
+
vfs,
|
|
53
|
+
temporaryStorage,
|
|
54
|
+
cacheSizeKb,
|
|
55
|
+
flags: this.resolvedFlags,
|
|
56
|
+
encryptionKey: encryptionKey,
|
|
57
|
+
logLevel: this.logger.getLevel()
|
|
58
|
+
}),
|
|
59
|
+
identifier: this.options.dbFilename,
|
|
60
|
+
onClose: () => {
|
|
61
|
+
if (workerPort instanceof Worker) {
|
|
62
|
+
workerPort.terminate();
|
|
63
|
+
}
|
|
64
|
+
else {
|
|
65
|
+
workerPort.close();
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
});
|
|
69
|
+
}
|
|
70
|
+
else {
|
|
71
|
+
// Don't use a web worker
|
|
72
|
+
return new WASqliteConnection({
|
|
73
|
+
dbFilename: this.options.dbFilename,
|
|
74
|
+
dbLocation: this.options.dbLocation,
|
|
75
|
+
debugMode: this.options.debugMode,
|
|
76
|
+
vfs,
|
|
77
|
+
temporaryStorage,
|
|
78
|
+
cacheSizeKb,
|
|
79
|
+
flags: this.resolvedFlags,
|
|
80
|
+
encryptionKey: encryptionKey
|
|
81
|
+
});
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
/**
|
|
86
|
+
* Asserts that the factory options are valid.
|
|
87
|
+
*/
|
|
88
|
+
function assertValidWASQLiteOpenFactoryOptions(options) {
|
|
89
|
+
// The OPFS VFS only works in dedicated web workers.
|
|
90
|
+
if ('vfs' in options && 'flags' in options) {
|
|
91
|
+
const { vfs, flags = {} } = options;
|
|
92
|
+
if (vfs !== WASQLiteVFS.IDBBatchAtomicVFS && 'useWebWorker' in flags && !flags.useWebWorker) {
|
|
93
|
+
throw new Error(`Invalid configuration: The 'useWebWorker' flag must be true when using an OPFS-based VFS (${vfs}).`);
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { AbstractPowerSyncDatabase, DBAdapter, PowerSyncDatabaseOptions } from '@powersync/common';
|
|
2
|
+
import { AbstractWebPowerSyncDatabaseOpenFactory } from '../AbstractWebPowerSyncDatabaseOpenFactory.js';
|
|
3
|
+
/**
|
|
4
|
+
* @deprecated {@link PowerSyncDatabase} can now be constructed directly
|
|
5
|
+
* @example
|
|
6
|
+
* ```typescript
|
|
7
|
+
* const powersync = new PowerSyncDatabase({database: {
|
|
8
|
+
* dbFileName: 'powersync.db'
|
|
9
|
+
* }});
|
|
10
|
+
* ```
|
|
11
|
+
*/
|
|
12
|
+
export declare class WASQLitePowerSyncDatabaseOpenFactory extends AbstractWebPowerSyncDatabaseOpenFactory {
|
|
13
|
+
protected openDB(): DBAdapter;
|
|
14
|
+
generateInstance(options: PowerSyncDatabaseOptions): AbstractPowerSyncDatabase;
|
|
15
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { PowerSyncDatabase } from '../../../db/PowerSyncDatabase.js';
|
|
2
|
+
import { AbstractWebPowerSyncDatabaseOpenFactory } from '../AbstractWebPowerSyncDatabaseOpenFactory.js';
|
|
3
|
+
import { WASQLiteOpenFactory } from './WASQLiteOpenFactory.js';
|
|
4
|
+
/**
|
|
5
|
+
* @deprecated {@link PowerSyncDatabase} can now be constructed directly
|
|
6
|
+
* @example
|
|
7
|
+
* ```typescript
|
|
8
|
+
* const powersync = new PowerSyncDatabase({database: {
|
|
9
|
+
* dbFileName: 'powersync.db'
|
|
10
|
+
* }});
|
|
11
|
+
* ```
|
|
12
|
+
*/
|
|
13
|
+
export class WASQLitePowerSyncDatabaseOpenFactory extends AbstractWebPowerSyncDatabaseOpenFactory {
|
|
14
|
+
openDB() {
|
|
15
|
+
const factory = new WASQLiteOpenFactory(this.options);
|
|
16
|
+
return factory.openDB();
|
|
17
|
+
}
|
|
18
|
+
generateInstance(options) {
|
|
19
|
+
return new PowerSyncDatabase(options);
|
|
20
|
+
}
|
|
21
|
+
}
|