A Bun-first SQLite helper for creating and configuring Bun SQL connections with sensible defaults, configurable PRAGMAs, typed options, lifecycle hooks, and clear configuration errors.
@bun-and-butter/sqlite is built for the common case where you want to use
SQLite through Bun SQL, but do not want to hand-roll the connection setup
every time. It gives you a small wrapper around Bun SQL that applies
production-friendly defaults, exposes the most important SQLite tuning options
through a typed API, and keeps configuration errors separate from normal Bun SQL
runtime errors.
This package is Bun-only.
We do not publish prebuilt artifacts to npm or another package registry.
@bun-and-butter/sqlite is installed directly from the GitHub repository and
that is the recommended install path for the current version.
For the exact bun add behavior, see the
Bun docs on Git dependencies.
bun add git@github.com:bun-and-butter/sqlite.gitimport { buildSQLite } from "@bun-and-butter/sqlite";
interface MessageRow {
message: string;
}
const sqlite = await buildSQLite();
try {
await sqlite`CREATE TABLE messages (message TEXT NOT NULL);`;
await sqlite`INSERT INTO messages (message) VALUES (${"Hello from SQLite"});`;
const rows = await sqlite<
MessageRow[]
>`SELECT message FROM messages ORDER BY rowid;`;
console.log(rows[0]?.message);
} finally {
await sqlite.close();
}Using Bun SQL directly is already pleasant, but SQLite connections often need a bit of repeated setup around them:
- pick a sensible
synchronousmode - decide when
PRAGMA optimizeshould run - enable foreign keys
- configure cache size, busy timeout, WAL checkpointing, and temp storage
- parse
sqlite://paths and optional?mode=... - run connection lifecycle hooks
- distinguish invalid configuration from normal SQL runtime failures
buildSQLite(...) keeps that setup small and consistent while still returning a
normal Bun SQL client for queries.
The package currently focuses on the connection layer:
- creates a Bun
SQLclient configured for the SQLite adapter - accepts plain file paths and Bun-style
sqlite://paths - supports
ro,rw, andrwcmodes - applies sensible defaults for long-running processes
- lets you override important SQLite PRAGMAs with typed options
- supports
onAfterConnect,onBeforeClose, andonAfterClosehooks - provides
deleteSQLiteFiles(...)for removing a database together with its-waland-shmfiles
The package throws SQLiteError only for configuration problems handled by the
package itself, for example:
- an empty path
- an invalid
mode - other invalid connection setup passed into
buildSQLite(...)
Everything else is left as a normal Bun SQL or SQLite runtime error. That means query failures, connection failures, and close-time failures are not wrapped or translated.
When you call buildSQLite() without overrides, the package uses defaults aimed
at long-running processes such as servers, workers, and other application
runtimes.
That includes:
- an in-memory database when no path is provided
mode=rwcsynchronous = normalforeign_keys = onbusy_timeout = 5000cache_size = -65536journal_mode = walfor writable connections- periodic
PRAGMA optimize
For short-lived scripts or one-off jobs, you can override those defaults with
Optimize.OnExit and any other options you need.
The repository currently includes four examples:
examples/demo.tsshows the smallest useful setup withbuildSQLite(), a simple query, andclose()examples/full_demo.tsshows the broader API surface including typed options, hooks, and PRAGMA inspectionexamples/short_living_process.tsshows a configuration tuned for a short-lived script or jobexamples/long_living_process.tsshows how the long-running-process defaults fit a web service style setup
- the returned connection is a normal Bun
SQLclient close()is wrapped so optimize behavior and lifecycle hooks run in the right order- repeated
close()calls are guarded so cleanup does not run multiple times - configuration is resolved once up front so the runtime code can work with a complete option object
For most applications:
- start with
buildSQLite()orbuildSQLite({ path }) - keep the defaults unless you have a concrete reason to change them
- use
Optimize.OnExitfor short-lived jobs - use the default optimize behavior for long-running services
- treat
SQLiteErroras a configuration problem and all other errors as Bun SQL runtime failures
