@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 +21 -0
- package/README.md +115 -0
- package/dist/chunk-CNKWKIZV.js +18 -0
- package/dist/chunk-LBITF2LV.js +8 -0
- package/dist/index.d.ts +6 -7
- package/dist/index.js +26 -392
- package/dist/web/index.d.ts +43 -0
- package/dist/web/index.js +42 -0
- package/dist/web/worker.d.ts +2 -0
- package/dist/web/worker.js +90 -0
- package/package.json +40 -4
- package/scripts/build-web-worker.mjs +77 -0
- package/src/params.ts +9 -0
- package/src/rx-storage-sqlite.ts +34 -20
- package/src/web/errors.ts +25 -0
- package/src/web/index.ts +62 -0
- package/src/web/sqlite-basics-sahpool.ts +75 -0
- package/src/web/worker.ts +60 -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
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import {
|
|
2
|
+
StorageWorkerStartError,
|
|
3
|
+
isStorageWorkerStartError
|
|
4
|
+
} from "../chunk-CNKWKIZV.js";
|
|
5
|
+
|
|
6
|
+
// src/web/index.ts
|
|
7
|
+
import { MESSAGE_CHANNEL_CACHE_BY_IDENTIFIER, OPEN_REMOTE_MESSAGE_CHANNELS } from "rxdb/plugins/storage-remote";
|
|
8
|
+
import { getRxStorageWorker } from "rxdb-premium/plugins/storage-worker";
|
|
9
|
+
var SQLITE_SAHPOOL_ENGINE = "sqlite-sahpool";
|
|
10
|
+
function getRxStorageSQLiteWasm(options) {
|
|
11
|
+
const { workerInput } = options;
|
|
12
|
+
const workerOptions = { type: "module", name: options.workerName ?? "tallyui-storage" };
|
|
13
|
+
let worker;
|
|
14
|
+
const createWorker = () => worker = typeof workerInput === "function" ? workerInput() : new Worker(workerInput, workerOptions);
|
|
15
|
+
createWorker.toString = () => String(workerInput);
|
|
16
|
+
const storage = getRxStorageWorker({ workerInput: createWorker, workerOptions, mode: "one" });
|
|
17
|
+
let terminated = false;
|
|
18
|
+
return Object.assign(storage, {
|
|
19
|
+
tallyEngine: SQLITE_SAHPOOL_ENGINE,
|
|
20
|
+
terminate() {
|
|
21
|
+
if (terminated) return;
|
|
22
|
+
terminated = true;
|
|
23
|
+
worker?.terminate();
|
|
24
|
+
const channel = storage.messageChannelIfOneMode;
|
|
25
|
+
const cache = MESSAGE_CHANNEL_CACHE_BY_IDENTIFIER.get(storage.settings.identifier);
|
|
26
|
+
cache?.forEach((item, key) => {
|
|
27
|
+
if (item.messageChannel === channel) cache.delete(key);
|
|
28
|
+
});
|
|
29
|
+
void channel?.then((open) => {
|
|
30
|
+
OPEN_REMOTE_MESSAGE_CHANNELS.delete(open);
|
|
31
|
+
return open.close();
|
|
32
|
+
}).catch(() => {
|
|
33
|
+
});
|
|
34
|
+
}
|
|
35
|
+
});
|
|
36
|
+
}
|
|
37
|
+
export {
|
|
38
|
+
SQLITE_SAHPOOL_ENGINE,
|
|
39
|
+
StorageWorkerStartError,
|
|
40
|
+
getRxStorageSQLiteWasm,
|
|
41
|
+
isStorageWorkerStartError
|
|
42
|
+
};
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
import {
|
|
2
|
+
sqliteBoolParams
|
|
3
|
+
} from "../chunk-LBITF2LV.js";
|
|
4
|
+
import {
|
|
5
|
+
StorageWorkerStartError
|
|
6
|
+
} from "../chunk-CNKWKIZV.js";
|
|
7
|
+
|
|
8
|
+
// src/web/worker.ts
|
|
9
|
+
import sqlite3InitModule from "@sqlite.org/sqlite-wasm";
|
|
10
|
+
import { getRxStorageSQLite } from "rxdb-premium/plugins/storage-sqlite";
|
|
11
|
+
import { exposeWorkerRxStorage } from "rxdb-premium/plugins/storage-worker";
|
|
12
|
+
|
|
13
|
+
// src/web/sqlite-basics-sahpool.ts
|
|
14
|
+
function getSQLiteBasicsOpfsSahPool({ openDb }) {
|
|
15
|
+
const handles = /* @__PURE__ */ new Map();
|
|
16
|
+
const namesByHandle = /* @__PURE__ */ new Map();
|
|
17
|
+
return {
|
|
18
|
+
debugId: "tallyui-sqlite-sahpool",
|
|
19
|
+
journalMode: "WAL",
|
|
20
|
+
open: async (name2) => {
|
|
21
|
+
const cached = handles.get(name2);
|
|
22
|
+
if (cached !== void 0) {
|
|
23
|
+
return cached;
|
|
24
|
+
}
|
|
25
|
+
const opening = (async () => {
|
|
26
|
+
const db = await openDb(name2);
|
|
27
|
+
db.exec({ sql: "PRAGMA locking_mode = exclusive" });
|
|
28
|
+
namesByHandle.set(db, name2);
|
|
29
|
+
return db;
|
|
30
|
+
})();
|
|
31
|
+
handles.set(name2, opening);
|
|
32
|
+
return opening;
|
|
33
|
+
},
|
|
34
|
+
all: async (db, q) => {
|
|
35
|
+
return db.exec({
|
|
36
|
+
sql: q.query,
|
|
37
|
+
bind: sqliteBoolParams(q.params),
|
|
38
|
+
rowMode: "object",
|
|
39
|
+
returnValue: "resultRows"
|
|
40
|
+
});
|
|
41
|
+
},
|
|
42
|
+
run: async (db, q) => {
|
|
43
|
+
db.exec({ sql: q.query, bind: sqliteBoolParams(q.params) });
|
|
44
|
+
},
|
|
45
|
+
setPragma: async (db, key, value) => {
|
|
46
|
+
db.exec({ sql: `pragma ${key} = ${value};` });
|
|
47
|
+
},
|
|
48
|
+
close: async (db) => {
|
|
49
|
+
const name2 = namesByHandle.get(db);
|
|
50
|
+
if (name2 !== void 0) {
|
|
51
|
+
handles.delete(name2);
|
|
52
|
+
namesByHandle.delete(db);
|
|
53
|
+
}
|
|
54
|
+
db.close();
|
|
55
|
+
}
|
|
56
|
+
};
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
// src/web/worker.ts
|
|
60
|
+
var { name, rxdbVersion } = getRxStorageSQLite({
|
|
61
|
+
sqliteBasics: getSQLiteBasicsOpfsSahPool({ openDb: () => Promise.reject(new Error("unused: throwaway instance")) })
|
|
62
|
+
});
|
|
63
|
+
var ready = sqlite3InitModule().then((sqlite3) => sqlite3.installOpfsSAHPoolVfs({ name: "tallyui", initialCapacity: 64 })).then(
|
|
64
|
+
(pool) => getRxStorageSQLite({
|
|
65
|
+
sqliteBasics: getSQLiteBasicsOpfsSahPool({
|
|
66
|
+
// The OO1 `exec()` here is the real, overloaded sqlite-wasm signature;
|
|
67
|
+
// `Oo1Db` is the minimal shape this package's adapters need from it.
|
|
68
|
+
openDb: async (dbName) => new pool.OpfsSAHPoolDb("/" + dbName)
|
|
69
|
+
})
|
|
70
|
+
})
|
|
71
|
+
);
|
|
72
|
+
ready.catch(() => {
|
|
73
|
+
});
|
|
74
|
+
var storage = {
|
|
75
|
+
name,
|
|
76
|
+
rxdbVersion,
|
|
77
|
+
createStorageInstance: async (params) => {
|
|
78
|
+
let realStorage;
|
|
79
|
+
try {
|
|
80
|
+
realStorage = await ready;
|
|
81
|
+
} catch (cause) {
|
|
82
|
+
throw new StorageWorkerStartError(
|
|
83
|
+
"SQLite worker start failed: another tab may hold the database, or this browser lacks OPFS.",
|
|
84
|
+
{ cause }
|
|
85
|
+
);
|
|
86
|
+
}
|
|
87
|
+
return realStorage.createStorageInstance(params);
|
|
88
|
+
}
|
|
89
|
+
};
|
|
90
|
+
exposeWorkerRxStorage({ storage });
|
package/package.json
CHANGED
|
@@ -1,29 +1,65 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tallyui/storage-sqlite",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "2.0.0",
|
|
4
4
|
"type": "module",
|
|
5
|
-
"description": "RxDB storage
|
|
5
|
+
"description": "RxDB Premium SQLite storage for a synchronous SQLite handle (expo-sqlite)",
|
|
6
6
|
"main": "./dist/index.js",
|
|
7
7
|
"types": "./dist/index.d.ts",
|
|
8
8
|
"source": "./src/index.ts",
|
|
9
|
+
"bin": {
|
|
10
|
+
"tallyui-build-sqlite-worker": "./scripts/build-web-worker.mjs"
|
|
11
|
+
},
|
|
9
12
|
"exports": {
|
|
10
13
|
".": {
|
|
11
14
|
"types": "./dist/index.d.ts",
|
|
12
15
|
"import": "./dist/index.js",
|
|
13
16
|
"source": "./src/index.ts"
|
|
17
|
+
},
|
|
18
|
+
"./web": {
|
|
19
|
+
"types": "./dist/web/index.d.ts",
|
|
20
|
+
"import": "./dist/web/index.js",
|
|
21
|
+
"source": "./src/web/index.ts"
|
|
22
|
+
},
|
|
23
|
+
"./web-worker": {
|
|
24
|
+
"types": "./dist/web/worker.d.ts",
|
|
25
|
+
"import": "./dist/web/worker.js",
|
|
26
|
+
"source": "./src/web/worker.ts"
|
|
14
27
|
}
|
|
15
28
|
},
|
|
16
29
|
"files": [
|
|
17
30
|
"dist",
|
|
18
|
-
"src"
|
|
31
|
+
"src",
|
|
32
|
+
"!src/**/*.test.ts",
|
|
33
|
+
"!src/**/*.test-helper.ts",
|
|
34
|
+
"scripts/*.mjs"
|
|
19
35
|
],
|
|
20
36
|
"license": "MIT",
|
|
37
|
+
"repository": {
|
|
38
|
+
"type": "git",
|
|
39
|
+
"url": "git+https://github.com/TallyUI/tallyui.git",
|
|
40
|
+
"directory": "packages/storage-sqlite"
|
|
41
|
+
},
|
|
21
42
|
"dependencies": {
|
|
22
43
|
"rxdb": "16.21.1",
|
|
23
44
|
"rxjs": "7.8.2"
|
|
24
45
|
},
|
|
46
|
+
"devDependencies": {
|
|
47
|
+
"@sqlite.org/sqlite-wasm": "3.53.4-build1",
|
|
48
|
+
"esbuild": "0.27.3",
|
|
49
|
+
"rxdb-premium": "16.21.1"
|
|
50
|
+
},
|
|
25
51
|
"peerDependencies": {
|
|
26
|
-
"
|
|
52
|
+
"@sqlite.org/sqlite-wasm": "^3.53.4-build1",
|
|
53
|
+
"expo-sqlite": ">=15.0.0",
|
|
54
|
+
"rxdb-premium": "16.21.1"
|
|
55
|
+
},
|
|
56
|
+
"peerDependenciesMeta": {
|
|
57
|
+
"@sqlite.org/sqlite-wasm": {
|
|
58
|
+
"optional": true
|
|
59
|
+
},
|
|
60
|
+
"expo-sqlite": {
|
|
61
|
+
"optional": true
|
|
62
|
+
}
|
|
27
63
|
},
|
|
28
64
|
"scripts": {
|
|
29
65
|
"build": "tsup",
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
// Prebuilds the sqlite worker for Metro/Expo web apps, which can't bundle a
|
|
3
|
+
// module worker at runtime (see the package README's "Expo web (Metro)"
|
|
4
|
+
// section). Run this from the CONSUMING APP's own build step, e.g.
|
|
5
|
+
// `npx tallyui-build-sqlite-worker public/` — never in this repo. The entry
|
|
6
|
+
// point is this bin's own package source (src/web/worker.ts), not the
|
|
7
|
+
// installed '@tallyui/storage-sqlite/web-worker' export, which resolves to
|
|
8
|
+
// dist/web/worker.js and so wouldn't exist for a consumer that installs
|
|
9
|
+
// TallyUI from a raw checkout with no build step. The output bundle links
|
|
10
|
+
// rxdb-premium code, so it is built under, and bound by, the APP's own RXDB
|
|
11
|
+
// Premium licence, and it must NEVER be committed to this repo or published
|
|
12
|
+
// from this package.
|
|
13
|
+
//
|
|
14
|
+
// Usage: tallyui-build-sqlite-worker <outDir>
|
|
15
|
+
|
|
16
|
+
import { createRequire } from 'node:module';
|
|
17
|
+
import { pathToFileURL, fileURLToPath } from 'node:url';
|
|
18
|
+
import { mkdir, copyFile, writeFile } from 'node:fs/promises';
|
|
19
|
+
import path from 'node:path';
|
|
20
|
+
|
|
21
|
+
const outDir = process.argv[2];
|
|
22
|
+
if (!outDir) {
|
|
23
|
+
console.error('Usage: tallyui-build-sqlite-worker <outDir>');
|
|
24
|
+
process.exit(1);
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
let esbuild;
|
|
28
|
+
try {
|
|
29
|
+
({ default: esbuild } = await import('esbuild'));
|
|
30
|
+
} catch {
|
|
31
|
+
console.error('esbuild is not installed: install esbuild as a dev dependency to use this script.');
|
|
32
|
+
process.exit(1);
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
const appDir = process.cwd();
|
|
36
|
+
const resolvedOutDir = path.resolve(appDir, outDir);
|
|
37
|
+
await mkdir(resolvedOutDir, { recursive: true });
|
|
38
|
+
|
|
39
|
+
const packageDir = fileURLToPath(new URL('..', import.meta.url));
|
|
40
|
+
const entryPath = fileURLToPath(new URL('../src/web/worker.ts', import.meta.url));
|
|
41
|
+
const entryRelative = path.relative(packageDir, entryPath);
|
|
42
|
+
const metafilePath = process.env.TALLYUI_WORKER_METAFILE;
|
|
43
|
+
|
|
44
|
+
const result = await esbuild.build({
|
|
45
|
+
// The entry is this bin's own package source file (src/web/worker.ts),
|
|
46
|
+
// resolved from the script's own location, not through the package's
|
|
47
|
+
// `exports`: same relative path in a raw checkout and a published
|
|
48
|
+
// install, and neither needs a `dist` build.
|
|
49
|
+
//
|
|
50
|
+
// Everything the entry imports (rxdb-premium, sqlite-wasm) resolves from
|
|
51
|
+
// the entry file's own location, as it did when the entry came through the
|
|
52
|
+
// package export: the installed package's dependencies, which are the
|
|
53
|
+
// app's own peer installs. absWorkingDir keeps output and metafile paths
|
|
54
|
+
// relative to the app.
|
|
55
|
+
absWorkingDir: appDir,
|
|
56
|
+
entryPoints: [entryPath],
|
|
57
|
+
outfile: path.join(resolvedOutDir, 'tallyui-sqlite-worker.js'),
|
|
58
|
+
bundle: true,
|
|
59
|
+
format: 'esm',
|
|
60
|
+
platform: 'browser',
|
|
61
|
+
metafile: Boolean(metafilePath),
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
if (metafilePath) {
|
|
65
|
+
await writeFile(metafilePath, JSON.stringify(result.metafile));
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
// sqlite-wasm's default `locateFile` resolves `sqlite3.wasm` as
|
|
69
|
+
// `new URL('sqlite3.wasm', import.meta.url)`, and esbuild leaves that
|
|
70
|
+
// `import.meta.url` untouched when bundling, so at runtime it evaluates to
|
|
71
|
+
// the worker's own URL. The wasm file must sit next to the built worker for
|
|
72
|
+
// that lookup to find it; no `locateFile` override is needed.
|
|
73
|
+
const require = createRequire(pathToFileURL(path.join(appDir, 'package.json')));
|
|
74
|
+
const wasmSource = require.resolve('@sqlite.org/sqlite-wasm/sqlite3.wasm');
|
|
75
|
+
await copyFile(wasmSource, path.join(resolvedOutDir, 'sqlite3.wasm'));
|
|
76
|
+
|
|
77
|
+
console.log(`Built ${path.join(resolvedOutDir, 'tallyui-sqlite-worker.js')} from ${entryRelative} and copied sqlite3.wasm.`);
|
package/src/params.ts
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* RxDB Premium's SQLite storage passes query params as
|
|
3
|
+
* `(string | number | boolean)[]`, but SQLite itself has no boolean type.
|
|
4
|
+
* Every `SQLiteBasics` implementation in this package binds params the same
|
|
5
|
+
* way, so booleans become `0`/`1` here once instead of in each adapter.
|
|
6
|
+
*/
|
|
7
|
+
export function sqliteBoolParams(params: readonly (string | number | boolean)[]): (string | number)[] {
|
|
8
|
+
return params.map((param) => (typeof param === 'boolean' ? Number(param) : param));
|
|
9
|
+
}
|
package/src/rx-storage-sqlite.ts
CHANGED
|
@@ -1,24 +1,38 @@
|
|
|
1
|
-
import type { RxStorage
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
import
|
|
5
|
-
import {
|
|
6
|
-
createSQLiteStorageInstance,
|
|
7
|
-
type SQLiteStorageInternals,
|
|
8
|
-
} from './storage-instance';
|
|
1
|
+
import type { RxStorage } from 'rxdb';
|
|
2
|
+
import type { SQLiteBasics } from 'rxdb/plugins/storage-sqlite';
|
|
3
|
+
import { getRxStorageSQLite as getPremiumRxStorageSQLite } from 'rxdb-premium/plugins/storage-sqlite';
|
|
4
|
+
import { sqliteBoolParams } from './params';
|
|
5
|
+
import type { SQLiteDatabase } from './types';
|
|
9
6
|
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
7
|
+
/**
|
|
8
|
+
* Wraps a synchronous SQLite handle with RxDB Premium SQLite storage.
|
|
9
|
+
* Apps install the RxDB Premium peer dependency under their own RxDB Premium licence.
|
|
10
|
+
* One SQLite handle serves exactly one RxDB database.
|
|
11
|
+
*/
|
|
12
|
+
export function getRxStorageSQLite(database: SQLiteDatabase): RxStorage<any, any> {
|
|
13
|
+
let databaseName: string | undefined;
|
|
14
|
+
const sqliteBasics: SQLiteBasics<SQLiteDatabase> = {
|
|
15
|
+
debugId: 'tallyui-sqlite-sync',
|
|
16
|
+
journalMode: 'WAL',
|
|
17
|
+
open: (name) => {
|
|
18
|
+
if (databaseName !== undefined && databaseName !== name) {
|
|
19
|
+
throw new Error(`one SQLite handle serves exactly one RxDB database: "${databaseName}" and "${name}"`);
|
|
20
|
+
}
|
|
21
|
+
databaseName = name;
|
|
22
|
+
return Promise.resolve(database);
|
|
23
|
+
},
|
|
24
|
+
all: async (db, q) => {
|
|
25
|
+
return db.getAllSync(q.query, sqliteBoolParams(q.params));
|
|
26
|
+
},
|
|
27
|
+
run: async (db, q) => {
|
|
28
|
+
db.runSync(q.query, sqliteBoolParams(q.params));
|
|
29
|
+
},
|
|
30
|
+
setPragma: async (db, key, value) => {
|
|
31
|
+
db.execSync('pragma ' + key + ' = ' + value + ';');
|
|
32
|
+
},
|
|
33
|
+
close: async () => {
|
|
34
|
+
// The caller opened the handle and owns its lifetime.
|
|
21
35
|
},
|
|
22
36
|
};
|
|
23
|
-
return
|
|
37
|
+
return getPremiumRxStorageSQLite({ sqliteBasics });
|
|
24
38
|
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Thrown by the web SQLite worker's storage (ADR-061) when start-up failed:
|
|
3
|
+
* another tab may hold the opfs-sahpool database, or this browser has no
|
|
4
|
+
* OPFS or sync access handles. Keeps the original failure as `cause`.
|
|
5
|
+
*
|
|
6
|
+
* RxDB's remote-storage channel re-throws a rejected `createStorageInstance`
|
|
7
|
+
* on the main thread as a plain `Error` whose message embeds the original
|
|
8
|
+
* error's `name` as JSON, since `name` itself does not survive the trip.
|
|
9
|
+
* `isStorageWorkerStartError` matches that fixed substring in the message
|
|
10
|
+
* too, for whichever of the error, a `{ name, message }` copy, or the
|
|
11
|
+
* message alone actually arrives.
|
|
12
|
+
*/
|
|
13
|
+
export class StorageWorkerStartError extends Error {
|
|
14
|
+
constructor(message: string, options?: { cause?: unknown }) {
|
|
15
|
+
super(message, options);
|
|
16
|
+
this.name = 'StorageWorkerStartError';
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export function isStorageWorkerStartError(error: unknown): boolean {
|
|
21
|
+
if (typeof error === 'string') return error.includes('StorageWorkerStartError');
|
|
22
|
+
if (!error || typeof error !== 'object') return false;
|
|
23
|
+
const { name, message } = error as { name?: unknown; message?: unknown };
|
|
24
|
+
return name === 'StorageWorkerStartError' || (typeof message === 'string' && message.includes('StorageWorkerStartError'));
|
|
25
|
+
}
|
package/src/web/index.ts
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import type { RxStorage } from 'rxdb';
|
|
2
|
+
import { MESSAGE_CHANNEL_CACHE_BY_IDENTIFIER, OPEN_REMOTE_MESSAGE_CHANNELS } from 'rxdb/plugins/storage-remote';
|
|
3
|
+
import { getRxStorageWorker } from 'rxdb-premium/plugins/storage-worker';
|
|
4
|
+
|
|
5
|
+
export { StorageWorkerStartError, isStorageWorkerStartError } from './errors';
|
|
6
|
+
|
|
7
|
+
/** Marks a storage as this package's web engine (ADR-061), for job 2b to recognise. */
|
|
8
|
+
export const SQLITE_SAHPOOL_ENGINE = 'sqlite-sahpool';
|
|
9
|
+
|
|
10
|
+
/** The web storage, with `terminate()` for the live-tab park order (ADR-061, amendment 1). */
|
|
11
|
+
export type RxStorageSQLiteWasm = RxStorage<any, any> & {
|
|
12
|
+
tallyEngine: string;
|
|
13
|
+
/**
|
|
14
|
+
* Terminates the storage worker, which releases the opfs-sahpool access
|
|
15
|
+
* handles, and evicts its channel from rxdb's cache, so a new storage built
|
|
16
|
+
* the same way starts a fresh worker. Call it after the databases close.
|
|
17
|
+
* Idempotent. Build one storage per page life (ADR-061): a second storage
|
|
18
|
+
* from the same input while the first is alive shares its worker, and only
|
|
19
|
+
* the storage that created the worker can terminate it.
|
|
20
|
+
*/
|
|
21
|
+
terminate(): void;
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
/** The web storage (ADR-061): premium SQLite on sqlite-wasm opfs-sahpool, in one dedicated worker. */
|
|
25
|
+
export function getRxStorageSQLiteWasm(options: {
|
|
26
|
+
workerInput: string | URL | (() => Worker);
|
|
27
|
+
workerName?: string;
|
|
28
|
+
}): RxStorageSQLiteWasm {
|
|
29
|
+
const { workerInput } = options;
|
|
30
|
+
const workerOptions = { type: 'module' as const, name: options.workerName ?? 'tallyui-storage' };
|
|
31
|
+
// Premium creates the worker inside its own closure, out of reach, so this
|
|
32
|
+
// creates it instead and holds the reference that terminate() needs.
|
|
33
|
+
let worker: Worker | undefined;
|
|
34
|
+
const createWorker = () =>
|
|
35
|
+
(worker = typeof workerInput === 'function' ? workerInput() : new Worker(workerInput, workerOptions));
|
|
36
|
+
// Premium's cache identifier is `'rx-storage-worker-' + workerInput`: keep
|
|
37
|
+
// it the caller's input, not this closure's source text.
|
|
38
|
+
createWorker.toString = () => String(workerInput);
|
|
39
|
+
const storage = getRxStorageWorker({ workerInput: createWorker, workerOptions, mode: 'one' });
|
|
40
|
+
let terminated = false;
|
|
41
|
+
return Object.assign(storage, {
|
|
42
|
+
tallyEngine: SQLITE_SAHPOOL_ENGINE,
|
|
43
|
+
terminate() {
|
|
44
|
+
if (terminated) return;
|
|
45
|
+
terminated = true;
|
|
46
|
+
worker?.terminate();
|
|
47
|
+
// Mode 'one' keeps its channel alive forever in rxdb's storage-remote
|
|
48
|
+
// cache, keyed by the identifier above. Left there, the next storage
|
|
49
|
+
// built from the same input reuses the dead channel, and every call hangs.
|
|
50
|
+
const channel = storage.messageChannelIfOneMode;
|
|
51
|
+
const cache = MESSAGE_CHANNEL_CACHE_BY_IDENTIFIER.get(storage.settings.identifier);
|
|
52
|
+
cache?.forEach((item, key) => {
|
|
53
|
+
if (item.messageChannel === channel) cache.delete(key);
|
|
54
|
+
});
|
|
55
|
+
// A worker that never started, or a close that fails, has nothing left to release.
|
|
56
|
+
void channel?.then((open) => {
|
|
57
|
+
OPEN_REMOTE_MESSAGE_CHANNELS.delete(open);
|
|
58
|
+
return open.close();
|
|
59
|
+
}).catch(() => {});
|
|
60
|
+
},
|
|
61
|
+
});
|
|
62
|
+
}
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
import type { SQLiteBasics, SQLResultRow } from 'rxdb/plugins/storage-sqlite';
|
|
2
|
+
import { sqliteBoolParams } from '../params';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* The minimal surface of `@sqlite.org/sqlite-wasm`'s synchronous OO1 API
|
|
6
|
+
* (https://sqlite.org/wasm/doc/trunk/api-oo1.md) that this adapter needs,
|
|
7
|
+
* whether the handle comes from `sqlite3.oo1.DB` or from an opfs-sahpool
|
|
8
|
+
* `OpfsSAHPoolDb`.
|
|
9
|
+
*/
|
|
10
|
+
export interface Oo1Db {
|
|
11
|
+
exec(opts: { sql: string; bind?: unknown[]; rowMode?: 'object'; returnValue?: 'resultRows' }): unknown;
|
|
12
|
+
close(): void;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* A port of WCPOS's `getSQLiteBasicsOo1` spike onto the synchronous OO1 API,
|
|
17
|
+
* for RxDB Premium's SQLite storage (ADR-061). Unlike the expo-sqlite
|
|
18
|
+
* `SQLiteBasics` in `rx-storage-sqlite.ts`, one worker here can serve several
|
|
19
|
+
* RxDB databases: `openDb(name)` opens a separate opfs-sahpool file per name,
|
|
20
|
+
* for example one per store scope after a store switch. What must not happen
|
|
21
|
+
* is opening the *same* name twice, which would give two handles on one pool
|
|
22
|
+
* file — so handles are cached by name. `open(name)` returns the cached
|
|
23
|
+
* handle if there is one, otherwise opens a new one (running
|
|
24
|
+
* `PRAGMA locking_mode = exclusive` before anything else touches it), caches
|
|
25
|
+
* it, and returns it; concurrent opens of the same name share a single
|
|
26
|
+
* in-flight promise. `close` removes the handle from the cache, so a later
|
|
27
|
+
* `open` of that name reopens it.
|
|
28
|
+
*/
|
|
29
|
+
export function getSQLiteBasicsOpfsSahPool({ openDb }: { openDb: (name: string) => Promise<Oo1Db> }): SQLiteBasics<Oo1Db> {
|
|
30
|
+
const handles = new Map<string, Promise<Oo1Db>>();
|
|
31
|
+
const namesByHandle = new Map<Oo1Db, string>();
|
|
32
|
+
return {
|
|
33
|
+
debugId: 'tallyui-sqlite-sahpool',
|
|
34
|
+
journalMode: 'WAL',
|
|
35
|
+
open: async (name) => {
|
|
36
|
+
const cached = handles.get(name);
|
|
37
|
+
if (cached !== undefined) {
|
|
38
|
+
return cached;
|
|
39
|
+
}
|
|
40
|
+
const opening = (async () => {
|
|
41
|
+
const db = await openDb(name);
|
|
42
|
+
// Exclusive locking must be set before anything else touches the
|
|
43
|
+
// handle: it drops SQLite's per-transaction lock/unlock cycle, which
|
|
44
|
+
// is the point of opfs-sahpool owning the origin's storage alone.
|
|
45
|
+
db.exec({ sql: 'PRAGMA locking_mode = exclusive' });
|
|
46
|
+
namesByHandle.set(db, name);
|
|
47
|
+
return db;
|
|
48
|
+
})();
|
|
49
|
+
handles.set(name, opening);
|
|
50
|
+
return opening;
|
|
51
|
+
},
|
|
52
|
+
all: async (db, q) => {
|
|
53
|
+
return db.exec({
|
|
54
|
+
sql: q.query,
|
|
55
|
+
bind: sqliteBoolParams(q.params),
|
|
56
|
+
rowMode: 'object',
|
|
57
|
+
returnValue: 'resultRows',
|
|
58
|
+
}) as SQLResultRow[];
|
|
59
|
+
},
|
|
60
|
+
run: async (db, q) => {
|
|
61
|
+
db.exec({ sql: q.query, bind: sqliteBoolParams(q.params) });
|
|
62
|
+
},
|
|
63
|
+
setPragma: async (db, key, value) => {
|
|
64
|
+
db.exec({ sql: `pragma ${key} = ${value};` });
|
|
65
|
+
},
|
|
66
|
+
close: async (db) => {
|
|
67
|
+
const name = namesByHandle.get(db);
|
|
68
|
+
if (name !== undefined) {
|
|
69
|
+
handles.delete(name);
|
|
70
|
+
namesByHandle.delete(db);
|
|
71
|
+
}
|
|
72
|
+
db.close();
|
|
73
|
+
},
|
|
74
|
+
};
|
|
75
|
+
}
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import sqlite3InitModule from '@sqlite.org/sqlite-wasm';
|
|
2
|
+
import type { RxStorage } from 'rxdb';
|
|
3
|
+
import { getRxStorageSQLite } from 'rxdb-premium/plugins/storage-sqlite';
|
|
4
|
+
import { exposeWorkerRxStorage } from 'rxdb-premium/plugins/storage-worker';
|
|
5
|
+
import { getSQLiteBasicsOpfsSahPool, type Oo1Db } from './sqlite-basics-sahpool';
|
|
6
|
+
import { StorageWorkerStartError } from './errors';
|
|
7
|
+
|
|
8
|
+
// This must run inside a dedicated worker (ADR-061): `createSyncAccessHandle`,
|
|
9
|
+
// which opfs-sahpool needs, exists only there, not on the main thread or in
|
|
10
|
+
// a shared worker. opfs-sahpool also holds exclusive access handles for the
|
|
11
|
+
// whole origin, so once this worker has the pool open, a second tab cannot
|
|
12
|
+
// open it at all; ADR-061 makes one live tab, one dedicated worker, the
|
|
13
|
+
// supported topology and a reload the recovery from a dead or hung worker.
|
|
14
|
+
//
|
|
15
|
+
// `exposeWorkerRxStorage` below must run synchronously, at module start,
|
|
16
|
+
// before the pool install is even awaited: in real Chromium, RxDB's first
|
|
17
|
+
// message (creating the storage instance) arrives well before start-up
|
|
18
|
+
// finishes, and a worker that isn't listening yet drops it, hanging every
|
|
19
|
+
// cold start.
|
|
20
|
+
|
|
21
|
+
// A throwaway instance gives `name`/`rxdbVersion` without awaiting anything;
|
|
22
|
+
// its `openDb` is never called, since only `ready` below opens a real pool.
|
|
23
|
+
const { name, rxdbVersion } = getRxStorageSQLite({
|
|
24
|
+
sqliteBasics: getSQLiteBasicsOpfsSahPool({ openDb: () => Promise.reject(new Error('unused: throwaway instance')) }),
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
const ready: Promise<RxStorage<any, any>> = sqlite3InitModule()
|
|
28
|
+
.then((sqlite3) => sqlite3.installOpfsSAHPoolVfs({ name: 'tallyui', initialCapacity: 64 }))
|
|
29
|
+
.then((pool) =>
|
|
30
|
+
getRxStorageSQLite({
|
|
31
|
+
sqliteBasics: getSQLiteBasicsOpfsSahPool({
|
|
32
|
+
// The OO1 `exec()` here is the real, overloaded sqlite-wasm signature;
|
|
33
|
+
// `Oo1Db` is the minimal shape this package's adapters need from it.
|
|
34
|
+
openDb: async (dbName) => new pool.OpfsSAHPoolDb('/' + dbName) as unknown as Oo1Db,
|
|
35
|
+
}),
|
|
36
|
+
})
|
|
37
|
+
);
|
|
38
|
+
// A pool install failure before any createStorageInstance call would otherwise be an unhandled rejection; createStorageInstance below still surfaces it as StorageWorkerStartError.
|
|
39
|
+
ready.catch(() => {});
|
|
40
|
+
|
|
41
|
+
const storage: RxStorage<any, any> = {
|
|
42
|
+
name,
|
|
43
|
+
rxdbVersion,
|
|
44
|
+
createStorageInstance: async (params) => {
|
|
45
|
+
let realStorage: RxStorage<any, any>;
|
|
46
|
+
try {
|
|
47
|
+
realStorage = await ready;
|
|
48
|
+
} catch (cause) {
|
|
49
|
+
// The pool failed to install: another tab may hold it, or there's no OPFS
|
|
50
|
+
// or sync access handles. Fail every call instead of hanging it forever.
|
|
51
|
+
throw new StorageWorkerStartError(
|
|
52
|
+
'SQLite worker start failed: another tab may hold the database, or this browser lacks OPFS.',
|
|
53
|
+
{ cause }
|
|
54
|
+
);
|
|
55
|
+
}
|
|
56
|
+
return realStorage.createStorageInstance(params);
|
|
57
|
+
},
|
|
58
|
+
};
|
|
59
|
+
|
|
60
|
+
exposeWorkerRxStorage({ storage });
|