Skip to content

Bun-SQLite & D1 Store adapters

inseam ships two first-party Store adapters: @inseam/adapter-store-bun-sqlite for Bun-runtime hosts and @inseam/adapter-store-d1 for Cloudflare Workers. Pick whichever matches the runtime; the rest of inseam doesn’t change. Both adapters share a parameterized conformance harness exported from @inseam/store-contract/testing.

Each adapter wraps a single driver as a StoreAdapter. They are interchangeable from core’s perspective: same contract, same error envelope, same migration semantics. The two exist because the two target runtimes have non-overlapping driver shapes (sync vs. async, embedded vs. binding-supplied) and we want each to be idiomatic on its own runtime rather than papered over.

Both adapters:

  • Throw only AdapterError (with cause and adapterKind) — higher-level error types are core’s concern.
  • Implement batch as a real atomic unit — a SQLite transaction for bun-sqlite, db.batch(...) for D1.
  • Resolve batch([]) to [] without touching the driver and resolve health() to false (never throws) on a broken connection.
  • bunSqliteAdapter(path) opens or creates a SQLite database at path (or ":memory:"). Applies WAL, foreign_keys=ON, and a fixed busy_timeout at construction. Wraps the sync bun:sqlite driver to the async StoreAdapter shape. kind: "bun-sqlite".
  • d1Adapter(db) captures a Worker-injected D1Database binding and performs no driver work at construction. Normalizes D1’s success: false envelopes into thrown AdapterErrors; on batch failure, attaches batchIndex (from D1 when available, else 0) to the wrapped error. kind: "d1".

Exact factory and method contracts: @inseam/adapter-store-bun-sqlite API · @inseam/adapter-store-d1 API · arch/adapters-bun-d1-sqlite/spec.md.

Pick by runtime:

  • Bun host (CLI, server, local desktop) → bunSqliteAdapter. Synchronous driver under the hood, lowest possible latency, single-file durability.
  • Cloudflare Workerd1Adapter. Async by nature, runs against the D1 binding the Worker supplies.

If neither fits — a Node host, a Postgres backend, a remote SQLite proxy — write a new adapter against @inseam/store-contract and run the conformance harness against it before shipping.

  • LLM summary — dense reference for agents.
  • Store — the port these adapters implement.
  • arch/adapters-bun-d1-sqlite/design.md — why these two adapters and why bun:sqlite for the local case.
  • arch/adapters-bun-d1-sqlite/spec.md — per-method behavior, error wrapping, acceptance criteria.