SQLite
Run your Models on a SQLite file, on Node.js or Bun.
Installing
The SQLite adapter comes with @forestfuture/arbor, as its /sqlite entry. It runs on node:sqlite, which is built into Node.js and Bun, so there’s nothing else to install:
npm install @forestfuture/arbor
It needs Bun, or Node.js 24.16 or later.
Your Models, queries, and callbacks work the same on SQLite as on D1, which runs SQLite too. The sections after Connecting are the places the adapter works differently.
Using better-sqlite3
The adapter can open your file with better-sqlite3 in place of node:sqlite. better-sqlite3 is a stable package with no ExperimentalWarning. Install version 13:
npm install better-sqlite3
Then name it as the driver. Your schema’s binding names it with sqlite({ binding: "DATABASE_URL", driver: "better-sqlite3" }). Name it wherever you call connect with a path too:
import { connect } from "@forestfuture/arbor/sqlite";
const db = connect("storage/test.sqlite3", { driver: "better-sqlite3" });
The adapter loads better-sqlite3 only when a driver names it, and throws an error saying to install it if it’s missing. Your Models behave the same on either driver, and both store the same values. better-sqlite3 takes a path or ":memory:" but not a file: URL, so the adapter refuses one.
Connecting
Your schema names a binding with adapter: sqlite({ binding: "DATABASE_URL" }). The binding is the environment variable of that name, holding the path to your database file. A relative path is read from the process’s working directory. With DATABASE_URL=storage/development.sqlite3 set, User.find(id) works with nothing to set up. The first query opens the file, and every later query in the process uses that connection. If the variable isn’t set, a query throws NoDatabase naming it.
connect opens a database yourself. It takes a path, ":memory:", a file: URL, or a DatabaseSync or better-sqlite3 Database you opened:
import { withDatabase } from "@forestfuture/arbor";
import { connect } from "@forestfuture/arbor/sqlite";
const db = connect(":memory:");
await withDatabase(db, () => runTests());
Inside withDatabase, a Database opened from the variable’s file serves your Models. One opened from another file doesn’t, and neither does a :memory: database. When the variable isn’t set, the one Database you pass to withDatabase serves, whatever file it’s on, and passing several is an error. A database you pass is matched by its file, whichever driver opened it.
The library opens each file once per driver in a process, however many times you connect to it. A :memory: database is never shared, so each connect(":memory:") is a new, empty database. A database you pass stays yours to close.
Migrating with the command
Run npx arbor init --adapter sqlite to name your database in arbor.jsonc. It writes DATABASE_URL on the file storage/development.sqlite3, and adds /storage/*.sqlite3* to .gitignore. Then npx arbor migrate makes the file and applies your migrations. It writes your schema from the file, with adapter: sqlite({ binding: "DATABASE_URL" }). When your package.json depends on better-sqlite3, init names it as the entry’s "driver", and the schema’s sqlite() names it too. npx arbor console opens the file with your Models.
The command records each migration’s version in a schema_migrations table, as on Postgres. Each migration runs with its version between BEGIN IMMEDIATE and COMMIT, so one that fails leaves nothing behind. Foreign keys are checked when a migration ends, not as each statement runs, so a migration can rebuild a table other tables reference: create the new table, copy the rows, drop the old one, and rename the new one. A migration that leaves a key pointing at nothing fails and is rolled back. A second arbor migrate at the same time waits for the first one’s migration, then skips what it applied. A migration with its own BEGIN, COMMIT, or ROLLBACK is refused, but a trigger’s BEGIN … END is fine.
A SQLite file has no remote database, so --remote is refused. In production, set the entry’s "path": "${DATABASE_URL}" and run npx arbor migrate on the machine that has the file.
Waiting for a write
Files the library opens use SQLite’s write-ahead log, so reading never waits for a write. A write waits up to 5 seconds for another process’s write to finish, then throws database is locked. A database you pass keeps its own settings.
Batches are transactions
A batch is written whole or not at all, as on D1. Its writes run between BEGIN IMMEDIATE and COMMIT, and nothing else in your process runs until it’s done. A preload’s or a page’s queries run in one transaction too, so they read the same data.
There’s no transaction(async (tx) => …). As on D1, you can make a set of writes atomic by preparing them in advance as a batch.
Large integers
An integer column reads as a number. A value past Number.MAX_SAFE_INTEGER throws a TypeError naming its attribute, rather than reading as a number that’s slightly wrong.
Sequence keys and counters never get near that limit. Ids made elsewhere can pass it, such as snowflake ids or ids from another service. To check a database before you use Arbor with it, save this query as check.sql:
WITH columns AS (
SELECT m.name AS table_name, p.name AS column_name,
'"' || replace(m.name, '"', '""') || '"' AS quoted_table,
'"' || replace(p.name, '"', '""') || '"' AS quoted_column
FROM sqlite_schema AS m JOIN pragma_table_info(m.name) AS p
WHERE m.type = 'table' AND m.name NOT GLOB 'sqlite_*'
AND (upper(p.type) GLOB '*INT*' OR NOT (upper(p.type) GLOB '*CHAR*'
OR upper(p.type) GLOB '*CLOB*' OR upper(p.type) GLOB '*TEXT*'))
)
SELECT coalesce(group_concat(
'SELECT ' || quote(table_name || '.' || column_name) || ' AS "column" WHERE EXISTS (SELECT 1 FROM ' ||
quoted_table || ' WHERE typeof(' || quoted_column || ') = ''integer'' AND (' || quoted_column ||
' > 9007199254740991 OR ' || quoted_column || ' < -9007199254740991));',
char(10)), 'SELECT NULL AS "column" WHERE 0;') AS statements
FROM columns;
It writes one statement for each column that can hold an integer. Run them against the same file:
sqlite3 storage/development.sqlite3 < check.sql | sqlite3 storage/development.sqlite3
It lists each column that holds a value Arbor can’t read. A text column turns an integer into text, so the check skips those. It can read each table once for each column it checks, so run it on a copy of a large database.
Keep such ids in a TEXT column. It reads as a string attribute, which keeps every digit, writes to JSON without loss, and links to text, integer, or uuid keys in other tables. Assign and look up each id as text, such as "1234567890123456789".
Digit text in an INTEGER column is stored as an integer again, so the column’s type has to change. Rebuild the table as Changing a column’s type shows. Rebuild each table that references it the same way. Copy each id with CAST(id AS TEXT).
In the generated schema, a TEXT key gets generateId: true, so a record built without an id gets a random UUID. When every id comes from elsewhere, turn that off in the Model:
// src/models/account.ts
import { ApplicationRecord } from "./application-record";
export class Account extends ApplicationRecord {
static override attributes = {
id: { generateId: false },
} as const;
}
Bound parameters
A statement binds at most 32,766 values. The adapter binds lists as one JSON value, as on D1, so where({ id: [...] }) and insertAll work with any number of values. A statement that would bind more throws TooManyBoundValues, and nothing in its batch is written.
Foreign keys are enforced
Files the library opens check the foreign keys a table declares with REFERENCES, as D1 does. Destroying a record whose rows still point at it throws InvalidForeignKey, unless the association has a dependent strategy that removes or updates those rows first.
SQLite 3.41 or later
Opening a database with an older SQLite throws, naming its version. Node.js 24.16 brings a newer one, so this only happens with an older build of SQLite.
Node.js 24.16 or later
Opening a database on an older Node.js throws, naming its version. Node.js 24 needs 24.16 or later, and Node.js 26 needs 26.1 or later. Before them, node:sqlite cuts a text value at its first NUL character when it reads it, so a value could come back shorter than you saved it. Bun runs the adapter whatever Node.js version it reports.