@tallyui/storage-sqlite 0.2.0 → 2.0.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.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Paul Kilmurray and TallyUI contributors
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,115 @@
1
+ # @tallyui/storage-sqlite
2
+
3
+ RxDB Premium SQLite storage for TallyUI: a synchronous SQLite handle
4
+ (`expo-sqlite`) on native, and SQLite-wasm on `opfs-sahpool` in one dedicated
5
+ worker on the web (ADR-061 in `docs/DECISIONS.md`).
6
+
7
+ `rxdb-premium` needs a licence key to install (ADR-031). Locally, set
8
+ `RXDB_PREMIUM=<token>`, see `docs/CONTRIBUTING.md`; in CI it comes from the
9
+ `RXDB_PREMIUM` secret. `expo-sqlite` and `@sqlite.org/sqlite-wasm` are both
10
+ optional peer dependencies: a native app installs only the first, a web app
11
+ only the second.
12
+
13
+ ## Web (SQLite-wasm)
14
+
15
+ `getRxStorageSQLiteWasm` (from `@tallyui/storage-sqlite/web`) runs SQLite-wasm
16
+ on `opfs-sahpool` inside one dedicated worker, reached through RxDB Premium's
17
+ `getRxStorageWorker` in mode `'one'`. `createSyncAccessHandle`, which
18
+ opfs-sahpool needs, exists only inside a dedicated worker, and opfs-sahpool
19
+ holds exclusive OPFS access handles for the whole origin — so the app must
20
+ run **exactly one live tab, one dedicated worker** per store (ADR-061 in
21
+ `docs/DECISIONS.md`); a second tab cannot open storage at all, and the
22
+ recovery from a dead or hung worker is a reload.
23
+
24
+ The app supplies the worker itself, as `workerInput`:
25
+
26
+ ```ts
27
+ import { getRxStorageSQLiteWasm } from '@tallyui/storage-sqlite/web';
28
+
29
+ const storage = getRxStorageSQLiteWasm({ workerInput /* see below */ });
30
+ ```
31
+
32
+ ### Vite
33
+
34
+ Pass a module-worker factory as `workerInput`:
35
+
36
+ ```ts
37
+ const storage = getRxStorageSQLiteWasm({
38
+ workerInput: () =>
39
+ new Worker(new URL('@tallyui/storage-sqlite/web-worker', import.meta.url), { type: 'module' }),
40
+ });
41
+ ```
42
+
43
+ and exclude the wasm package from dependency pre-bundling, so Vite emits
44
+ `sqlite3.wasm` as a static asset instead of trying to bundle it:
45
+
46
+ ```ts
47
+ // vite.config.ts
48
+ export default defineConfig({
49
+ optimizeDeps: { exclude: ['@sqlite.org/sqlite-wasm'] },
50
+ });
51
+ ```
52
+
53
+ ### Expo web (Metro)
54
+
55
+ Metro cannot bundle a module worker, so a Metro app prebuilds the
56
+ `@tallyui/storage-sqlite/web-worker` entry, together with `sqlite3.wasm`,
57
+ into the app's `public/` folder, as part of the app's own build:
58
+
59
+ ```sh
60
+ npm install --save-dev esbuild
61
+ npx tallyui-build-sqlite-worker public/
62
+ ```
63
+
64
+ Add the built files to the app's `.gitignore` (they link rxdb-premium code
65
+ under the app's own licence, so they must never be committed or published):
66
+
67
+ ```
68
+ public/tallyui-sqlite-worker.js
69
+ public/sqlite3.wasm
70
+ ```
71
+
72
+ and pass the built file's string URL as `workerInput`:
73
+
74
+ ```ts
75
+ const storage = getRxStorageSQLiteWasm({ workerInput: '/tallyui-sqlite-worker.js' });
76
+ ```
77
+
78
+ WCPOS `next` prebuilds its own worker the same way, as part of its own build
79
+ step.
80
+
81
+ `workerOptions` (the module type and the worker's debug name) only applies
82
+ when `workerInput` is a string or URL — RxDB Premium then constructs the
83
+ `Worker` itself. When `workerInput` is a function, as in the Vite example,
84
+ the function creates the `Worker`, so `workerOptions` is ignored; pass
85
+ `{ type: 'module' }` (and a name, if wanted) to the constructor call inside
86
+ the function instead.
87
+
88
+ ### Recognising a failed start
89
+
90
+ If the worker's start-up fails — most commonly another tab still holding the
91
+ opfs-sahpool database, or a browser with no OPFS or no sync access handles —
92
+ every storage call rejects with a `StorageWorkerStartError` instead of
93
+ hanging. Recognise it with `isStorageWorkerStartError` and tell the user to
94
+ close other tabs or reload:
95
+
96
+ ```ts
97
+ import { isStorageWorkerStartError } from '@tallyui/storage-sqlite/web';
98
+
99
+ try {
100
+ await db.addCollections({ /* ... */ });
101
+ } catch (error) {
102
+ if (isStorageWorkerStartError(error)) {
103
+ showMessage('Close other tabs or reload.');
104
+ } else {
105
+ throw error;
106
+ }
107
+ }
108
+ ```
109
+
110
+ ### Single live tab
111
+
112
+ A second tab must never open storage while the first one holds it; see
113
+ ADR-061 in `docs/DECISIONS.md` for the accepted take-over flow (a
114
+ BroadcastChannel hand-over with a timeout, and a parked "POS is open in
115
+ another tab" screen), which is a separate job from this package.
@@ -0,0 +1,18 @@
1
+ // src/web/errors.ts
2
+ var StorageWorkerStartError = class extends Error {
3
+ constructor(message, options) {
4
+ super(message, options);
5
+ this.name = "StorageWorkerStartError";
6
+ }
7
+ };
8
+ function isStorageWorkerStartError(error) {
9
+ if (typeof error === "string") return error.includes("StorageWorkerStartError");
10
+ if (!error || typeof error !== "object") return false;
11
+ const { name, message } = error;
12
+ return name === "StorageWorkerStartError" || typeof message === "string" && message.includes("StorageWorkerStartError");
13
+ }
14
+
15
+ export {
16
+ StorageWorkerStartError,
17
+ isStorageWorkerStartError
18
+ };
@@ -0,0 +1,8 @@
1
+ // src/params.ts
2
+ function sqliteBoolParams(params) {
3
+ return params.map((param) => typeof param === "boolean" ? Number(param) : param);
4
+ }
5
+
6
+ export {
7
+ sqliteBoolParams
8
+ };
package/dist/index.d.ts CHANGED
@@ -12,12 +12,11 @@ interface SQLiteStorageSettings {
12
12
  database: SQLiteDatabase;
13
13
  }
14
14
 
15
- interface SQLiteStorageInternals {
16
- database: SQLiteDatabase;
17
- tableName: string;
18
- }
19
-
20
- type RxStorageSQLite = RxStorage<SQLiteStorageInternals, SQLiteStorageSettings>;
21
- declare function getRxStorageSQLite(database: SQLiteDatabase): RxStorageSQLite;
15
+ /**
16
+ * Wraps a synchronous SQLite handle with RxDB Premium SQLite storage.
17
+ * Apps install the RxDB Premium peer dependency under their own RxDB Premium licence.
18
+ * One SQLite handle serves exactly one RxDB database.
19
+ */
20
+ declare function getRxStorageSQLite(database: SQLiteDatabase): RxStorage<any, any>;
22
21
 
23
22
  export { type SQLiteDatabase, type SQLiteStorageSettings, getRxStorageSQLite };
package/dist/index.js CHANGED
@@ -1,400 +1,34 @@
1
- // src/rx-storage-sqlite.ts
2
- import { ensureRxStorageInstanceParamsAreCorrect } from "rxdb";
3
- import { RXDB_VERSION } from "rxdb/plugins/utils";
4
-
5
- // src/storage-instance.ts
6
- import { categorizeBulkWriteRows, getQueryMatcher, getSortComparator } from "rxdb";
7
- import { now, ensureNotFalsy } from "rxdb/plugins/utils";
8
- import { Subject } from "rxjs";
9
-
10
- // src/mango-to-sql.ts
11
- function mangoQueryToSQL(query) {
12
- const params = [];
13
- const conditions = [];
14
- conditions.push(`json_extract(data, '$._deleted') = 0`);
15
- if (query.selector && Object.keys(query.selector).length > 0) {
16
- const selectorSql = selectorToSQL(query.selector, params);
17
- if (selectorSql) {
18
- conditions.push(selectorSql);
19
- }
20
- }
21
- const where = conditions.length > 0 ? `WHERE ${conditions.join(" AND ")}` : "";
22
- let orderBy = "";
23
- if (query.sort && query.sort.length > 0) {
24
- const orderParts = query.sort.map((sortPart) => {
25
- const entries = Object.entries(sortPart);
26
- if (entries.length === 0) return "";
27
- const [field, direction] = entries[0];
28
- const jsonPath = fieldToJsonExtract(field);
29
- return `${jsonPath} ${direction === "desc" ? "DESC" : "ASC"}`;
30
- }).filter(Boolean);
31
- if (orderParts.length > 0) {
32
- orderBy = `ORDER BY ${orderParts.join(", ")}`;
33
- }
34
- }
35
- const limitParams = [];
36
- let limit = "";
37
- if (query.limit !== void 0 && query.limit !== null) {
38
- limit = "LIMIT ?";
39
- limitParams.push(query.limit);
40
- }
41
- if (query.skip && query.skip > 0) {
42
- if (!limit) {
43
- limit = "LIMIT -1";
44
- }
45
- limit += " OFFSET ?";
46
- limitParams.push(query.skip);
47
- }
48
- return { where, params, orderBy, limit, limitParams };
49
- }
50
- function buildQuerySQL(tableName, query) {
51
- const result = mangoQueryToSQL(query);
52
- const allParams = [...result.params, ...result.limitParams];
53
- const parts = [
54
- `SELECT data FROM "${tableName}"`,
55
- result.where,
56
- result.orderBy,
57
- result.limit
58
- ].filter(Boolean);
59
- return { sql: parts.join(" "), params: allParams };
60
- }
61
- function buildCountSQL(tableName, query) {
62
- const result = mangoQueryToSQL(query);
63
- const parts = [
64
- `SELECT COUNT(*) as count FROM "${tableName}"`,
65
- result.where
66
- ].filter(Boolean);
67
- return { sql: parts.join(" "), params: result.params };
68
- }
69
- function fieldToJsonExtract(field) {
70
- return `json_extract(data, '$.${field}')`;
71
- }
72
- function selectorToSQL(selector, params) {
73
- const conditions = [];
74
- for (const [key, value] of Object.entries(selector)) {
75
- if (key === "$and") {
76
- const andConditions = value.map(
77
- (sub) => selectorToSQL(sub, params)
78
- ).filter(Boolean);
79
- if (andConditions.length > 0) {
80
- conditions.push(`(${andConditions.join(" AND ")})`);
81
- }
82
- } else if (key === "$or") {
83
- const orConditions = value.map(
84
- (sub) => selectorToSQL(sub, params)
85
- ).filter(Boolean);
86
- if (orConditions.length > 0) {
87
- conditions.push(`(${orConditions.join(" OR ")})`);
88
- }
89
- } else {
90
- const fieldSql = fieldConditionToSQL(key, value, params);
91
- if (fieldSql) {
92
- conditions.push(fieldSql);
93
- }
94
- }
95
- }
96
- return conditions.join(" AND ");
97
- }
98
- function fieldConditionToSQL(field, value, params) {
99
- const jsonPath = fieldToJsonExtract(field);
100
- if (value === null || value === void 0 || typeof value !== "object" || Array.isArray(value)) {
101
- params.push(value);
102
- return `${jsonPath} = ?`;
103
- }
104
- const conditions = [];
105
- for (const [op, opValue] of Object.entries(value)) {
106
- switch (op) {
107
- case "$eq":
108
- params.push(opValue);
109
- conditions.push(`${jsonPath} = ?`);
110
- break;
111
- case "$ne":
112
- params.push(opValue);
113
- conditions.push(`${jsonPath} != ?`);
114
- break;
115
- case "$gt":
116
- params.push(opValue);
117
- conditions.push(`${jsonPath} > ?`);
118
- break;
119
- case "$gte":
120
- params.push(opValue);
121
- conditions.push(`${jsonPath} >= ?`);
122
- break;
123
- case "$lt":
124
- params.push(opValue);
125
- conditions.push(`${jsonPath} < ?`);
126
- break;
127
- case "$lte":
128
- params.push(opValue);
129
- conditions.push(`${jsonPath} <= ?`);
130
- break;
131
- case "$in": {
132
- const arr = opValue;
133
- if (arr.length === 0) {
134
- conditions.push("0");
135
- } else {
136
- const placeholders = arr.map(() => "?").join(", ");
137
- params.push(...arr);
138
- conditions.push(`${jsonPath} IN (${placeholders})`);
139
- }
140
- break;
141
- }
142
- case "$nin": {
143
- const arr = opValue;
144
- if (arr.length === 0) {
145
- } else {
146
- const placeholders = arr.map(() => "?").join(", ");
147
- params.push(...arr);
148
- conditions.push(`${jsonPath} NOT IN (${placeholders})`);
149
- }
150
- break;
151
- }
152
- case "$regex": {
153
- const regexStr = typeof opValue === "string" ? opValue : opValue.source;
154
- const likePattern = regexToLike(regexStr);
155
- params.push(likePattern);
156
- conditions.push(`${jsonPath} LIKE ?`);
157
- break;
158
- }
159
- default:
160
- break;
161
- }
162
- }
163
- return conditions.join(" AND ");
164
- }
165
- function regexToLike(regex) {
166
- let pattern = regex;
167
- let prefix = "%";
168
- let suffix = "%";
169
- if (pattern.startsWith("^")) {
170
- prefix = "";
171
- pattern = pattern.slice(1);
172
- }
173
- if (pattern.endsWith("$")) {
174
- suffix = "";
175
- pattern = pattern.slice(0, -1);
176
- }
177
- pattern = pattern.replace(/\.\*/g, "%");
178
- pattern = pattern.replace(/\./g, "_");
179
- return `${prefix}${pattern}${suffix}`;
180
- }
181
-
182
- // src/storage-instance.ts
183
- function sanitizeTableName(databaseName, collectionName, schemaVersion) {
184
- return `${databaseName}_${collectionName}_v${schemaVersion}`.replace(/[^a-zA-Z0-9_]/g, "_");
185
- }
186
- var RxStorageInstanceSQLite = class {
187
- constructor(storage, params, database) {
188
- this.storage = storage;
189
- this.database = database;
190
- this.databaseName = params.databaseName;
191
- this.collectionName = params.collectionName;
192
- this.schema = params.schema;
193
- this.options = params.options;
194
- this.primaryPath = typeof params.schema.primaryKey === "string" ? params.schema.primaryKey : params.schema.primaryKey.key;
195
- const tableName = sanitizeTableName(params.databaseName, params.collectionName, params.schema.version);
196
- this.internals = { database, tableName };
197
- this.createTable();
198
- }
199
- databaseName;
200
- collectionName;
201
- schema;
202
- internals;
203
- options;
204
- primaryPath;
205
- changes$ = new Subject();
206
- closed = false;
207
- /**
208
- * Create the SQLite table for this collection if it does not exist.
209
- * Stores documents as JSON with indexed columns for _deleted, _rev, _meta.lwt and the primary key.
210
- */
211
- createTable() {
212
- this.database.execSync(
213
- `CREATE TABLE IF NOT EXISTS "${this.internals.tableName}" ( id TEXT PRIMARY KEY NOT NULL, data TEXT NOT NULL, _deleted INTEGER NOT NULL DEFAULT 0, _meta_lwt REAL NOT NULL DEFAULT 0)`
214
- );
215
- }
216
- /**
217
- * Read documents from SQLite by their primary keys.
218
- * Returns a Map<primaryKey, RxDocumentData> for use with categorizeBulkWriteRows.
219
- */
220
- getDocsByIdMap(ids) {
221
- const map = /* @__PURE__ */ new Map();
222
- if (ids.length === 0) return map;
223
- const placeholders = ids.map(() => "?").join(",");
224
- const rows = this.database.getAllSync(
225
- `SELECT id, data FROM "${this.internals.tableName}" WHERE id IN (${placeholders})`,
226
- ids
227
- );
228
- for (const row of rows) {
229
- const doc = JSON.parse(row.data);
230
- map.set(row.id, doc);
231
- }
232
- return map;
233
- }
234
- async bulkWrite(documentWrites, context) {
235
- if (this.closed) {
236
- throw new Error("RxStorageInstanceSQLite: storage is closed");
237
- }
238
- const ids = documentWrites.map(
239
- (row) => row.document[this.primaryPath]
240
- );
241
- const docsInDb = this.getDocsByIdMap(ids);
242
- const categorized = categorizeBulkWriteRows(
243
- this,
244
- this.primaryPath,
245
- docsInDb,
246
- documentWrites,
247
- context
248
- );
249
- const error = categorized.errors;
250
- for (const row of categorized.bulkInsertDocs) {
251
- const doc = row.document;
252
- const docId = doc[this.primaryPath];
253
- const json = JSON.stringify(doc);
254
- this.database.runSync(
255
- `INSERT OR REPLACE INTO "${this.internals.tableName}" (id, data, _deleted, _meta_lwt) VALUES (?, ?, ?, ?)`,
256
- [docId, json, doc._deleted ? 1 : 0, doc._meta.lwt]
257
- );
258
- }
259
- for (const row of categorized.bulkUpdateDocs) {
260
- const doc = row.document;
261
- const docId = doc[this.primaryPath];
262
- const json = JSON.stringify(doc);
263
- this.database.runSync(
264
- `UPDATE "${this.internals.tableName}" SET data = ?, _deleted = ?, _meta_lwt = ? WHERE id = ?`,
265
- [json, doc._deleted ? 1 : 0, doc._meta.lwt, docId]
266
- );
267
- }
268
- if (categorized.eventBulk.events.length > 0) {
269
- const lastState = ensureNotFalsy(categorized.newestRow).document;
270
- categorized.eventBulk.checkpoint = {
271
- id: lastState[this.primaryPath],
272
- lwt: lastState._meta.lwt
273
- };
274
- this.changes$.next(categorized.eventBulk);
275
- }
276
- return { error };
277
- }
278
- async findDocumentsById(ids, withDeleted) {
279
- if (ids.length === 0) return [];
280
- const placeholders = ids.map(() => "?").join(",");
281
- let sql = `SELECT data FROM "${this.internals.tableName}" WHERE id IN (${placeholders})`;
282
- if (!withDeleted) {
283
- sql += " AND _deleted = 0";
284
- }
285
- const rows = this.database.getAllSync(sql, ids);
286
- return rows.map((row) => JSON.parse(row.data));
287
- }
288
- async query(preparedQuery) {
289
- const { query } = preparedQuery;
290
- try {
291
- const { sql, params } = buildQuerySQL(this.internals.tableName, query);
292
- const rows = this.database.getAllSync(sql, params);
293
- const documents = rows.map(
294
- (row) => JSON.parse(row.data)
295
- );
296
- return { documents };
297
- } catch {
298
- return this.queryInMemory(query);
299
- }
300
- }
301
- async count(preparedQuery) {
302
- const { query } = preparedQuery;
303
- try {
304
- const { sql, params } = buildCountSQL(this.internals.tableName, query);
305
- const rows = this.database.getAllSync(sql, params);
306
- if (rows.length > 0 && typeof rows[0].count === "number") {
307
- return { count: rows[0].count, mode: "fast" };
308
- }
309
- } catch {
310
- }
311
- const result = await this.queryInMemory(query);
312
- return { count: result.documents.length, mode: "slow" };
313
- }
314
- /**
315
- * In-memory query fallback using RxDB's built-in query matcher and sort comparator.
316
- * Used when the Mango-to-SQL translation fails (e.g., unsupported operators).
317
- */
318
- async queryInMemory(query) {
319
- const rows = this.database.getAllSync(
320
- `SELECT data FROM "${this.internals.tableName}" WHERE _deleted = 0`
321
- );
322
- let documents = rows.map(
323
- (row) => JSON.parse(row.data)
324
- );
325
- const queryMatcher = getQueryMatcher(this.schema, query);
326
- documents = documents.filter((doc) => queryMatcher(doc));
327
- const sortComparator = getSortComparator(this.schema, query);
328
- documents.sort(sortComparator);
329
- const skip = query.skip ?? 0;
330
- const limit = query.limit ?? Infinity;
331
- documents = documents.slice(skip, skip + limit);
332
- return { documents };
333
- }
334
- getAttachmentData(_documentId, _attachmentId, _digest) {
335
- throw new Error("Attachments not supported by SQLite storage");
336
- }
337
- async getChangedDocumentsSince(limit, checkpoint) {
338
- let sql;
339
- let params;
340
- if (checkpoint) {
341
- sql = `SELECT data FROM "${this.internals.tableName}" WHERE (_meta_lwt > ?) OR (_meta_lwt = ? AND id > ?) ORDER BY _meta_lwt ASC, id ASC LIMIT ?`;
342
- params = [checkpoint.lwt, checkpoint.lwt, checkpoint.id, limit];
343
- } else {
344
- sql = `SELECT data FROM "${this.internals.tableName}" ORDER BY _meta_lwt ASC, id ASC LIMIT ?`;
345
- params = [limit];
346
- }
347
- const rows = this.database.getAllSync(sql, params);
348
- const documents = rows.map(
349
- (row) => JSON.parse(row.data)
350
- );
351
- const lastDoc = documents.length > 0 ? documents[documents.length - 1] : void 0;
352
- const newCheckpoint = lastDoc ? {
353
- id: lastDoc[this.primaryPath],
354
- lwt: lastDoc._meta.lwt
355
- } : checkpoint ?? { id: "", lwt: 0 };
356
- return {
357
- documents,
358
- checkpoint: newCheckpoint
359
- };
360
- }
361
- changeStream() {
362
- return this.changes$.asObservable();
363
- }
364
- async cleanup(minimumDeletedTime) {
365
- const maxDeletionTime = now() - minimumDeletedTime;
366
- this.database.runSync(
367
- `DELETE FROM "${this.internals.tableName}" WHERE _deleted = 1 AND _meta_lwt < ?`,
368
- [maxDeletionTime]
369
- );
370
- return true;
371
- }
372
- async close() {
373
- if (this.closed) return;
374
- this.closed = true;
375
- this.changes$.complete();
376
- }
377
- async remove() {
378
- this.database.execSync(`DROP TABLE IF EXISTS "${this.internals.tableName}"`);
379
- await this.close();
380
- }
381
- };
382
- async function createSQLiteStorageInstance(storage, params, database) {
383
- const instance = new RxStorageInstanceSQLite(storage, params, database);
384
- return instance;
385
- }
1
+ import {
2
+ sqliteBoolParams
3
+ } from "./chunk-LBITF2LV.js";
386
4
 
387
5
  // src/rx-storage-sqlite.ts
6
+ import { getRxStorageSQLite as getPremiumRxStorageSQLite } from "rxdb-premium/plugins/storage-sqlite";
388
7
  function getRxStorageSQLite(database) {
389
- const storage = {
390
- name: "sqlite",
391
- rxdbVersion: RXDB_VERSION,
392
- createStorageInstance(params) {
393
- ensureRxStorageInstanceParamsAreCorrect(params);
394
- return createSQLiteStorageInstance(storage, params, database);
8
+ let databaseName;
9
+ const sqliteBasics = {
10
+ debugId: "tallyui-sqlite-sync",
11
+ journalMode: "WAL",
12
+ open: (name) => {
13
+ if (databaseName !== void 0 && databaseName !== name) {
14
+ throw new Error(`one SQLite handle serves exactly one RxDB database: "${databaseName}" and "${name}"`);
15
+ }
16
+ databaseName = name;
17
+ return Promise.resolve(database);
18
+ },
19
+ all: async (db, q) => {
20
+ return db.getAllSync(q.query, sqliteBoolParams(q.params));
21
+ },
22
+ run: async (db, q) => {
23
+ db.runSync(q.query, sqliteBoolParams(q.params));
24
+ },
25
+ setPragma: async (db, key, value) => {
26
+ db.execSync("pragma " + key + " = " + value + ";");
27
+ },
28
+ close: async () => {
395
29
  }
396
30
  };
397
- return storage;
31
+ return getPremiumRxStorageSQLite({ sqliteBasics });
398
32
  }
399
33
  export {
400
34
  getRxStorageSQLite
@@ -0,0 +1,43 @@
1
+ import { RxStorage } from 'rxdb';
2
+
3
+ /**
4
+ * Thrown by the web SQLite worker's storage (ADR-061) when start-up failed:
5
+ * another tab may hold the opfs-sahpool database, or this browser has no
6
+ * OPFS or sync access handles. Keeps the original failure as `cause`.
7
+ *
8
+ * RxDB's remote-storage channel re-throws a rejected `createStorageInstance`
9
+ * on the main thread as a plain `Error` whose message embeds the original
10
+ * error's `name` as JSON, since `name` itself does not survive the trip.
11
+ * `isStorageWorkerStartError` matches that fixed substring in the message
12
+ * too, for whichever of the error, a `{ name, message }` copy, or the
13
+ * message alone actually arrives.
14
+ */
15
+ declare class StorageWorkerStartError extends Error {
16
+ constructor(message: string, options?: {
17
+ cause?: unknown;
18
+ });
19
+ }
20
+ declare function isStorageWorkerStartError(error: unknown): boolean;
21
+
22
+ /** Marks a storage as this package's web engine (ADR-061), for job 2b to recognise. */
23
+ declare const SQLITE_SAHPOOL_ENGINE = "sqlite-sahpool";
24
+ /** The web storage, with `terminate()` for the live-tab park order (ADR-061, amendment 1). */
25
+ type RxStorageSQLiteWasm = RxStorage<any, any> & {
26
+ tallyEngine: string;
27
+ /**
28
+ * Terminates the storage worker, which releases the opfs-sahpool access
29
+ * handles, and evicts its channel from rxdb's cache, so a new storage built
30
+ * the same way starts a fresh worker. Call it after the databases close.
31
+ * Idempotent. Build one storage per page life (ADR-061): a second storage
32
+ * from the same input while the first is alive shares its worker, and only
33
+ * the storage that created the worker can terminate it.
34
+ */
35
+ terminate(): void;
36
+ };
37
+ /** The web storage (ADR-061): premium SQLite on sqlite-wasm opfs-sahpool, in one dedicated worker. */
38
+ declare function getRxStorageSQLiteWasm(options: {
39
+ workerInput: string | URL | (() => Worker);
40
+ workerName?: string;
41
+ }): RxStorageSQLiteWasm;
42
+
43
+ export { type RxStorageSQLiteWasm, SQLITE_SAHPOOL_ENGINE, StorageWorkerStartError, getRxStorageSQLiteWasm, isStorageWorkerStartError };