Cloudflare D1
Bind D1, resolve it lazily and apply migrations with wrangler.
D1 is the database for Cloudflare Workers deployments. A binding only exists
inside a Worker request, so the adapter is always wrapped in
lazyStatusDatabase() — the configuration module must stay importable in a
build step, a CLI command and a test.
The Worker publishes the binding before the framework handles the request:
A standalone dev server cannot manufacture a Cloudflare binding, and
silently writing somewhere else would hide the fact that the deployed
database was never touched. Use wrangler dev for a local D1.
Why lazy
Status configuration is a module-level value. Without deferral, a missing
binding throws while the module is being imported, which takes down the whole
framework process — including pages that never touch the database — and makes
every template import order-sensitive. lazyStatusDatabase() moves the
failure to the request that actually needs persistence, where it is reported
as a normal API error.
A failed resolution is not cached: a missing binding fixed by a redeploy works on the next request, without a restart.
Binding it
Migrations
The generated project copies the bundled SQL into migrations/ and points
migrations_dir at it, so wrangler owns the migration state:
D1StatusDatabase can also migrate itself, which is what a one-off
deployment job would use:
Request handlers never migrate automatically. d1(binding, { migrate: true })
exists for local and test databases; leave it off for a remote one.
Options
A structural type — importing Heyo Status does not require Workers types.
Defaults to false. Applies the bundled migrations on construction.
Inserts use binding.batch() when it is available, falling back to parallel
statements otherwise.
Limitations on Workers
node:netandnode:tlsare unavailable, sotcpandsslmonitors record… monitoring is unavailable in this runtime.and report down. Usehttpanddns, which work everywhere.- The snapshot cache is per-isolate. That is fine — it still absorbs bursts — but it is not a shared cache.
- D1 has per-query and per-invocation limits; the default 90-day window with compaction keeps the snapshot query well inside them.