@tallyui/storage-sqlite 0.2.0 → 3.0.0-next.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 +21 -0
- package/README.md +168 -0
- package/dist/chunk-3OYD5CSS.js +32 -0
- package/dist/chunk-LBITF2LV.js +8 -0
- package/dist/index.d.ts +9 -12
- package/dist/index.js +29 -392
- package/dist/web/index.d.ts +71 -0
- package/dist/web/index.js +62 -0
- package/dist/web/worker.d.ts +2 -0
- package/dist/web/worker.js +106 -0
- package/package.json +45 -6
- package/scripts/build-web-worker.mjs +77 -0
- package/src/handle-types.test-d.ts +17 -0
- package/src/params.ts +9 -0
- package/src/rx-storage-sqlite.ts +41 -19
- package/src/types.ts +4 -2
- package/src/web/errors.ts +51 -0
- package/src/web/index.ts +71 -0
- package/src/web/is-storage-worker-start-error.ts +20 -0
- package/src/web/sqlite-basics-sahpool.ts +75 -0
- package/src/web/worker.ts +84 -0
- package/src/integration.test.ts +0 -169
- package/src/mango-to-sql.test.ts +0 -239
- package/src/mango-to-sql.ts +0 -274
- package/src/mock-sqlite.ts +0 -566
- package/src/storage-instance.test.ts +0 -265
- package/src/storage-instance.ts +0 -333
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,168 @@
|
|
|
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
|
+
## Upgrading to 3.0.0
|
|
14
|
+
|
|
15
|
+
3.0.0 moves to RxDB and RxDB Premium 17.5.0 (with `@tallyui/pos` and
|
|
16
|
+
`@tallyui/database` 3.0.0). See ADR-069 in `docs/DECISIONS.md`.
|
|
17
|
+
|
|
18
|
+
- **Storage is one-way.** Once a till has opened 3.0.0, an older build shows
|
|
19
|
+
no orders and sends none of the pending ones until the till is upgraded
|
|
20
|
+
again. Nothing is deleted. Never roll an app back across 3.0.0, and never
|
|
21
|
+
re-ring the sales it hides.
|
|
22
|
+
- A web app ships the 17.5.0 storage worker with the 17.5.0 main thread.
|
|
23
|
+
- Pin `rxdb` and `rxdb-premium` to exactly `17.5.0`.
|
|
24
|
+
- RxDB 17 defaults `toggleOnDocumentVisible` to true: a replication no longer
|
|
25
|
+
keeps a hidden tab awake, so a browser may throttle its pull (see the
|
|
26
|
+
changeset for the detail).
|
|
27
|
+
|
|
28
|
+
## Supported handles
|
|
29
|
+
|
|
30
|
+
- Native: expo-sqlite 16's `SQLiteDatabase` from `openDatabaseSync`, checked at
|
|
31
|
+
compile time by `typecheck/expo-handle.ts`. The peer range allows 15, which
|
|
32
|
+
isn't checked.
|
|
33
|
+
- Node: a `node:sqlite` `DatabaseSync` wrapped like `src/node-sqlite.test-helper.ts`.
|
|
34
|
+
- Web: `getRxStorageSQLiteWasm` takes `workerInput`; the worker opens its own
|
|
35
|
+
SQLite-wasm `opfs-sahpool` handle, so callers do not supply a database handle.
|
|
36
|
+
|
|
37
|
+
Any handle with `execSync`, `getAllSync`, and `runSync` matching the minimal
|
|
38
|
+
`SQLiteDatabase` interface works with `getRxStorageSQLite`.
|
|
39
|
+
|
|
40
|
+
## Web (SQLite-wasm)
|
|
41
|
+
|
|
42
|
+
`getRxStorageSQLiteWasm` (from `@tallyui/storage-sqlite/web`) runs SQLite-wasm
|
|
43
|
+
on `opfs-sahpool` inside one dedicated worker, reached through RxDB Premium's
|
|
44
|
+
`getRxStorageWorker` in mode `'one'`. `createSyncAccessHandle`, which
|
|
45
|
+
opfs-sahpool needs, exists only inside a dedicated worker, and opfs-sahpool
|
|
46
|
+
holds exclusive OPFS access handles for the whole origin — so the app must
|
|
47
|
+
run **exactly one live tab, one dedicated worker** per store (ADR-061 in
|
|
48
|
+
`docs/DECISIONS.md`); a second tab cannot open storage at all, and the
|
|
49
|
+
recovery from a dead or hung worker is a reload.
|
|
50
|
+
|
|
51
|
+
The app supplies the worker itself, as `workerInput`:
|
|
52
|
+
|
|
53
|
+
```ts
|
|
54
|
+
import { getRxStorageSQLiteWasm } from '@tallyui/storage-sqlite/web';
|
|
55
|
+
|
|
56
|
+
const storage = getRxStorageSQLiteWasm({ workerInput /* see below */ });
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
### Vite
|
|
60
|
+
|
|
61
|
+
Pass a module-worker factory as `workerInput`:
|
|
62
|
+
|
|
63
|
+
```ts
|
|
64
|
+
const storage = getRxStorageSQLiteWasm({
|
|
65
|
+
workerInput: () =>
|
|
66
|
+
new Worker(new URL('@tallyui/storage-sqlite/web-worker', import.meta.url), { type: 'module' }),
|
|
67
|
+
});
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
and exclude the wasm package from dependency pre-bundling, so Vite emits
|
|
71
|
+
`sqlite3.wasm` as a static asset instead of trying to bundle it:
|
|
72
|
+
|
|
73
|
+
```ts
|
|
74
|
+
// vite.config.ts
|
|
75
|
+
export default defineConfig({
|
|
76
|
+
optimizeDeps: { exclude: ['@sqlite.org/sqlite-wasm'] },
|
|
77
|
+
});
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
### Expo web (Metro)
|
|
81
|
+
|
|
82
|
+
Metro cannot bundle a module worker, so a Metro app prebuilds the
|
|
83
|
+
`@tallyui/storage-sqlite/web-worker` entry, together with `sqlite3.wasm`,
|
|
84
|
+
into the app's `public/` folder, as part of the app's own build:
|
|
85
|
+
|
|
86
|
+
```sh
|
|
87
|
+
npm install --save-dev esbuild
|
|
88
|
+
npx tallyui-build-sqlite-worker public/
|
|
89
|
+
```
|
|
90
|
+
|
|
91
|
+
Add the built files to the app's `.gitignore` (they link rxdb-premium code
|
|
92
|
+
under the app's own licence, so they must never be committed or published):
|
|
93
|
+
|
|
94
|
+
```
|
|
95
|
+
public/tallyui-sqlite-worker.js
|
|
96
|
+
public/sqlite3.wasm
|
|
97
|
+
```
|
|
98
|
+
|
|
99
|
+
and pass the built file's string URL as `workerInput`:
|
|
100
|
+
|
|
101
|
+
```ts
|
|
102
|
+
const storage = getRxStorageSQLiteWasm({ workerInput: '/tallyui-sqlite-worker.js' });
|
|
103
|
+
```
|
|
104
|
+
|
|
105
|
+
WCPOS `next` prebuilds its own worker the same way, as part of its own build
|
|
106
|
+
step.
|
|
107
|
+
|
|
108
|
+
`workerOptions` (the module type and the worker's debug name) only applies
|
|
109
|
+
when `workerInput` is a string or URL — RxDB Premium then constructs the
|
|
110
|
+
`Worker` itself. When `workerInput` is a function, as in the Vite example,
|
|
111
|
+
the function creates the `Worker`, so `workerOptions` is ignored; pass
|
|
112
|
+
`{ type: 'module' }` (and a name, if wanted) to the constructor call inside
|
|
113
|
+
the function instead.
|
|
114
|
+
|
|
115
|
+
### Recognising a failed start
|
|
116
|
+
|
|
117
|
+
If the worker's start-up fails, every storage call rejects instead of
|
|
118
|
+
hanging. Each error carries its cause's name and message in its own message,
|
|
119
|
+
since the cause itself doesn't cross the worker channel. Three failures have
|
|
120
|
+
their own predicate, and their own sentence for the cashier:
|
|
121
|
+
|
|
122
|
+
- **Storage unavailable: `isStorageUnavailableError`** (a
|
|
123
|
+
`StorageUnavailableError`). The browser gives the page no usable OPFS:
|
|
124
|
+
`navigator.storage.getDirectory()` fails, or sync access handles are
|
|
125
|
+
missing. This is what happens in a **private window**: Safari Private
|
|
126
|
+
Browsing (WebKit's ephemeral storage, #293) refuses OPFS. No other tab is
|
|
127
|
+
involved, and neither closing tabs nor reloading helps.
|
|
128
|
+
- **Held by another tab: `isStorageHeldError`** (a `StorageWorkerStartError`
|
|
129
|
+
whose message says another tab holds the database). OPFS is reachable, but
|
|
130
|
+
another tab holds the opfs-sahpool database (WebKit's `InvalidStateError`,
|
|
131
|
+
Chromium's `NoModificationAllowedError`).
|
|
132
|
+
- **Stale worker: `isRxdbRemoteVersionMismatch`** from `@tallyui/core`
|
|
133
|
+
(RxDB's RM1, #280). After an upgrade the browser may still have the old
|
|
134
|
+
worker cached, and RxDB refuses it; a reload loads the matching worker.
|
|
135
|
+
|
|
136
|
+
Any other start failure is a generic `StorageWorkerStartError` ("SQLite
|
|
137
|
+
worker start failed", with its cause). `isStorageWorkerStartError` is true
|
|
138
|
+
for any failed start except storage unavailable; use the three predicates
|
|
139
|
+
above to choose the wording, in this order:
|
|
140
|
+
|
|
141
|
+
```ts
|
|
142
|
+
import { isRxdbRemoteVersionMismatch } from '@tallyui/core';
|
|
143
|
+
import { isStorageHeldError, isStorageUnavailableError } from '@tallyui/storage-sqlite/web';
|
|
144
|
+
|
|
145
|
+
try {
|
|
146
|
+
await db.addCollections({ /* ... */ });
|
|
147
|
+
} catch (error) {
|
|
148
|
+
if (isStorageUnavailableError(error)) {
|
|
149
|
+
showMessage(
|
|
150
|
+
"This till can't save sales in a private window.",
|
|
151
|
+
'Open it in a normal Safari window (or another browser) and sign in again. Nothing has been lost: no sale was taken here.'
|
|
152
|
+
);
|
|
153
|
+
} else if (isStorageHeldError(error)) {
|
|
154
|
+
showMessage('This till is already open in another tab. Close the other tab, then reload this one.');
|
|
155
|
+
} else if (isRxdbRemoteVersionMismatch(error)) {
|
|
156
|
+
showMessage('This till needs a quick reload to finish updating. Reload the page.');
|
|
157
|
+
} else {
|
|
158
|
+
throw error; // or the app's own generic failure message
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
```
|
|
162
|
+
|
|
163
|
+
### Single live tab
|
|
164
|
+
|
|
165
|
+
A second tab must never open storage while the first one holds it; see
|
|
166
|
+
ADR-061 in `docs/DECISIONS.md` for the accepted take-over flow (a
|
|
167
|
+
BroadcastChannel hand-over with a timeout, and a parked "POS is open in
|
|
168
|
+
another tab" screen), which is a separate job from this package.
|
|
@@ -0,0 +1,32 @@
|
|
|
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
|
+
var StorageUnavailableError = class extends Error {
|
|
9
|
+
constructor(message, options) {
|
|
10
|
+
super(message, options);
|
|
11
|
+
this.name = "StorageUnavailableError";
|
|
12
|
+
}
|
|
13
|
+
};
|
|
14
|
+
var STORAGE_HELD_MARKER = "another tab holds the database";
|
|
15
|
+
function isStorageHeldError(error) {
|
|
16
|
+
const text = typeof error === "string" ? error : error?.message;
|
|
17
|
+
return typeof text === "string" && text.includes("StorageWorkerStartError") && text.includes(STORAGE_HELD_MARKER);
|
|
18
|
+
}
|
|
19
|
+
function isStorageUnavailableError(error) {
|
|
20
|
+
if (typeof error === "string") return error.includes("StorageUnavailableError");
|
|
21
|
+
if (!error || typeof error !== "object") return false;
|
|
22
|
+
const { name, message } = error;
|
|
23
|
+
return name === "StorageUnavailableError" || typeof message === "string" && message.includes("StorageUnavailableError");
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export {
|
|
27
|
+
StorageWorkerStartError,
|
|
28
|
+
StorageUnavailableError,
|
|
29
|
+
STORAGE_HELD_MARKER,
|
|
30
|
+
isStorageHeldError,
|
|
31
|
+
isStorageUnavailableError
|
|
32
|
+
};
|
package/dist/index.d.ts
CHANGED
|
@@ -1,23 +1,20 @@
|
|
|
1
1
|
import { RxStorage } from 'rxdb';
|
|
2
|
+
import { SQLResultRow } from 'rxdb/plugins/storage-sqlite';
|
|
2
3
|
|
|
3
4
|
interface SQLiteDatabase {
|
|
4
5
|
execSync(source: string): void;
|
|
5
|
-
getAllSync
|
|
6
|
-
runSync(source: string, params
|
|
7
|
-
changes: number;
|
|
8
|
-
lastInsertRowId: number;
|
|
9
|
-
};
|
|
6
|
+
getAllSync(source: string, params: (string | number)[]): SQLResultRow[];
|
|
7
|
+
runSync(source: string, params: (string | number)[]): unknown;
|
|
10
8
|
}
|
|
11
9
|
interface SQLiteStorageSettings {
|
|
12
10
|
database: SQLiteDatabase;
|
|
13
11
|
}
|
|
14
12
|
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
declare function getRxStorageSQLite(database: SQLiteDatabase): RxStorageSQLite;
|
|
13
|
+
/**
|
|
14
|
+
* Wraps a synchronous SQLite handle with RxDB Premium SQLite storage.
|
|
15
|
+
* Apps install the RxDB Premium peer dependency under their own RxDB Premium licence.
|
|
16
|
+
* One SQLite handle serves exactly one RxDB database.
|
|
17
|
+
*/
|
|
18
|
+
declare function getRxStorageSQLite(database: SQLiteDatabase): RxStorage<any, any>;
|
|
22
19
|
|
|
23
20
|
export { type SQLiteDatabase, type SQLiteStorageSettings, getRxStorageSQLite };
|
package/dist/index.js
CHANGED
|
@@ -1,400 +1,37 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
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 { setPremiumFlag } from "rxdb-premium/plugins/shared";
|
|
7
|
+
import { getRxStorageSQLite as getPremiumRxStorageSQLite } from "rxdb-premium/plugins/storage-sqlite";
|
|
8
|
+
setPremiumFlag();
|
|
388
9
|
function getRxStorageSQLite(database) {
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
10
|
+
setPremiumFlag();
|
|
11
|
+
let databaseName;
|
|
12
|
+
const sqliteBasics = {
|
|
13
|
+
debugId: "tallyui-sqlite-sync",
|
|
14
|
+
journalMode: "WAL",
|
|
15
|
+
open: (name) => {
|
|
16
|
+
if (databaseName !== void 0 && databaseName !== name) {
|
|
17
|
+
throw new Error(`one SQLite handle serves exactly one RxDB database: "${databaseName}" and "${name}"`);
|
|
18
|
+
}
|
|
19
|
+
databaseName = name;
|
|
20
|
+
return Promise.resolve(database);
|
|
21
|
+
},
|
|
22
|
+
all: async (db, q) => {
|
|
23
|
+
return db.getAllSync(q.query, sqliteBoolParams(q.params));
|
|
24
|
+
},
|
|
25
|
+
run: async (db, q) => {
|
|
26
|
+
db.runSync(q.query, sqliteBoolParams(q.params));
|
|
27
|
+
},
|
|
28
|
+
setPragma: async (db, key, value) => {
|
|
29
|
+
db.execSync("pragma " + key + " = " + value + ";");
|
|
30
|
+
},
|
|
31
|
+
close: async () => {
|
|
395
32
|
}
|
|
396
33
|
};
|
|
397
|
-
return
|
|
34
|
+
return getPremiumRxStorageSQLite({ sqliteBasics });
|
|
398
35
|
}
|
|
399
36
|
export {
|
|
400
37
|
getRxStorageSQLite
|