@powersync/web 1.12.3 → 1.13.1
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/README.md +33 -0
- package/dist/3cb48be086dd9edd02ff.wasm +0 -0
- package/dist/{_journeyapps_wa-sqlite-_journeyapps_wa-sqlite_src_examples_IDBBatchAtomicVFS_js-async-mutex-c-3cff7d0.index.umd.js → _journeyapps_wa-sqlite-_journeyapps_wa-sqlite_src_examples_IDBBatchAtomicVFS_js-_powersync_co-780aa20.index.umd.js} +18 -8
- package/dist/_journeyapps_wa-sqlite-_journeyapps_wa-sqlite_src_examples_IDBBatchAtomicVFS_js-_powersync_co-780aa20.index.umd.js.map +1 -0
- package/dist/{_journeyapps_wa-sqlite-_journeyapps_wa-sqlite_src_examples_IDBBatchAtomicVFS_js-async-mutex-c-3cff7d1.index.umd.js → _journeyapps_wa-sqlite-_journeyapps_wa-sqlite_src_examples_IDBBatchAtomicVFS_js-_powersync_co-780aa21.index.umd.js} +18 -8
- package/dist/_journeyapps_wa-sqlite-_journeyapps_wa-sqlite_src_examples_IDBBatchAtomicVFS_js-_powersync_co-780aa21.index.umd.js.map +1 -0
- package/dist/df958358cadf945bd0fe.wasm +0 -0
- package/dist/f9c8ada26c59f5bf4339.wasm +0 -0
- package/dist/index.umd.js +3710 -498
- package/dist/index.umd.js.map +1 -1
- package/dist/worker/SharedSyncImplementation.umd.js +286 -2210
- package/dist/worker/SharedSyncImplementation.umd.js.map +1 -1
- package/dist/worker/WASQLiteDB.umd.js +768 -196
- package/dist/worker/WASQLiteDB.umd.js.map +1 -1
- package/dist/worker/node_modules_journeyapps_wa-sqlite_dist_mc-wa-sqlite-async_mjs.umd.js +45 -0
- package/dist/worker/node_modules_journeyapps_wa-sqlite_dist_mc-wa-sqlite-async_mjs.umd.js.map +1 -0
- package/dist/worker/node_modules_journeyapps_wa-sqlite_dist_mc-wa-sqlite_mjs.umd.js +45 -0
- package/dist/worker/node_modules_journeyapps_wa-sqlite_dist_mc-wa-sqlite_mjs.umd.js.map +1 -0
- package/dist/worker/node_modules_journeyapps_wa-sqlite_dist_wa-sqlite_mjs.umd.js +45 -0
- package/dist/worker/node_modules_journeyapps_wa-sqlite_dist_wa-sqlite_mjs.umd.js.map +1 -0
- package/dist/worker/node_modules_journeyapps_wa-sqlite_src_examples_AccessHandlePoolVFS_js.umd.js +1509 -0
- package/dist/worker/node_modules_journeyapps_wa-sqlite_src_examples_AccessHandlePoolVFS_js.umd.js.map +1 -0
- package/dist/worker/node_modules_journeyapps_wa-sqlite_src_examples_OPFSCoopSyncVFS_js.umd.js +1641 -0
- package/dist/worker/node_modules_journeyapps_wa-sqlite_src_examples_OPFSCoopSyncVFS_js.umd.js.map +1 -0
- package/lib/package.json +3 -3
- package/lib/src/db/PowerSyncDatabase.d.ts +12 -4
- package/lib/src/db/PowerSyncDatabase.js +23 -5
- package/lib/src/db/adapters/AbstractWebSQLOpenFactory.d.ts +2 -0
- package/lib/src/db/adapters/AbstractWebSQLOpenFactory.js +3 -0
- package/lib/src/db/adapters/AsyncDatabaseConnection.d.ts +26 -0
- package/lib/src/db/adapters/LockedAsyncDatabaseAdapter.d.ts +82 -0
- package/lib/src/db/adapters/LockedAsyncDatabaseAdapter.js +239 -0
- package/lib/src/db/adapters/WebDBAdapter.d.ts +17 -0
- package/lib/src/db/adapters/WebDBAdapter.js +1 -0
- package/lib/src/db/adapters/WorkerWrappedAsyncDatabaseConnection.d.ts +39 -0
- package/lib/src/db/adapters/WorkerWrappedAsyncDatabaseConnection.js +46 -0
- package/lib/src/db/adapters/wa-sqlite/WASQLiteConnection.d.ts +127 -0
- package/lib/src/db/adapters/wa-sqlite/WASQLiteConnection.js +343 -0
- package/lib/src/db/adapters/wa-sqlite/WASQLiteDBAdapter.d.ts +10 -42
- package/lib/src/db/adapters/wa-sqlite/WASQLiteDBAdapter.js +36 -212
- package/lib/src/db/adapters/wa-sqlite/WASQLiteOpenFactory.d.ts +12 -0
- package/lib/src/db/adapters/wa-sqlite/WASQLiteOpenFactory.js +81 -4
- package/lib/src/db/adapters/web-sql-flags.d.ts +17 -0
- package/lib/src/db/sync/SharedWebStreamingSyncImplementation.d.ts +9 -2
- package/lib/src/db/sync/SharedWebStreamingSyncImplementation.js +16 -10
- package/lib/src/db/sync/WebStreamingSyncImplementation.d.ts +0 -5
- package/lib/src/index.d.ts +8 -7
- package/lib/src/index.js +8 -7
- package/lib/src/worker/db/WASQLiteDB.worker.js +38 -20
- package/lib/src/worker/db/open-worker-database.d.ts +5 -4
- package/lib/src/worker/db/open-worker-database.js +5 -3
- package/lib/src/worker/sync/AbstractSharedSyncClientProvider.d.ts +1 -0
- package/lib/src/worker/sync/SharedSyncImplementation.d.ts +20 -3
- package/lib/src/worker/sync/SharedSyncImplementation.js +40 -11
- package/lib/tsconfig.tsbuildinfo +1 -1
- package/package.json +4 -4
- package/dist/_journeyapps_wa-sqlite-_journeyapps_wa-sqlite_src_examples_IDBBatchAtomicVFS_js-async-mutex-c-3cff7d0.index.umd.js.map +0 -1
- package/dist/_journeyapps_wa-sqlite-_journeyapps_wa-sqlite_src_examples_IDBBatchAtomicVFS_js-async-mutex-c-3cff7d1.index.umd.js.map +0 -1
- package/lib/src/shared/open-db.d.ts +0 -5
- package/lib/src/shared/open-db.js +0 -193
- package/lib/src/shared/types.d.ts +0 -22
- /package/lib/src/{shared/types.js → db/adapters/AsyncDatabaseConnection.js} +0 -0
|
@@ -1,193 +0,0 @@
|
|
|
1
|
-
import '@journeyapps/wa-sqlite';
|
|
2
|
-
import * as SQLite from '@journeyapps/wa-sqlite';
|
|
3
|
-
import { Mutex } from 'async-mutex';
|
|
4
|
-
import * as Comlink from 'comlink';
|
|
5
|
-
let nextId = 1;
|
|
6
|
-
export async function _openDB(dbFileName, options = { useWebWorker: true }) {
|
|
7
|
-
const { default: moduleFactory } = await import('@journeyapps/wa-sqlite/dist/wa-sqlite-async.mjs');
|
|
8
|
-
const module = await moduleFactory();
|
|
9
|
-
const sqlite3 = SQLite.Factory(module);
|
|
10
|
-
/**
|
|
11
|
-
* Register the PowerSync core SQLite extension
|
|
12
|
-
*/
|
|
13
|
-
module.ccall('powersync_init_static', 'int', []);
|
|
14
|
-
const { IDBBatchAtomicVFS } = await import('@journeyapps/wa-sqlite/src/examples/IDBBatchAtomicVFS.js');
|
|
15
|
-
// @ts-expect-error The types for this static method are missing upstream
|
|
16
|
-
const vfs = await IDBBatchAtomicVFS.create(dbFileName, module, { lockPolicy: 'exclusive' });
|
|
17
|
-
sqlite3.vfs_register(vfs, true);
|
|
18
|
-
const db = await sqlite3.open_v2(dbFileName);
|
|
19
|
-
const statementMutex = new Mutex();
|
|
20
|
-
/**
|
|
21
|
-
* Listeners are exclusive to the DB connection.
|
|
22
|
-
*/
|
|
23
|
-
const listeners = new Map();
|
|
24
|
-
let updatedTables = new Set();
|
|
25
|
-
let updateTimer = null;
|
|
26
|
-
function fireUpdates() {
|
|
27
|
-
updateTimer = null;
|
|
28
|
-
const event = { tables: [...updatedTables], groupedUpdates: {}, rawUpdates: [] };
|
|
29
|
-
updatedTables.clear();
|
|
30
|
-
Array.from(listeners.values()).forEach((l) => l(event));
|
|
31
|
-
}
|
|
32
|
-
sqlite3.update_hook(db, (updateType, dbName, tableName) => {
|
|
33
|
-
if (!tableName) {
|
|
34
|
-
return;
|
|
35
|
-
}
|
|
36
|
-
updatedTables.add(tableName);
|
|
37
|
-
if (updateTimer == null) {
|
|
38
|
-
updateTimer = setTimeout(fireUpdates, 0);
|
|
39
|
-
}
|
|
40
|
-
});
|
|
41
|
-
/**
|
|
42
|
-
* This executes single SQL statements inside a requested lock.
|
|
43
|
-
*/
|
|
44
|
-
const execute = async (sql, bindings) => {
|
|
45
|
-
// Running multiple statements on the same connection concurrently should not be allowed
|
|
46
|
-
return _acquireExecuteLock(async () => {
|
|
47
|
-
return executeSingleStatement(sql, bindings);
|
|
48
|
-
});
|
|
49
|
-
};
|
|
50
|
-
/**
|
|
51
|
-
* This requests a lock for executing statements.
|
|
52
|
-
* Should only be used internally.
|
|
53
|
-
*/
|
|
54
|
-
const _acquireExecuteLock = (callback) => {
|
|
55
|
-
return statementMutex.runExclusive(callback);
|
|
56
|
-
};
|
|
57
|
-
/**
|
|
58
|
-
* This executes a single statement using SQLite3.
|
|
59
|
-
*/
|
|
60
|
-
const executeSingleStatement = async (sql, bindings) => {
|
|
61
|
-
const results = [];
|
|
62
|
-
for await (const stmt of sqlite3.statements(db, sql)) {
|
|
63
|
-
let columns;
|
|
64
|
-
const wrappedBindings = bindings ? [bindings] : [[]];
|
|
65
|
-
for (const binding of wrappedBindings) {
|
|
66
|
-
// TODO not sure why this is needed currently, but booleans break
|
|
67
|
-
binding.forEach((b, index, arr) => {
|
|
68
|
-
if (typeof b == 'boolean') {
|
|
69
|
-
arr[index] = b ? 1 : 0;
|
|
70
|
-
}
|
|
71
|
-
});
|
|
72
|
-
sqlite3.reset(stmt);
|
|
73
|
-
if (bindings) {
|
|
74
|
-
sqlite3.bind_collection(stmt, binding);
|
|
75
|
-
}
|
|
76
|
-
const rows = [];
|
|
77
|
-
while ((await sqlite3.step(stmt)) === SQLite.SQLITE_ROW) {
|
|
78
|
-
const row = sqlite3.row(stmt);
|
|
79
|
-
rows.push(row);
|
|
80
|
-
}
|
|
81
|
-
columns = columns ?? sqlite3.column_names(stmt);
|
|
82
|
-
if (columns.length) {
|
|
83
|
-
results.push({ columns, rows });
|
|
84
|
-
}
|
|
85
|
-
}
|
|
86
|
-
// When binding parameters, only a single statement is executed.
|
|
87
|
-
if (bindings) {
|
|
88
|
-
break;
|
|
89
|
-
}
|
|
90
|
-
}
|
|
91
|
-
const rows = [];
|
|
92
|
-
for (const resultset of results) {
|
|
93
|
-
for (const row of resultset.rows) {
|
|
94
|
-
const outRow = {};
|
|
95
|
-
resultset.columns.forEach((key, index) => {
|
|
96
|
-
outRow[key] = row[index];
|
|
97
|
-
});
|
|
98
|
-
rows.push(outRow);
|
|
99
|
-
}
|
|
100
|
-
}
|
|
101
|
-
const result = {
|
|
102
|
-
insertId: sqlite3.last_insert_id(db),
|
|
103
|
-
rowsAffected: sqlite3.changes(db),
|
|
104
|
-
rows: {
|
|
105
|
-
_array: rows,
|
|
106
|
-
length: rows.length
|
|
107
|
-
}
|
|
108
|
-
};
|
|
109
|
-
return result;
|
|
110
|
-
};
|
|
111
|
-
/**
|
|
112
|
-
* This executes SQL statements in a batch.
|
|
113
|
-
*/
|
|
114
|
-
const executeBatch = async (sql, bindings) => {
|
|
115
|
-
return _acquireExecuteLock(async () => {
|
|
116
|
-
let affectedRows = 0;
|
|
117
|
-
try {
|
|
118
|
-
await executeSingleStatement('BEGIN TRANSACTION');
|
|
119
|
-
const wrappedBindings = bindings ? bindings : [];
|
|
120
|
-
for await (const stmt of sqlite3.statements(db, sql)) {
|
|
121
|
-
if (stmt === null) {
|
|
122
|
-
return {
|
|
123
|
-
rowsAffected: 0,
|
|
124
|
-
rows: { _array: [], length: 0 }
|
|
125
|
-
};
|
|
126
|
-
}
|
|
127
|
-
//Prepare statement once
|
|
128
|
-
for (const binding of wrappedBindings) {
|
|
129
|
-
// TODO not sure why this is needed currently, but booleans break
|
|
130
|
-
for (let i = 0; i < binding.length; i++) {
|
|
131
|
-
const b = binding[i];
|
|
132
|
-
if (typeof b == 'boolean') {
|
|
133
|
-
binding[i] = b ? 1 : 0;
|
|
134
|
-
}
|
|
135
|
-
}
|
|
136
|
-
if (bindings) {
|
|
137
|
-
sqlite3.bind_collection(stmt, binding);
|
|
138
|
-
}
|
|
139
|
-
const result = await sqlite3.step(stmt);
|
|
140
|
-
if (result === SQLite.SQLITE_DONE) {
|
|
141
|
-
//The value returned by sqlite3_changes() immediately after an INSERT, UPDATE or DELETE statement run on a view is always zero.
|
|
142
|
-
affectedRows += sqlite3.changes(db);
|
|
143
|
-
}
|
|
144
|
-
sqlite3.reset(stmt);
|
|
145
|
-
}
|
|
146
|
-
}
|
|
147
|
-
await executeSingleStatement('COMMIT');
|
|
148
|
-
}
|
|
149
|
-
catch (err) {
|
|
150
|
-
await executeSingleStatement('ROLLBACK');
|
|
151
|
-
return {
|
|
152
|
-
rowsAffected: 0,
|
|
153
|
-
rows: { _array: [], length: 0 }
|
|
154
|
-
};
|
|
155
|
-
}
|
|
156
|
-
const result = {
|
|
157
|
-
rowsAffected: affectedRows,
|
|
158
|
-
rows: { _array: [], length: 0 }
|
|
159
|
-
};
|
|
160
|
-
return result;
|
|
161
|
-
});
|
|
162
|
-
};
|
|
163
|
-
if (options.useWebWorker) {
|
|
164
|
-
const registerOnTableChange = (callback) => {
|
|
165
|
-
const id = nextId++;
|
|
166
|
-
listeners.set(id, callback);
|
|
167
|
-
return Comlink.proxy(() => {
|
|
168
|
-
listeners.delete(id);
|
|
169
|
-
});
|
|
170
|
-
};
|
|
171
|
-
return {
|
|
172
|
-
execute: Comlink.proxy(execute),
|
|
173
|
-
executeBatch: Comlink.proxy(executeBatch),
|
|
174
|
-
registerOnTableChange: Comlink.proxy(registerOnTableChange),
|
|
175
|
-
close: Comlink.proxy(() => {
|
|
176
|
-
sqlite3.close(db);
|
|
177
|
-
})
|
|
178
|
-
};
|
|
179
|
-
}
|
|
180
|
-
const registerOnTableChange = (callback) => {
|
|
181
|
-
const id = nextId++;
|
|
182
|
-
listeners.set(id, callback);
|
|
183
|
-
return () => {
|
|
184
|
-
listeners.delete(id);
|
|
185
|
-
};
|
|
186
|
-
};
|
|
187
|
-
return {
|
|
188
|
-
execute: execute,
|
|
189
|
-
executeBatch: executeBatch,
|
|
190
|
-
registerOnTableChange: registerOnTableChange,
|
|
191
|
-
close: () => sqlite3.close(db)
|
|
192
|
-
};
|
|
193
|
-
}
|
|
@@ -1,22 +0,0 @@
|
|
|
1
|
-
import type { BatchedUpdateNotification, QueryResult } from '@powersync/common';
|
|
2
|
-
export type WASQLExecuteResult = Omit<QueryResult, 'rows'> & {
|
|
3
|
-
rows: {
|
|
4
|
-
_array: any[];
|
|
5
|
-
length: number;
|
|
6
|
-
};
|
|
7
|
-
};
|
|
8
|
-
export type DBFunctionsInterface = {
|
|
9
|
-
close?: () => void;
|
|
10
|
-
execute: WASQLiteExecuteMethod;
|
|
11
|
-
executeBatch: WASQLiteExecuteBatchMethod;
|
|
12
|
-
registerOnTableChange: (callback: OnTableChangeCallback) => void;
|
|
13
|
-
};
|
|
14
|
-
/**
|
|
15
|
-
* @deprecated use [DBFunctionsInterface instead]
|
|
16
|
-
*/
|
|
17
|
-
export type DBWorkerInterface = DBFunctionsInterface;
|
|
18
|
-
export type WASQLiteExecuteMethod = (sql: string, params?: any[]) => Promise<WASQLExecuteResult>;
|
|
19
|
-
export type WASQLiteExecuteBatchMethod = (sql: string, params?: any[]) => Promise<WASQLExecuteResult>;
|
|
20
|
-
export type OnTableChangeCallback = (event: BatchedUpdateNotification) => void;
|
|
21
|
-
export type OpenDB = (dbFileName: string) => DBWorkerInterface;
|
|
22
|
-
export type SQLBatchTuple = [string] | [string, Array<any> | Array<Array<any>>];
|
|
File without changes
|