Run authtempo on SQLite
For: a developer of a Node or Bun app that keeps its data in a SQLite file, or in a libSQL database.
What it does: @authtempo/adapter-sqlite gives core a storage adapter and a shared limiter on SQLite. It runs the code of the D1 adapter over the handle that the app opens.
Before you start
Section titled “Before you start”- Node 22.13 or newer, or Bun 1.4.3, the tested version.
- A working setup from a getting started tutorial.
- On Workers, use D1 or a remote libSQL database. Workers on D1 tells how to set up D1.
- Open the database with the driver of the app. Set a busy timeout on
node:sqliteand onbun:sqlite. - Wrap the handle with the entry of its driver, such as
nodeSqlite(db). - Run
applySqliteMigrations(driver)at each start, or runauthtempo migrate --target sqlitebefore the start. - Pass
createSqliteAdapter(driver)andcreateSqliteLimiter(driver)tocreateAuth.
import { closeSync, openSync } from "node:fs";import { DatabaseSync } from "node:sqlite";import { applySqliteMigrations, createSqliteAdapter, createSqliteLimiter,} from "@authtempo/adapter-sqlite";import { nodeSqlite } from "@authtempo/adapter-sqlite/node";import { createAuth, type MailPort } from "@authtempo/core";
/** * authtempo on a SQLite file through `node:sqlite` (Node 22.13 or newer). * The storage and the owned limiter use one handle. Call `close` when the * process stops. */export async function authOnSqlite(path: string, mail: MailPort, secret: string) { // The file holds sealed secrets and password hashes. Make it with mode 0600 // before SQLite opens it: SQLite makes a new file with mode 0644, less the // umask. A file that exists keeps its mode. closeSync(openSync(path, "a", 0o600)); const db = new DatabaseSync(path); // The default busy timeout of node:sqlite is 0 ms: a second writer then // gets "database is locked" at once. db.exec("PRAGMA busy_timeout = 5000"); const driver = nodeSqlite(db); await applySqliteMigrations(driver); const auth = createAuth({ baseURL: "https://api.example.com", trustedOrigins: [], secret, mail, storage: createSqliteAdapter(driver), limiter: createSqliteLimiter(driver), }); return { auth, close: () => db.close() };}The drivers
Section titled “The drivers”The app opens the database and passes the handle to the entry of its driver. No entry imports a driver at run time. So an app on Bun loads no better-sqlite3, and a Worker loads only the libSQL module that it imports.
| Entry | Driver | Tested on | Peer dependency |
|---|---|---|---|
@authtempo/adapter-sqlite/node |
node:sqlite |
Node 22.13 or newer | none |
@authtempo/adapter-sqlite/better-sqlite3 |
better-sqlite3 13 | Node | better-sqlite3 (optional) |
@authtempo/adapter-sqlite/bun |
bun:sqlite |
Bun | none |
@authtempo/adapter-sqlite/libsql |
@libsql/client for file:, and @libsql/client/web for http:, https:, and libsql: |
Node; Workers in a bundle proof | @libsql/client (optional) |
Every driver runs the same core suites. They are the storage conformance suite, the limiter suite, the secret rotation suite, the two refresh race suites, and the shape check.
Transactions
Section titled “Transactions”SQLite through this adapter has no interactive transaction. The adapter keeps an undo log with one entry for each write of the transaction. The log lives in memory. If the function throws, the adapter runs the undo entries in reverse order.
The undo log has two limits:
- A transaction is not isolated from other requests. Another request can read a row that a later rollback removes. The real guards of the engine are unique keys and conditional updates.
- A process that stops during a transaction never rolls back, because the undo log lives in memory. The writes made before the stop stay.
A write of another request that lands after a write of the transaction, and before its rollback, is lost to the rollback. If an undo entry fails, the adapter throws StorageError with rollback_failed.
On node:sqlite, better-sqlite3, and bun:sqlite, a batch is BEGIN IMMEDIATE, the statements, and COMMIT in one synchronous block. On libSQL, a batch is client.batch(statements, "write"). No transaction stays open across an await.
The busy timeout and the write lock
Section titled “The busy timeout and the write lock”- The default busy timeout of
node:sqliteand ofbun:sqliteis 0 ms. A second process that writes to the same file then gets “database is locked” at once. RunPRAGMA busy_timeout = 5000after the open. - better-sqlite3 waits 5000 ms by default.
- A libSQL batch on a
file:database fails at once withSQLITE_BUSYwhile another connection holds the write lock. The adapter passes that error through, as D1 does. - Over HTTP, the libSQL server makes a batch wait for the lock. The server ends a write transaction after 5 seconds.
Workers
Section titled “Workers”| Driver | On Workers |
|---|---|
| D1 | Yes, through @authtempo/adapter-d1. |
libSQL over https: or libsql: |
Yes. Import createClient from @libsql/client/web, which uses fetch. |
libSQL over file: |
No. @libsql/client/web refuses a file: URL. |
better-sqlite3, node:sqlite, bun:sqlite |
No. Each one is a native module or a built-in of another runtime. |
A proof Worker of the package bundles the libsql entry, the adapter, and @libsql/client/web with Node.js compatibility off. The bundle imports no Node module. That is a proof of the bundle, not a deploy.
For a libSQL host that is not a loopback address, use https: or libsql:, so the token and the rows go over TLS.
The database file
Section titled “The database file”- The file holds sealed secrets and password hashes. Create it with mode 0600, as the sample does.
authtempo migrate --target sqlite --file <path>makes a new file with mode 0600. A file that exists keeps its mode.- Keep the file outside the web root.
- Treat a backup of the file as a secret.
Settings the adapter refuses
Section titled “Settings the adapter refuses”The decode rules of core read integers as numbers. A handle that reads integers as BigInt breaks dates, counts, and flags. So the first call of the adapter throws StorageError with config_mismatch on these settings:
node:sqlite:readBigInts: true.- better-sqlite3:
db.defaultSafeIntegers(true). bun:sqlite:safeIntegers: true.- libSQL:
intMode: "bigint".
Migrations
Section titled “Migrations”The SQLite migrations are the D1 files of @authtempo/adapter-d1, copied into the package at build with a byte check. The runner records each name in ta_migrations. It runs each file and its record in one write transaction, so no file runs twice.
A second process that runs the migrations at the same time waits for the write lock. It then reads the record and skips the work. The wait on a file depends on the driver:
node:sqlite,bun:sqlite, and better-sqlite3: the busy timeout of the handle. At 0 ms, the second process fails at once with “database is locked”.- libSQL on a
file:database: the runner tries the step again for up to 10 seconds.
When the wait ends with no lock, the driver error passes through. Run the migrations again.
authtempo migrate --target sqlite --file <path> applies the same files from the command line, with a busy timeout of 5000 ms. For a libSQL URL on a host that is not a loopback address, the command needs TLS. authtempo migrate gives the rules. authtempo generate --dialect sqlite prints the schema for an ORM that reads the tables.
If it fails
Section titled “If it fails”schema_mismatch: the migrations did not run. Run them, and the next call reads the version again.config_mismatch: the handle reads integers asBigInt. Open it without the setting.rollback_failed: an undo entry failed. Read thecause, and check the rows of the transaction.- “database is locked” or
SQLITE_BUSY: another process holds the write lock. Set a busy timeout, or retry the request.