Skip to main content
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-wal and notes.db-shm beside 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 real Error:
The three possible messages are:
The third uses the parent directory exactly as derived from the path passed to open (. when there is no directory component). Full model: Permissions.

Quick look

Database.open(path)

Opens (creating when missing) and returns the handle. Every open enables 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).
The path is relative to the process cwd. Database.open("app.db") in ff start lands next to where you launched, not necessarily next to your script. Permission scopes are fixed at launch, so launch from the directory you intend to write to, or pass an absolute path that a scope covers.
: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:
Permission errors are the exception: they are real 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:
db.lastInsertRowId without () is the function object, not the id. This bites in Response.json({ id: db.lastInsertRowId }) — always write db.lastInsertRowId().

Transactions: transaction(fn)

Semantics: 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:
Failed transfer rolls everything back — no half-writes:

Tuning: busyTimeout / close

Raise the timeout when workers or concurrent writers contend; lower it to fail fast on deadlock-prone flows.

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