Skip to content

@typedblite/runtime

The TypeDBLite engine: it runs TypeQLite against a SQLite database inside your process, and keeps a change log that replication and full-text search are built on.

Terminal window
npm install @typedblite/runtime

Requires Node.js 22.16 or later (node:sqlite). In a browser, it runs on WASM SQLite: see In the browser.

import { TypeDBLiteInterpreter } from '@typedblite/runtime';
const db = new TypeDBLiteInterpreter(':memory:'); // or './my.db'
db.define(`
define
entity person, owns name @key;
attribute name, value string;
`);
db.execute(`insert $p isa person, has name "Alice";`);
const { rows } = db.query(`match $p isa person, has name $n; select $n;`);
rows[0]?.n.asString(); // 'Alice'

API reference: npm run docs:api at the repository root (not hosted yet). Guides: Embedding the runtime · The TypeQLite language · Replication and search

Export What it is
TypeDBLiteInterpreter The engine. Synchronous: define, execute, query, transactions, schemaSnapshot, changeLog
SqliteEngine An async wrapper with a transaction(work) helper, for code written against an asynchronous database interface (ADR-002)
TypeDBLiteValue A result cell: an entity, relation, attribute, value or type
DiagnosticCode, diagnosticOf Stable codes for every fact and refusal, and how to read one from a thrown error (ADR-004)
ChangeLog Reached as db.changeLog: every committed write, with CouchDB-style revisions (ADR-003)
replicateOnce One-shot push, pull or duplex replication between two change logs
serveChangeStream, createChangeStreamHandler The hub: an SSE stream of changes, and a POST endpoint for spokes
TypeDBLiteReplicator, ChangeStreamClient The spoke: one-shot or continuous replication with a hub over HTTP
FullTextIndex Reached as db.changeLog.fullText: FTS5 search over every recorded thing
SqliteDatabase, SqliteOpener, openNodeSqlite The binding interface, and its node:sqlite implementation (Node only)
initWasmSqlite, openWasmSqlite, wipeOpfsPool, poolInfo From @typedblite/runtime/wasm: the binding on WASM SQLite, with OPFS persistence

The storage model mirrors TypeDB’s, not a table per type. Every store has the same fixed tables: thing (one row per entity or relation), attribute (one row per type and value), has (ownership), role_player, type_parent, and schema_* tables recording the schema. @key and @unique are partial UNIQUE indexes, and role players have foreign keys to thing. A duplicate key or a dangling role player is therefore refused by SQLite itself. Architecture has the details.

TypeDBLite runs in a browser, Electron or a Capacitor WebView on the official SQLite WASM build (ADR-009):

Terminal window
npm install @typedblite/runtime @sqlite.org/sqlite-wasm
// In a dedicated Web Worker:
import { TypeDBLiteInterpreter } from '@typedblite/runtime';
import { initWasmSqlite, openWasmSqlite } from '@typedblite/runtime/wasm';
await initWasmSqlite({
moduleOptions: { locateFile: (file) => `/${file}` }, // where you serve sqlite3.wasm
sahPool: { name: 'my-app' }, // persist in OPFS
});
const db = new TypeDBLiteInterpreter('/tasks.db', undefined, { openDatabase: openWasmSqlite });
db.define(schema); // on every open: it restores the store's key ownerships
  • A bundler picks the browser entry through the browser export condition. It has everything except the node:sqlite binding and the SSE hub. Replicating to a hub with TypeDBLiteReplicator works from the browser.
  • Persistence is OPFS through SQLite’s SAH-pool VFS. That works in a dedicated Worker, and needs neither SharedArrayBuffer nor COOP/COEP headers. Without sahPool, a path is held in memory for the life of the page.
  • One connection per store. The SAH pool allows one connection to a file at a time. Give a store one owner: a single worker, or one elected tab.
  • Browser floor: Chromium 117, Firefox 119, Safari 17.4. The parser calls Object.groupBy. For an older engine, such as Android 14’s built-in WebView (Chromium 113), add a polyfill before importing the runtime.
  • Serve sqlite3.wasm from @sqlite.org/sqlite-wasm/dist/, and point locateFile at it.

Only node-sqlite.ts imports node:sqlite. Everything else uses the SqliteDatabase interface in src/sqlite-database.ts, so another binding can be passed as { openDatabase }. A binding must be synchronous, bind named parameters without a prefix ({ id } binds :id), report changes exactly, run several statements in one exec, and include FTS5. A WASM SQLite running synchronously in a Web Worker qualifies.

Terminal window
npm exec nx -- run typedblite-runtime:build
npm exec nx -- run typedblite-runtime:test

Tests live in test/*.test.ts. Two more targets run the engine on WASM SQLite:

Terminal window
npm exec nx -- run typedblite-runtime:test-wasm # the whole suite on WASM SQLite, in Node
npm exec nx -- run typedblite-runtime:test-browser # in a real Chromium, on OPFS (needs Playwright's Chromium)

See Testing.

TypeDBLite is an independent open-source project, built on TypeDB with thanks. It is not affiliated with, sponsored by, or endorsed by TypeDB Ltd. Released under the MPL-2.0 license.