@powersync/web 1.12.2 → 1.13.0

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.
Files changed (68) hide show
  1. package/README.md +33 -0
  2. package/dist/3cb48be086dd9edd02ff.wasm +0 -0
  3. 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
  4. package/dist/_journeyapps_wa-sqlite-_journeyapps_wa-sqlite_src_examples_IDBBatchAtomicVFS_js-_powersync_co-780aa20.index.umd.js.map +1 -0
  5. 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
  6. package/dist/_journeyapps_wa-sqlite-_journeyapps_wa-sqlite_src_examples_IDBBatchAtomicVFS_js-_powersync_co-780aa21.index.umd.js.map +1 -0
  7. package/dist/df958358cadf945bd0fe.wasm +0 -0
  8. package/dist/f9c8ada26c59f5bf4339.wasm +0 -0
  9. package/dist/fe5693c7678cf12e05ac.wasm +0 -0
  10. package/dist/index.umd.js +3709 -499
  11. package/dist/index.umd.js.map +1 -1
  12. package/dist/worker/SharedSyncImplementation.umd.js +247 -2172
  13. package/dist/worker/SharedSyncImplementation.umd.js.map +1 -1
  14. package/dist/worker/WASQLiteDB.umd.js +767 -196
  15. package/dist/worker/WASQLiteDB.umd.js.map +1 -1
  16. package/dist/worker/node_modules_journeyapps_wa-sqlite_dist_mc-wa-sqlite-async_mjs.umd.js +45 -0
  17. package/dist/worker/node_modules_journeyapps_wa-sqlite_dist_mc-wa-sqlite-async_mjs.umd.js.map +1 -0
  18. package/dist/worker/node_modules_journeyapps_wa-sqlite_dist_mc-wa-sqlite_mjs.umd.js +45 -0
  19. package/dist/worker/node_modules_journeyapps_wa-sqlite_dist_mc-wa-sqlite_mjs.umd.js.map +1 -0
  20. package/dist/worker/node_modules_journeyapps_wa-sqlite_dist_wa-sqlite-async_mjs.umd.js +2 -2
  21. package/dist/worker/node_modules_journeyapps_wa-sqlite_dist_wa-sqlite-async_mjs.umd.js.map +1 -1
  22. package/dist/worker/node_modules_journeyapps_wa-sqlite_dist_wa-sqlite_mjs.umd.js +45 -0
  23. package/dist/worker/node_modules_journeyapps_wa-sqlite_dist_wa-sqlite_mjs.umd.js.map +1 -0
  24. package/dist/worker/node_modules_journeyapps_wa-sqlite_src_examples_AccessHandlePoolVFS_js.umd.js +1509 -0
  25. package/dist/worker/node_modules_journeyapps_wa-sqlite_src_examples_AccessHandlePoolVFS_js.umd.js.map +1 -0
  26. package/dist/worker/node_modules_journeyapps_wa-sqlite_src_examples_IDBBatchAtomicVFS_js.umd.js +39 -0
  27. package/dist/worker/node_modules_journeyapps_wa-sqlite_src_examples_IDBBatchAtomicVFS_js.umd.js.map +1 -1
  28. package/dist/worker/node_modules_journeyapps_wa-sqlite_src_examples_OPFSCoopSyncVFS_js.umd.js +1641 -0
  29. package/dist/worker/node_modules_journeyapps_wa-sqlite_src_examples_OPFSCoopSyncVFS_js.umd.js.map +1 -0
  30. package/lib/package.json +6 -6
  31. package/lib/src/db/PowerSyncDatabase.d.ts +10 -2
  32. package/lib/src/db/PowerSyncDatabase.js +20 -4
  33. package/lib/src/db/adapters/AbstractWebSQLOpenFactory.d.ts +2 -0
  34. package/lib/src/db/adapters/AbstractWebSQLOpenFactory.js +3 -0
  35. package/lib/src/db/adapters/AsyncDatabaseConnection.d.ts +26 -0
  36. package/lib/src/db/adapters/LockedAsyncDatabaseAdapter.d.ts +82 -0
  37. package/lib/src/db/adapters/LockedAsyncDatabaseAdapter.js +239 -0
  38. package/lib/src/db/adapters/WebDBAdapter.d.ts +17 -0
  39. package/lib/src/db/adapters/WebDBAdapter.js +1 -0
  40. package/lib/src/db/adapters/WorkerWrappedAsyncDatabaseConnection.d.ts +39 -0
  41. package/lib/src/db/adapters/WorkerWrappedAsyncDatabaseConnection.js +46 -0
  42. package/lib/src/db/adapters/wa-sqlite/WASQLiteConnection.d.ts +127 -0
  43. package/lib/src/db/adapters/wa-sqlite/WASQLiteConnection.js +343 -0
  44. package/lib/src/db/adapters/wa-sqlite/WASQLiteDBAdapter.d.ts +10 -42
  45. package/lib/src/db/adapters/wa-sqlite/WASQLiteDBAdapter.js +36 -212
  46. package/lib/src/db/adapters/wa-sqlite/WASQLiteOpenFactory.d.ts +12 -0
  47. package/lib/src/db/adapters/wa-sqlite/WASQLiteOpenFactory.js +81 -4
  48. package/lib/src/db/adapters/web-sql-flags.d.ts +17 -0
  49. package/lib/src/db/sync/SharedWebStreamingSyncImplementation.d.ts +9 -2
  50. package/lib/src/db/sync/SharedWebStreamingSyncImplementation.js +16 -10
  51. package/lib/src/db/sync/WebStreamingSyncImplementation.d.ts +0 -5
  52. package/lib/src/index.d.ts +8 -7
  53. package/lib/src/index.js +8 -7
  54. package/lib/src/worker/db/WASQLiteDB.worker.js +38 -20
  55. package/lib/src/worker/db/open-worker-database.d.ts +5 -4
  56. package/lib/src/worker/db/open-worker-database.js +5 -3
  57. package/lib/src/worker/sync/AbstractSharedSyncClientProvider.d.ts +1 -0
  58. package/lib/src/worker/sync/SharedSyncImplementation.d.ts +20 -3
  59. package/lib/src/worker/sync/SharedSyncImplementation.js +40 -11
  60. package/lib/tsconfig.tsbuildinfo +1 -1
  61. package/package.json +7 -7
  62. package/dist/24cd027f23123a1360de.wasm +0 -0
  63. package/dist/_journeyapps_wa-sqlite-_journeyapps_wa-sqlite_src_examples_IDBBatchAtomicVFS_js-async-mutex-c-3cff7d0.index.umd.js.map +0 -1
  64. package/dist/_journeyapps_wa-sqlite-_journeyapps_wa-sqlite_src_examples_IDBBatchAtomicVFS_js-async-mutex-c-3cff7d1.index.umd.js.map +0 -1
  65. package/lib/src/shared/open-db.d.ts +0 -5
  66. package/lib/src/shared/open-db.js +0 -193
  67. package/lib/src/shared/types.d.ts +0 -22
  68. /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>>];