@monlite/wasm
v0.2.2
Published
Run @monlite/core in the browser (or anywhere) on SQLite-WASM via sql.js — a custom driver.
Downloads
1,132
Maintainers
Readme
@monlite/wasm
Run @monlite/core in the browser on SQLite
compiled to WebAssembly, via sql.js.
monlite's driver-adapter seam means the browser is just another backend — the document and
structured collection API, reactive watch(), and the kv/queue/cron harness all run unchanged,
in-browser, no server required.
npm install @monlite/core @monlite/wasm sql.jsQuick start
initSqlJs is async (it loads the .wasm binary), so initialise it first, then hand the
module to wasmDriver. createDb itself stays synchronous.
import initSqlJs from "sql.js";
import { createDb } from "@monlite/core";
import { wasmDriver } from "@monlite/wasm";
const SQL = await initSqlJs({
locateFile: (file) => `/sqljs/${file}`, // point at where your bundler serves sql-wasm.wasm
});
const db = createDb(":memory:", { driver: wasmDriver(SQL) });
await db.collection("notes").create({ data: { title: "hello", body: "from the browser" } });
const notes = await db.collection("notes").findMany({ where: { title: "hello" } });Bundler note: copy
node_modules/sql.js/dist/sql-wasm.wasmto a served path and return it fromlocateFile. For Vite, put it inpublic/sqljs/.
Persistence
sql.js holds the database in memory. To persist it across page loads, snapshot the bytes to IndexedDB (or OPFS) and restore them on startup:
import { wasmDriver, exportDatabase } from "@monlite/wasm";
// On startup: restore from IndexedDB if a previous snapshot exists
const saved = await idbGet("monlite-db"); // Uint8Array | undefined
const db = createDb(":memory:", { driver: wasmDriver(SQL, { data: saved }) });
// After writes (debounced) or on beforeunload: persist
async function persist() {
await idbSet("monlite-db", exportDatabase(db));
}A reactive debounce works well here: subscribe with collection.watch() and call persist() on
a trailing debounce after each change.
Sync with the server
A monlite WASM database uses the same SQLite format as the Node backends, so it syncs with a
server or another device through @monlite/sync
like any other monlite database.
Notes
- sql.js runs in Node as well, so
@monlite/wasmis covered by the normal test suite. - Snapshotting rewrites the whole file, which is fine up to tens of MB. For larger, write-heavy
databases the planned next step is a driver over
@sqlite.org/sqlite-wasmwith the OPFS VFS (incremental persistence via a Web Worker). SameDriverinterface, drop-in.
License
MIT
