@monlite/wasm 0.1.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 +70 -0
- package/dist/index.cjs +102 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +73 -0
- package/dist/index.d.ts +73 -0
- package/dist/index.js +98 -0
- package/dist/index.js.map +1 -0
- package/package.json +75 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Emad Jumaah
|
|
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,70 @@
|
|
|
1
|
+
# @monlite/wasm
|
|
2
|
+
|
|
3
|
+
Run [`@monlite/core`](https://www.npmjs.com/package/@monlite/core) **in the browser** (or anywhere) on SQLite compiled to WebAssembly, via [sql.js](https://sql.js.org). monlite's driver-adapter seam means the browser is just another backend — the document/structured query API, reactivity, and the kv/queue/cron harness all run unchanged.
|
|
4
|
+
|
|
5
|
+
```bash
|
|
6
|
+
npm install @monlite/core @monlite/wasm sql.js
|
|
7
|
+
```
|
|
8
|
+
|
|
9
|
+
## Quick start
|
|
10
|
+
|
|
11
|
+
`initSqlJs` is async (it loads the `.wasm`), so initialise it first, then hand the
|
|
12
|
+
module to `wasmDriver` — `createDb` itself stays synchronous:
|
|
13
|
+
|
|
14
|
+
```ts
|
|
15
|
+
import initSqlJs from "sql.js";
|
|
16
|
+
import { createDb } from "@monlite/core";
|
|
17
|
+
import { wasmDriver } from "@monlite/wasm";
|
|
18
|
+
|
|
19
|
+
const SQL = await initSqlJs({
|
|
20
|
+
// point at where your bundler serves sql-wasm.wasm
|
|
21
|
+
locateFile: (file) => `/sqljs/${file}`,
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
const db = createDb(":memory:", { driver: wasmDriver(SQL) });
|
|
25
|
+
|
|
26
|
+
await db.collection("notes").create({ data: { title: "hello", body: "from the browser" } });
|
|
27
|
+
const notes = await db.collection("notes").findMany({ where: { title: "hello" } });
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
> Bundlers: copy `node_modules/sql.js/dist/sql-wasm.wasm` to a served path and
|
|
31
|
+
> return it from `locateFile`. (Vite: put it in `public/sqljs/`.)
|
|
32
|
+
|
|
33
|
+
## Persistence
|
|
34
|
+
|
|
35
|
+
sql.js holds the database in memory. Persist it by **snapshotting the bytes** to
|
|
36
|
+
IndexedDB (or OPFS) and reopening from them — `exportDatabase()` gives you the
|
|
37
|
+
bytes, and `wasmDriver(SQL, { data })` restores them:
|
|
38
|
+
|
|
39
|
+
```ts
|
|
40
|
+
import { wasmDriver, exportDatabase } from "@monlite/wasm";
|
|
41
|
+
|
|
42
|
+
// On startup: restore previous bytes (your IndexedDB getter), if any.
|
|
43
|
+
const saved = await idbGet("monlite-db"); // Uint8Array | undefined
|
|
44
|
+
const db = createDb(":memory:", { driver: wasmDriver(SQL, { data: saved }) });
|
|
45
|
+
|
|
46
|
+
// After writes (debounced) or on `beforeunload`: snapshot back.
|
|
47
|
+
async function persist() {
|
|
48
|
+
await idbSet("monlite-db", exportDatabase(db));
|
|
49
|
+
}
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
A small reactive debounce works well: persist after a quiet period following any
|
|
53
|
+
write (e.g. subscribe with `collection.watch(...)` and call `persist()` on a
|
|
54
|
+
trailing debounce).
|
|
55
|
+
|
|
56
|
+
### Roadmap: incremental OPFS persistence
|
|
57
|
+
|
|
58
|
+
Snapshotting rewrites the whole file, which is fine up to tens of MB. For larger,
|
|
59
|
+
write-heavy databases the planned next step is a driver over the **official
|
|
60
|
+
`@sqlite.org/sqlite-wasm` with the OPFS VFS** (running in a Web Worker with
|
|
61
|
+
synchronous access handles), which persists **incrementally** — no full-file
|
|
62
|
+
re-export. Same `Driver` seam, drop-in. (Tracked on the roadmap.)
|
|
63
|
+
|
|
64
|
+
## Notes
|
|
65
|
+
|
|
66
|
+
- sql.js runs in Node as well, so this driver is covered by the normal test suite.
|
|
67
|
+
- A monlite WASM database is the same SQLite format as the Node backends, so it
|
|
68
|
+
syncs with a server (or another device) through [`@monlite/sync`](https://www.npmjs.com/package/@monlite/sync) like any other monlite database.
|
|
69
|
+
|
|
70
|
+
MIT
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
// src/index.ts
|
|
4
|
+
var WasmDriver = class {
|
|
5
|
+
name = "wasm-sqlite";
|
|
6
|
+
raw;
|
|
7
|
+
cache = /* @__PURE__ */ new Map();
|
|
8
|
+
depth = 0;
|
|
9
|
+
constructor(SQL, options = {}) {
|
|
10
|
+
this.raw = new SQL.Database(options.data ?? null);
|
|
11
|
+
this.raw.run("PRAGMA foreign_keys = ON");
|
|
12
|
+
}
|
|
13
|
+
exec(sql) {
|
|
14
|
+
this.raw.run(sql);
|
|
15
|
+
}
|
|
16
|
+
lastInsertRowid() {
|
|
17
|
+
const res = this.raw.exec("SELECT last_insert_rowid() AS id");
|
|
18
|
+
return res[0]?.values?.[0]?.[0] ?? 0;
|
|
19
|
+
}
|
|
20
|
+
prepare(sql) {
|
|
21
|
+
const cached = this.cache.get(sql);
|
|
22
|
+
if (cached) return cached.wrapped;
|
|
23
|
+
const stmt = this.raw.prepare(sql);
|
|
24
|
+
const wrapped = {
|
|
25
|
+
run: (...params) => {
|
|
26
|
+
stmt.run(params);
|
|
27
|
+
return {
|
|
28
|
+
changes: this.raw.getRowsModified(),
|
|
29
|
+
lastInsertRowid: this.lastInsertRowid()
|
|
30
|
+
};
|
|
31
|
+
},
|
|
32
|
+
get: (...params) => {
|
|
33
|
+
stmt.bind(params);
|
|
34
|
+
const row = stmt.step() ? stmt.getAsObject() : void 0;
|
|
35
|
+
stmt.reset();
|
|
36
|
+
return row;
|
|
37
|
+
},
|
|
38
|
+
all: (...params) => {
|
|
39
|
+
stmt.bind(params);
|
|
40
|
+
const rows = [];
|
|
41
|
+
while (stmt.step()) rows.push(stmt.getAsObject());
|
|
42
|
+
stmt.reset();
|
|
43
|
+
return rows;
|
|
44
|
+
}
|
|
45
|
+
};
|
|
46
|
+
this.cache.set(sql, { stmt, wrapped });
|
|
47
|
+
return wrapped;
|
|
48
|
+
}
|
|
49
|
+
transaction(fn) {
|
|
50
|
+
const savepoint = `monlite_sp_${this.depth}`;
|
|
51
|
+
if (this.depth === 0) this.raw.run("BEGIN");
|
|
52
|
+
else this.raw.run(`SAVEPOINT ${savepoint}`);
|
|
53
|
+
this.depth++;
|
|
54
|
+
try {
|
|
55
|
+
const result = fn();
|
|
56
|
+
this.depth--;
|
|
57
|
+
if (this.depth === 0) this.raw.run("COMMIT");
|
|
58
|
+
else this.raw.run(`RELEASE ${savepoint}`);
|
|
59
|
+
return result;
|
|
60
|
+
} catch (err) {
|
|
61
|
+
this.depth--;
|
|
62
|
+
try {
|
|
63
|
+
if (this.depth === 0) this.raw.run("ROLLBACK");
|
|
64
|
+
else this.raw.run(`ROLLBACK TO ${savepoint}; RELEASE ${savepoint}`);
|
|
65
|
+
} catch {
|
|
66
|
+
this.depth = 0;
|
|
67
|
+
try {
|
|
68
|
+
this.raw.run("ROLLBACK");
|
|
69
|
+
} catch {
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
throw err;
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
/** Serialize the whole database to bytes (for persistence). */
|
|
76
|
+
export() {
|
|
77
|
+
return this.raw.export();
|
|
78
|
+
}
|
|
79
|
+
close() {
|
|
80
|
+
for (const { stmt } of this.cache.values()) stmt.free();
|
|
81
|
+
this.cache.clear();
|
|
82
|
+
this.raw.close();
|
|
83
|
+
}
|
|
84
|
+
};
|
|
85
|
+
function wasmDriver(SQL, options) {
|
|
86
|
+
return new WasmDriver(SQL, options);
|
|
87
|
+
}
|
|
88
|
+
function exportDatabase(db) {
|
|
89
|
+
const driver = db.driver;
|
|
90
|
+
if (!(driver instanceof WasmDriver)) {
|
|
91
|
+
throw new Error(
|
|
92
|
+
"exportDatabase requires a database opened with the WASM driver"
|
|
93
|
+
);
|
|
94
|
+
}
|
|
95
|
+
return driver.export();
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
exports.WasmDriver = WasmDriver;
|
|
99
|
+
exports.exportDatabase = exportDatabase;
|
|
100
|
+
exports.wasmDriver = wasmDriver;
|
|
101
|
+
//# sourceMappingURL=index.cjs.map
|
|
102
|
+
//# sourceMappingURL=index.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/index.ts"],"names":[],"mappings":";;;AA+CO,IAAM,aAAN,MAAmC;AAAA,EAC/B,IAAA,GAAO,aAAA;AAAA,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,QAAA,IAAI,KAAK,KAAA,KAAU,CAAA,EAAG,IAAA,CAAK,GAAA,CAAI,IAAI,UAAU,CAAA;AAAA,kBACnC,GAAA,CAAI,GAAA,CAAI,eAAe,SAAS,CAAA,UAAA,EAAa,SAAS,CAAA,CAAE,CAAA;AAAA,MACpE,CAAA,CAAA,MAAQ;AACN,QAAA,IAAA,CAAK,KAAA,GAAQ,CAAA;AACb,QAAA,IAAI;AACF,UAAA,IAAA,CAAK,GAAA,CAAI,IAAI,UAAU,CAAA;AAAA,QACzB,CAAA,CAAA,MAAQ;AAAA,QAER;AAAA,MACF;AACA,MAAA,MAAM,GAAA;AAAA,IACR;AAAA,EACF;AAAA;AAAA,EAGA,MAAA,GAAqB;AACnB,IAAA,OAAO,IAAA,CAAK,IAAI,MAAA,EAAO;AAAA,EACzB;AAAA,EAEA,KAAA,GAAc;AACZ,IAAA,KAAA,MAAW,EAAE,MAAK,IAAK,IAAA,CAAK,MAAM,MAAA,EAAO,OAAQ,IAAA,EAAK;AACtD,IAAA,IAAA,CAAK,MAAM,KAAA,EAAM;AACjB,IAAA,IAAA,CAAK,IAAI,KAAA,EAAM;AAAA,EACjB;AACF;AAGO,SAAS,UAAA,CACd,KACA,OAAA,EACY;AACZ,EAAA,OAAO,IAAI,UAAA,CAAW,GAAA,EAAK,OAAO,CAAA;AACpC;AAMO,SAAS,eAAe,EAAA,EAAoC;AACjE,EAAA,MAAM,SAAS,EAAA,CAAG,MAAA;AAClB,EAAA,IAAI,EAAE,kBAAkB,UAAA,CAAA,EAAa;AACnC,IAAA,MAAM,IAAI,KAAA;AAAA,MACR;AAAA,KACF;AAAA,EACF;AACA,EAAA,OAAO,OAAO,MAAA,EAAO;AACvB","file":"index.cjs","sourcesContent":["import type { Driver, PreparedStatement, RunResult } from \"@monlite/core\";\n\n/**\n * The subset of the sql.js module/`Database` we rely on. Passing `sql.js`'s\n * `SqlJsStatic` (the result of `initSqlJs()`) satisfies this.\n */\nexport interface SqlJsStatic {\n Database: new (data?: Uint8Array | null) => SqlJsDatabase;\n}\nexport interface SqlJsDatabase {\n run(sql: string, params?: any[]): void;\n prepare(sql: string): SqlJsStatement;\n exec(sql: string): Array<{ columns: string[]; values: any[][] }>;\n getRowsModified(): number;\n export(): Uint8Array;\n close(): void;\n}\nexport interface SqlJsStatement {\n bind(params?: any[]): boolean;\n step(): boolean;\n getAsObject(): Record<string, any>;\n run(params?: any[]): void;\n reset(): void;\n free(): void;\n}\n\nexport interface WasmDriverOptions {\n /** Existing database bytes to open (e.g. restored from IndexedDB/OPFS). */\n data?: Uint8Array | null;\n}\n\n/**\n * A monlite {@link Driver} backed by SQLite-WASM (sql.js). Runs in the browser\n * (and Node). Construction is synchronous — initialise sql.js yourself first:\n *\n * ```ts\n * import initSqlJs from \"sql.js\";\n * import { wasmDriver } from \"@monlite/wasm\";\n * import { createDb } from \"@monlite/core\";\n *\n * const SQL = await initSqlJs({ locateFile: (f) => `/sqljs/${f}` });\n * const db = createDb(\":memory:\", { driver: wasmDriver(SQL) });\n * ```\n *\n * sql.js is in-memory; use {@link exportDatabase}/`{ data }` to persist (see the\n * README for IndexedDB/OPFS recipes).\n */\nexport class WasmDriver implements Driver {\n readonly name = \"wasm-sqlite\";\n readonly raw: SqlJsDatabase;\n private readonly cache = new Map<\n string,\n { stmt: SqlJsStatement; wrapped: PreparedStatement }\n >();\n private depth = 0;\n\n constructor(SQL: SqlJsStatic, options: WasmDriverOptions = {}) {\n this.raw = new SQL.Database(options.data ?? null);\n this.raw.run(\"PRAGMA foreign_keys = ON\");\n }\n\n exec(sql: string): void {\n this.raw.run(sql);\n }\n\n private lastInsertRowid(): number {\n const res = this.raw.exec(\"SELECT last_insert_rowid() AS id\");\n return (res[0]?.values?.[0]?.[0] as number) ?? 0;\n }\n\n prepare(sql: string): PreparedStatement {\n const cached = this.cache.get(sql);\n if (cached) return cached.wrapped;\n\n const stmt = this.raw.prepare(sql);\n // Arrow functions capture the driver's `this` lexically.\n const wrapped: PreparedStatement = {\n run: (...params: any[]): RunResult => {\n stmt.run(params);\n return {\n changes: this.raw.getRowsModified(),\n lastInsertRowid: this.lastInsertRowid(),\n };\n },\n get: (...params: any[]): any => {\n stmt.bind(params);\n const row = stmt.step() ? stmt.getAsObject() : undefined;\n stmt.reset();\n return row;\n },\n all: (...params: any[]): any[] => {\n stmt.bind(params);\n const rows: any[] = [];\n while (stmt.step()) rows.push(stmt.getAsObject());\n stmt.reset();\n return rows;\n },\n };\n this.cache.set(sql, { stmt, wrapped });\n return wrapped;\n }\n\n transaction<T>(fn: () => T): T {\n const savepoint = `monlite_sp_${this.depth}`;\n if (this.depth === 0) this.raw.run(\"BEGIN\");\n else this.raw.run(`SAVEPOINT ${savepoint}`);\n this.depth++;\n try {\n const result = fn();\n this.depth--;\n if (this.depth === 0) this.raw.run(\"COMMIT\");\n else this.raw.run(`RELEASE ${savepoint}`);\n return result;\n } catch (err) {\n this.depth--;\n try {\n if (this.depth === 0) this.raw.run(\"ROLLBACK\");\n else this.raw.run(`ROLLBACK TO ${savepoint}; RELEASE ${savepoint}`);\n } catch {\n this.depth = 0;\n try {\n this.raw.run(\"ROLLBACK\");\n } catch {\n /* no active transaction */\n }\n }\n throw err;\n }\n }\n\n /** Serialize the whole database to bytes (for persistence). */\n export(): Uint8Array {\n return this.raw.export();\n }\n\n close(): void {\n for (const { stmt } of this.cache.values()) stmt.free();\n this.cache.clear();\n this.raw.close();\n }\n}\n\n/** Create a WASM-backed monlite driver from an initialised sql.js module. */\nexport function wasmDriver(\n SQL: SqlJsStatic,\n options?: WasmDriverOptions,\n): WasmDriver {\n return new WasmDriver(SQL, options);\n}\n\n/**\n * Serialize a monlite database opened with the WASM driver to bytes — persist\n * these (IndexedDB/OPFS/file) and reopen with `wasmDriver(SQL, { data })`.\n */\nexport function exportDatabase(db: { driver: Driver }): Uint8Array {\n const driver = db.driver;\n if (!(driver instanceof WasmDriver)) {\n throw new Error(\n \"exportDatabase requires a database opened with the WASM driver\",\n );\n }\n return driver.export();\n}\n"]}
|
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
import { Driver, PreparedStatement } from '@monlite/core';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* The subset of the sql.js module/`Database` we rely on. Passing `sql.js`'s
|
|
5
|
+
* `SqlJsStatic` (the result of `initSqlJs()`) satisfies this.
|
|
6
|
+
*/
|
|
7
|
+
interface SqlJsStatic {
|
|
8
|
+
Database: new (data?: Uint8Array | null) => SqlJsDatabase;
|
|
9
|
+
}
|
|
10
|
+
interface SqlJsDatabase {
|
|
11
|
+
run(sql: string, params?: any[]): void;
|
|
12
|
+
prepare(sql: string): SqlJsStatement;
|
|
13
|
+
exec(sql: string): Array<{
|
|
14
|
+
columns: string[];
|
|
15
|
+
values: any[][];
|
|
16
|
+
}>;
|
|
17
|
+
getRowsModified(): number;
|
|
18
|
+
export(): Uint8Array;
|
|
19
|
+
close(): void;
|
|
20
|
+
}
|
|
21
|
+
interface SqlJsStatement {
|
|
22
|
+
bind(params?: any[]): boolean;
|
|
23
|
+
step(): boolean;
|
|
24
|
+
getAsObject(): Record<string, any>;
|
|
25
|
+
run(params?: any[]): void;
|
|
26
|
+
reset(): void;
|
|
27
|
+
free(): void;
|
|
28
|
+
}
|
|
29
|
+
interface WasmDriverOptions {
|
|
30
|
+
/** Existing database bytes to open (e.g. restored from IndexedDB/OPFS). */
|
|
31
|
+
data?: Uint8Array | null;
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* A monlite {@link Driver} backed by SQLite-WASM (sql.js). Runs in the browser
|
|
35
|
+
* (and Node). Construction is synchronous — initialise sql.js yourself first:
|
|
36
|
+
*
|
|
37
|
+
* ```ts
|
|
38
|
+
* import initSqlJs from "sql.js";
|
|
39
|
+
* import { wasmDriver } from "@monlite/wasm";
|
|
40
|
+
* import { createDb } from "@monlite/core";
|
|
41
|
+
*
|
|
42
|
+
* const SQL = await initSqlJs({ locateFile: (f) => `/sqljs/${f}` });
|
|
43
|
+
* const db = createDb(":memory:", { driver: wasmDriver(SQL) });
|
|
44
|
+
* ```
|
|
45
|
+
*
|
|
46
|
+
* sql.js is in-memory; use {@link exportDatabase}/`{ data }` to persist (see the
|
|
47
|
+
* README for IndexedDB/OPFS recipes).
|
|
48
|
+
*/
|
|
49
|
+
declare class WasmDriver implements Driver {
|
|
50
|
+
readonly name = "wasm-sqlite";
|
|
51
|
+
readonly raw: SqlJsDatabase;
|
|
52
|
+
private readonly cache;
|
|
53
|
+
private depth;
|
|
54
|
+
constructor(SQL: SqlJsStatic, options?: WasmDriverOptions);
|
|
55
|
+
exec(sql: string): void;
|
|
56
|
+
private lastInsertRowid;
|
|
57
|
+
prepare(sql: string): PreparedStatement;
|
|
58
|
+
transaction<T>(fn: () => T): T;
|
|
59
|
+
/** Serialize the whole database to bytes (for persistence). */
|
|
60
|
+
export(): Uint8Array;
|
|
61
|
+
close(): void;
|
|
62
|
+
}
|
|
63
|
+
/** Create a WASM-backed monlite driver from an initialised sql.js module. */
|
|
64
|
+
declare function wasmDriver(SQL: SqlJsStatic, options?: WasmDriverOptions): WasmDriver;
|
|
65
|
+
/**
|
|
66
|
+
* Serialize a monlite database opened with the WASM driver to bytes — persist
|
|
67
|
+
* these (IndexedDB/OPFS/file) and reopen with `wasmDriver(SQL, { data })`.
|
|
68
|
+
*/
|
|
69
|
+
declare function exportDatabase(db: {
|
|
70
|
+
driver: Driver;
|
|
71
|
+
}): Uint8Array;
|
|
72
|
+
|
|
73
|
+
export { type SqlJsDatabase, type SqlJsStatement, type SqlJsStatic, WasmDriver, type WasmDriverOptions, exportDatabase, wasmDriver };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
import { Driver, PreparedStatement } from '@monlite/core';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* The subset of the sql.js module/`Database` we rely on. Passing `sql.js`'s
|
|
5
|
+
* `SqlJsStatic` (the result of `initSqlJs()`) satisfies this.
|
|
6
|
+
*/
|
|
7
|
+
interface SqlJsStatic {
|
|
8
|
+
Database: new (data?: Uint8Array | null) => SqlJsDatabase;
|
|
9
|
+
}
|
|
10
|
+
interface SqlJsDatabase {
|
|
11
|
+
run(sql: string, params?: any[]): void;
|
|
12
|
+
prepare(sql: string): SqlJsStatement;
|
|
13
|
+
exec(sql: string): Array<{
|
|
14
|
+
columns: string[];
|
|
15
|
+
values: any[][];
|
|
16
|
+
}>;
|
|
17
|
+
getRowsModified(): number;
|
|
18
|
+
export(): Uint8Array;
|
|
19
|
+
close(): void;
|
|
20
|
+
}
|
|
21
|
+
interface SqlJsStatement {
|
|
22
|
+
bind(params?: any[]): boolean;
|
|
23
|
+
step(): boolean;
|
|
24
|
+
getAsObject(): Record<string, any>;
|
|
25
|
+
run(params?: any[]): void;
|
|
26
|
+
reset(): void;
|
|
27
|
+
free(): void;
|
|
28
|
+
}
|
|
29
|
+
interface WasmDriverOptions {
|
|
30
|
+
/** Existing database bytes to open (e.g. restored from IndexedDB/OPFS). */
|
|
31
|
+
data?: Uint8Array | null;
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* A monlite {@link Driver} backed by SQLite-WASM (sql.js). Runs in the browser
|
|
35
|
+
* (and Node). Construction is synchronous — initialise sql.js yourself first:
|
|
36
|
+
*
|
|
37
|
+
* ```ts
|
|
38
|
+
* import initSqlJs from "sql.js";
|
|
39
|
+
* import { wasmDriver } from "@monlite/wasm";
|
|
40
|
+
* import { createDb } from "@monlite/core";
|
|
41
|
+
*
|
|
42
|
+
* const SQL = await initSqlJs({ locateFile: (f) => `/sqljs/${f}` });
|
|
43
|
+
* const db = createDb(":memory:", { driver: wasmDriver(SQL) });
|
|
44
|
+
* ```
|
|
45
|
+
*
|
|
46
|
+
* sql.js is in-memory; use {@link exportDatabase}/`{ data }` to persist (see the
|
|
47
|
+
* README for IndexedDB/OPFS recipes).
|
|
48
|
+
*/
|
|
49
|
+
declare class WasmDriver implements Driver {
|
|
50
|
+
readonly name = "wasm-sqlite";
|
|
51
|
+
readonly raw: SqlJsDatabase;
|
|
52
|
+
private readonly cache;
|
|
53
|
+
private depth;
|
|
54
|
+
constructor(SQL: SqlJsStatic, options?: WasmDriverOptions);
|
|
55
|
+
exec(sql: string): void;
|
|
56
|
+
private lastInsertRowid;
|
|
57
|
+
prepare(sql: string): PreparedStatement;
|
|
58
|
+
transaction<T>(fn: () => T): T;
|
|
59
|
+
/** Serialize the whole database to bytes (for persistence). */
|
|
60
|
+
export(): Uint8Array;
|
|
61
|
+
close(): void;
|
|
62
|
+
}
|
|
63
|
+
/** Create a WASM-backed monlite driver from an initialised sql.js module. */
|
|
64
|
+
declare function wasmDriver(SQL: SqlJsStatic, options?: WasmDriverOptions): WasmDriver;
|
|
65
|
+
/**
|
|
66
|
+
* Serialize a monlite database opened with the WASM driver to bytes — persist
|
|
67
|
+
* these (IndexedDB/OPFS/file) and reopen with `wasmDriver(SQL, { data })`.
|
|
68
|
+
*/
|
|
69
|
+
declare function exportDatabase(db: {
|
|
70
|
+
driver: Driver;
|
|
71
|
+
}): Uint8Array;
|
|
72
|
+
|
|
73
|
+
export { type SqlJsDatabase, type SqlJsStatement, type SqlJsStatic, WasmDriver, type WasmDriverOptions, exportDatabase, wasmDriver };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
// src/index.ts
|
|
2
|
+
var WasmDriver = class {
|
|
3
|
+
name = "wasm-sqlite";
|
|
4
|
+
raw;
|
|
5
|
+
cache = /* @__PURE__ */ new Map();
|
|
6
|
+
depth = 0;
|
|
7
|
+
constructor(SQL, options = {}) {
|
|
8
|
+
this.raw = new SQL.Database(options.data ?? null);
|
|
9
|
+
this.raw.run("PRAGMA foreign_keys = ON");
|
|
10
|
+
}
|
|
11
|
+
exec(sql) {
|
|
12
|
+
this.raw.run(sql);
|
|
13
|
+
}
|
|
14
|
+
lastInsertRowid() {
|
|
15
|
+
const res = this.raw.exec("SELECT last_insert_rowid() AS id");
|
|
16
|
+
return res[0]?.values?.[0]?.[0] ?? 0;
|
|
17
|
+
}
|
|
18
|
+
prepare(sql) {
|
|
19
|
+
const cached = this.cache.get(sql);
|
|
20
|
+
if (cached) return cached.wrapped;
|
|
21
|
+
const stmt = this.raw.prepare(sql);
|
|
22
|
+
const wrapped = {
|
|
23
|
+
run: (...params) => {
|
|
24
|
+
stmt.run(params);
|
|
25
|
+
return {
|
|
26
|
+
changes: this.raw.getRowsModified(),
|
|
27
|
+
lastInsertRowid: this.lastInsertRowid()
|
|
28
|
+
};
|
|
29
|
+
},
|
|
30
|
+
get: (...params) => {
|
|
31
|
+
stmt.bind(params);
|
|
32
|
+
const row = stmt.step() ? stmt.getAsObject() : void 0;
|
|
33
|
+
stmt.reset();
|
|
34
|
+
return row;
|
|
35
|
+
},
|
|
36
|
+
all: (...params) => {
|
|
37
|
+
stmt.bind(params);
|
|
38
|
+
const rows = [];
|
|
39
|
+
while (stmt.step()) rows.push(stmt.getAsObject());
|
|
40
|
+
stmt.reset();
|
|
41
|
+
return rows;
|
|
42
|
+
}
|
|
43
|
+
};
|
|
44
|
+
this.cache.set(sql, { stmt, wrapped });
|
|
45
|
+
return wrapped;
|
|
46
|
+
}
|
|
47
|
+
transaction(fn) {
|
|
48
|
+
const savepoint = `monlite_sp_${this.depth}`;
|
|
49
|
+
if (this.depth === 0) this.raw.run("BEGIN");
|
|
50
|
+
else this.raw.run(`SAVEPOINT ${savepoint}`);
|
|
51
|
+
this.depth++;
|
|
52
|
+
try {
|
|
53
|
+
const result = fn();
|
|
54
|
+
this.depth--;
|
|
55
|
+
if (this.depth === 0) this.raw.run("COMMIT");
|
|
56
|
+
else this.raw.run(`RELEASE ${savepoint}`);
|
|
57
|
+
return result;
|
|
58
|
+
} catch (err) {
|
|
59
|
+
this.depth--;
|
|
60
|
+
try {
|
|
61
|
+
if (this.depth === 0) this.raw.run("ROLLBACK");
|
|
62
|
+
else this.raw.run(`ROLLBACK TO ${savepoint}; RELEASE ${savepoint}`);
|
|
63
|
+
} catch {
|
|
64
|
+
this.depth = 0;
|
|
65
|
+
try {
|
|
66
|
+
this.raw.run("ROLLBACK");
|
|
67
|
+
} catch {
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
throw err;
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
/** Serialize the whole database to bytes (for persistence). */
|
|
74
|
+
export() {
|
|
75
|
+
return this.raw.export();
|
|
76
|
+
}
|
|
77
|
+
close() {
|
|
78
|
+
for (const { stmt } of this.cache.values()) stmt.free();
|
|
79
|
+
this.cache.clear();
|
|
80
|
+
this.raw.close();
|
|
81
|
+
}
|
|
82
|
+
};
|
|
83
|
+
function wasmDriver(SQL, options) {
|
|
84
|
+
return new WasmDriver(SQL, options);
|
|
85
|
+
}
|
|
86
|
+
function exportDatabase(db) {
|
|
87
|
+
const driver = db.driver;
|
|
88
|
+
if (!(driver instanceof WasmDriver)) {
|
|
89
|
+
throw new Error(
|
|
90
|
+
"exportDatabase requires a database opened with the WASM driver"
|
|
91
|
+
);
|
|
92
|
+
}
|
|
93
|
+
return driver.export();
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
export { WasmDriver, exportDatabase, wasmDriver };
|
|
97
|
+
//# sourceMappingURL=index.js.map
|
|
98
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/index.ts"],"names":[],"mappings":";AA+CO,IAAM,aAAN,MAAmC;AAAA,EAC/B,IAAA,GAAO,aAAA;AAAA,EACP,GAAA;AAAA,EACQ,KAAA,uBAAY,GAAA,EAG3B;AAAA,EACM,KAAA,GAAQ,CAAA;AAAA,EAEhB,WAAA,CAAY,GAAA,EAAkB,OAAA,GAA6B,EAAC,EAAG;AAC7D,IAAA,IAAA,CAAK,MAAM,IAAI,GAAA,CAAI,QAAA,CAAS,OAAA,CAAQ,QAAQ,IAAI,CAAA;AAChD,IAAA,IAAA,CAAK,GAAA,CAAI,IAAI,0BAA0B,CAAA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AA,CAAI,IAAI,UAAU,CAAA;AAAA,QACzB,CAAA,CAAA,MAAQ;AAAA,QAER;AAAA,MACF;AACA,MAAA,MAAM,GAAA;AAAA,IACR;AAAA,EACF;AAAA;AAAA,EAGA,MAAA,GAAqB;AACnB,IAAA,OAAO,IAAA,CAAK,IAAI,MAAA,EAAO;AAAA,EACzB;AAAA,EAEA,KAAA,GAAc;AACZ,IAAA,KAAA,MAAW,EAAE,MAAK,IAAK,IAAA,CAAK,MAAM,MAAA,EAAO,OAAQ,IAAA,EAAK;AACtD,IAAA,IAAA,CAAK,MAAM,KAAA,EAAM;AACjB,IAAA,IAAA,CAAK,IAAI,KAAA,EAAM;AAAA,EACjB;AACF;AAGO,SAAS,UAAA,CACd,KACA,OAAA,EACY;AACZ,EAAA,OAAO,IAAI,UAAA,CAAW,GAAA,EAAK,OAAO,CAAA;AACpC;AAMO,SAAS,eAAe,EAAA,EAAoC;AACjE,EAAA,MAAM,SAAS,EAAA,CAAG,MAAA;AAClB,EAAA,IAAI,EAAE,kBAAkB,UAAA,CAAA,EAAa;AACnC,IAAA,MAAM,IAAI,KAAA;AAAA,MACR;AAAA,KACF;AAAA,EACF;AACA,EAAA,OAAO,OAAO,MAAA,EAAO;AACvB","file":"index.js","sourcesContent":["import type { Driver, PreparedStatement, RunResult } from \"@monlite/core\";\n\n/**\n * The subset of the sql.js module/`Database` we rely on. Passing `sql.js`'s\n * `SqlJsStatic` (the result of `initSqlJs()`) satisfies this.\n */\nexport interface SqlJsStatic {\n Database: new (data?: Uint8Array | null) => SqlJsDatabase;\n}\nexport interface SqlJsDatabase {\n run(sql: string, params?: any[]): void;\n prepare(sql: string): SqlJsStatement;\n exec(sql: string): Array<{ columns: string[]; values: any[][] }>;\n getRowsModified(): number;\n export(): Uint8Array;\n close(): void;\n}\nexport interface SqlJsStatement {\n bind(params?: any[]): boolean;\n step(): boolean;\n getAsObject(): Record<string, any>;\n run(params?: any[]): void;\n reset(): void;\n free(): void;\n}\n\nexport interface WasmDriverOptions {\n /** Existing database bytes to open (e.g. restored from IndexedDB/OPFS). */\n data?: Uint8Array | null;\n}\n\n/**\n * A monlite {@link Driver} backed by SQLite-WASM (sql.js). Runs in the browser\n * (and Node). Construction is synchronous — initialise sql.js yourself first:\n *\n * ```ts\n * import initSqlJs from \"sql.js\";\n * import { wasmDriver } from \"@monlite/wasm\";\n * import { createDb } from \"@monlite/core\";\n *\n * const SQL = await initSqlJs({ locateFile: (f) => `/sqljs/${f}` });\n * const db = createDb(\":memory:\", { driver: wasmDriver(SQL) });\n * ```\n *\n * sql.js is in-memory; use {@link exportDatabase}/`{ data }` to persist (see the\n * README for IndexedDB/OPFS recipes).\n */\nexport class WasmDriver implements Driver {\n readonly name = \"wasm-sqlite\";\n readonly raw: SqlJsDatabase;\n private readonly cache = new Map<\n string,\n { stmt: SqlJsStatement; wrapped: PreparedStatement }\n >();\n private depth = 0;\n\n constructor(SQL: SqlJsStatic, options: WasmDriverOptions = {}) {\n this.raw = new SQL.Database(options.data ?? null);\n this.raw.run(\"PRAGMA foreign_keys = ON\");\n }\n\n exec(sql: string): void {\n this.raw.run(sql);\n }\n\n private lastInsertRowid(): number {\n const res = this.raw.exec(\"SELECT last_insert_rowid() AS id\");\n return (res[0]?.values?.[0]?.[0] as number) ?? 0;\n }\n\n prepare(sql: string): PreparedStatement {\n const cached = this.cache.get(sql);\n if (cached) return cached.wrapped;\n\n const stmt = this.raw.prepare(sql);\n // Arrow functions capture the driver's `this` lexically.\n const wrapped: PreparedStatement = {\n run: (...params: any[]): RunResult => {\n stmt.run(params);\n return {\n changes: this.raw.getRowsModified(),\n lastInsertRowid: this.lastInsertRowid(),\n };\n },\n get: (...params: any[]): any => {\n stmt.bind(params);\n const row = stmt.step() ? stmt.getAsObject() : undefined;\n stmt.reset();\n return row;\n },\n all: (...params: any[]): any[] => {\n stmt.bind(params);\n const rows: any[] = [];\n while (stmt.step()) rows.push(stmt.getAsObject());\n stmt.reset();\n return rows;\n },\n };\n this.cache.set(sql, { stmt, wrapped });\n return wrapped;\n }\n\n transaction<T>(fn: () => T): T {\n const savepoint = `monlite_sp_${this.depth}`;\n if (this.depth === 0) this.raw.run(\"BEGIN\");\n else this.raw.run(`SAVEPOINT ${savepoint}`);\n this.depth++;\n try {\n const result = fn();\n this.depth--;\n if (this.depth === 0) this.raw.run(\"COMMIT\");\n else this.raw.run(`RELEASE ${savepoint}`);\n return result;\n } catch (err) {\n this.depth--;\n try {\n if (this.depth === 0) this.raw.run(\"ROLLBACK\");\n else this.raw.run(`ROLLBACK TO ${savepoint}; RELEASE ${savepoint}`);\n } catch {\n this.depth = 0;\n try {\n this.raw.run(\"ROLLBACK\");\n } catch {\n /* no active transaction */\n }\n }\n throw err;\n }\n }\n\n /** Serialize the whole database to bytes (for persistence). */\n export(): Uint8Array {\n return this.raw.export();\n }\n\n close(): void {\n for (const { stmt } of this.cache.values()) stmt.free();\n this.cache.clear();\n this.raw.close();\n }\n}\n\n/** Create a WASM-backed monlite driver from an initialised sql.js module. */\nexport function wasmDriver(\n SQL: SqlJsStatic,\n options?: WasmDriverOptions,\n): WasmDriver {\n return new WasmDriver(SQL, options);\n}\n\n/**\n * Serialize a monlite database opened with the WASM driver to bytes — persist\n * these (IndexedDB/OPFS/file) and reopen with `wasmDriver(SQL, { data })`.\n */\nexport function exportDatabase(db: { driver: Driver }): Uint8Array {\n const driver = db.driver;\n if (!(driver instanceof WasmDriver)) {\n throw new Error(\n \"exportDatabase requires a database opened with the WASM driver\",\n );\n }\n return driver.export();\n}\n"]}
|
package/package.json
ADDED
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@monlite/wasm",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Run @monlite/core in the browser (or anywhere) on SQLite-WASM via sql.js — a custom driver.",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "./dist/index.cjs",
|
|
7
|
+
"module": "./dist/index.js",
|
|
8
|
+
"types": "./dist/index.d.ts",
|
|
9
|
+
"exports": {
|
|
10
|
+
".": {
|
|
11
|
+
"import": {
|
|
12
|
+
"types": "./dist/index.d.ts",
|
|
13
|
+
"default": "./dist/index.js"
|
|
14
|
+
},
|
|
15
|
+
"require": {
|
|
16
|
+
"types": "./dist/index.d.cts",
|
|
17
|
+
"default": "./dist/index.cjs"
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
},
|
|
21
|
+
"files": [
|
|
22
|
+
"dist"
|
|
23
|
+
],
|
|
24
|
+
"sideEffects": false,
|
|
25
|
+
"keywords": [
|
|
26
|
+
"monlite",
|
|
27
|
+
"wasm",
|
|
28
|
+
"sqlite",
|
|
29
|
+
"browser",
|
|
30
|
+
"sql.js",
|
|
31
|
+
"local-first",
|
|
32
|
+
"opfs"
|
|
33
|
+
],
|
|
34
|
+
"license": "MIT",
|
|
35
|
+
"author": "Emad Jumaah <emadjumaah@gmail.com>",
|
|
36
|
+
"repository": {
|
|
37
|
+
"type": "git",
|
|
38
|
+
"url": "git+https://github.com/qataruts/monlite.git",
|
|
39
|
+
"directory": "packages/wasm"
|
|
40
|
+
},
|
|
41
|
+
"homepage": "https://github.com/qataruts/monlite/tree/main/packages/wasm#readme",
|
|
42
|
+
"bugs": {
|
|
43
|
+
"url": "https://github.com/qataruts/monlite/issues"
|
|
44
|
+
},
|
|
45
|
+
"publishConfig": {
|
|
46
|
+
"access": "public"
|
|
47
|
+
},
|
|
48
|
+
"engines": {
|
|
49
|
+
"node": ">=18"
|
|
50
|
+
},
|
|
51
|
+
"dependencies": {
|
|
52
|
+
"@monlite/core": "^1.3.0"
|
|
53
|
+
},
|
|
54
|
+
"peerDependencies": {
|
|
55
|
+
"sql.js": ">=1.8"
|
|
56
|
+
},
|
|
57
|
+
"peerDependenciesMeta": {
|
|
58
|
+
"sql.js": {
|
|
59
|
+
"optional": true
|
|
60
|
+
}
|
|
61
|
+
},
|
|
62
|
+
"devDependencies": {
|
|
63
|
+
"@types/node": "^22.10.0",
|
|
64
|
+
"@types/sql.js": "^1.4.9",
|
|
65
|
+
"sql.js": "^1.14.1",
|
|
66
|
+
"tsup": "^8.3.5",
|
|
67
|
+
"typescript": "^5.7.2",
|
|
68
|
+
"vitest": "^2.1.8"
|
|
69
|
+
},
|
|
70
|
+
"scripts": {
|
|
71
|
+
"build": "tsup",
|
|
72
|
+
"test": "vitest run",
|
|
73
|
+
"typecheck": "tsc --noEmit"
|
|
74
|
+
}
|
|
75
|
+
}
|