node:sqlite
Node's built-in node:sqlite, ported from Node's own src/node_sqlite.cc to TypeScript over the system libsqlite3 (linked only when a program imports the module). DatabaseSync and StatementSync are synchronous; rows are null-prototype objects, user functions, aggregates, the authorizer, sessions, serialization, tag stores and backup() all work as in Node. The one difference: Node bundles its own SQLite, while this uses the system library, so sessions and extension loading are as available as that library makes them.
DatabaseSync
ExactOpens (or creates) a database: a file path, a file: URL, or ':memory:'. Every Node option is honoured — open, readOnly, enableForeignKeyConstraints, enableDoubleQuotedStringLiterals, allowExtension, timeout, readBigInts, returnArrays, allowBareNamedParameters, allowUnknownNamedParameters, defensive.
const db = new DatabaseSync(':memory:');open / close / isOpen / isTransaction
ExactOpening, closing and state. Closing finalizes the database's statements; using one afterwards is ERR_INVALID_STATE, as in Node.
db.close();exec / prepare
Exactexec runs one or more statements; prepare compiles a reusable statement.
const insert = db.prepare('INSERT INTO tasks (title) VALUES (?)');location
ExactThe backing file of an attached database, or null for an in-memory or temporary one.
db.location();function
ExactRegisters a SQL function written in TypeScript (deterministic, directOnly, useBigIntArguments, varargs). An exception it throws is rethrown from the statement that called it.
db.function('shout', (s: string) => s.toUpperCase() + '!');aggregate
ExactRegisters an aggregate (start, step, result) or, with inverse, a window function.
db.aggregate('total', { start: 0, step: (acc: number, v: number) => acc + v });createSession / applyChangeset
Works, with caveatsRecords changes as a changeset or patchset and applies one elsewhere, with filter and conflict handlers.
const session = db.createSession();- Available only where the system libsqlite3 is built with sessions (macOS's is); elsewhere ERR_INVALID_STATE — Node bundles its own SQLite
enableLoadExtension / loadExtension
Works, with caveatsLoads a SQLite extension, when the database was opened with allowExtension.
db.enableLoadExtension(true);- The system libsqlite3 on macOS omits extension loading, so loading one there is ERR_INVALID_STATE — Node bundles its own SQLite
serialize / deserialize / enableDefensive
ExactSnapshots a database to bytes and restores one from them.
const image = db.serialize();run / get / all / iterate
ExactRows are null-prototype objects (arrays with returnArrays); get() is undefined without a row; iterate() steps lazily.
const rows = db.prepare('SELECT * FROM tasks').all();columns / sourceSQL / expandedSQL
ExactEach result column's name, declared type and origin database/table/column; the statement's SQL, and its SQL with the bound values.
stmt.columns();statement options
ExactPer-statement versions of the database's reading and binding options.
stmt.setReadBigInts(true);Parameter binding
ExactPositional ? parameters, or a leading object binding :x/@x/$x (and the bare key). Numbers bind as REAL and bigints as INTEGER, as in Node.
db.prepare('SELECT title FROM tasks WHERE id = :id').get({ id: 2 });Column value mapping
ExactAn INTEGER past 2^53 is ERR_OUT_OF_RANGE unless readBigInts is set.
stmt.setReadBigInts(true);Errors
ExactSQLite failures throw an Error with code ERR_SQLITE_ERROR, errcode and errstr, as Node's.
try { db.exec('bogus'); } catch (e) { e.errcode; }backup / constants
ExactCopies a database into a file a few pages per turn of the event loop, reporting progress; resolves the total page count.
await backup(db, 'copy.db');