Database is global — no import, no package. One file per database, synchronous calls, WAL mode on, 5s busy timeout by default.
Permissions
Database.open touches the filesystem, so on-disk databases need both
read and write grants:
:memory:is exempt — no file is touched, so no flags are needed.- Both file grants are required even for a query-only workload: the handle is
opened with
SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE. - A write scope naming only the database file is not enough. SQLite writes
notes.db-walandnotes.db-shmbeside it, so the containing directory must be writable too. - Relative paths resolve against the current cwd; scopes are resolved
against the launch cwd and never move. If you
process.chdir()before opening, the path lands in the new directory but must still fall inside a scope captured at startup. - Denial happens before
sqlite3_open, and the error is a realError:
open (. when there is no directory component).
Full model: Permissions.
Quick look
Database.open(path)
PRAGMA journal_mode=WAL and a 5000ms busy timeout — override the latter per handle with busyTimeout(ms).
Paths must be non-empty, must not contain NUL bytes, and file: URI filenames are not supported (they throw a TypeError).
:memory: gets an ephemeral database that disappears when the handle closes. It
is the easiest option for tests and scratch work because it needs no filesystem
permissions.
Reading: row / rows
Row objects map column names to values.
params is an array bound to ? placeholders in order — always prefer placeholders over string interpolation (no escaping bugs, no injection). Each Database handle maintains a 64-entry prepared-statement cache (with pre-interned column property atoms and automatic eviction on SQLITE_SCHEMA). Parameter binding maps 32-bit signed integers to INTEGER, other JS numbers (double) to REAL, strings to TEXT, ArrayBuffer/Uint8Array to BLOB, booleans to 0/1, and null/undefined/BigInt to NULL.
Writing: exec / execNoArgs
SQL errors throw — a bad statement never fails silently. The thrown value is a plain string, not an
Error object:
Error objects thrown by
Database.open, with name and code set.
Introspection: changes / lastInsertRowId
Both are methods — call them with parentheses, right after the write:
Transactions: transaction(fn)
BEGIN, then your function runs (called with the db as this, no arguments). Throw inside → ROLLBACK and the error propagates. Return cleanly → COMMIT. The return value of transaction() is whatever your function returned:
Tuning: busyTimeout / close
Practical example: notes API on disk
Troubleshooting
no such table: x — the execNoArgs(CREATE TABLE …) never ran (fresh cwd? different open path?). Paths are cwd-relative — log process.cwd() when in doubt.
Database.open throws PermissionDenied — check which message you got.
...requires --allow-read for 'notes.db' means the file scope is missing;
...requires --allow-write for 'notes.db' means the file write scope is
missing; ...requires --allow-write for '.' means the parent directory is
not writable. A common fix is --allow-read=notes.db --allow-write=..
Caught error has no .message — SQL failures throw plain strings (typeof e === "string"), so e.message is undefined and e instanceof Error is false. Log String(e). Database.open permission failures are real Errors and do have .message.
database is locked — contention beat the busy timeout. Raise db.busyTimeout(ms), shorten write transactions, or serialize writers through one handle.
id comes back as source text in JSON — you passed db.lastInsertRowId instead of calling db.lastInsertRowId(). Add the parens.
Queries after close() throw — by design. Open one handle per process (or per worker) and close only at shutdown.
transaction requires a function argument — you passed a non-function (e.g. the result of calling it). Pass the closure: db.transaction(() => { … }).
See also
- Permissions — flags, scopes, denial contract
- Postgres API — network database client
- API overview — every injected global
- Limitations — every hard cap