Use Cloudflare D1 with DaloyJS
Cloudflare D1 is a serverless SQLite-compatible database built into Cloudflare Workers. You access it through a Worker binding (no network driver, no auth token, no TCP), making it the lowest-friction database for the Cloudflare adapter.
1. Provision via Wrangler
Add the returned binding to your wrangler.toml:
2. Type the binding
Use a regular .ts module (not a .d.ts, which skipLibCheck would silently skip) so the import below resolves and the shape stays type-checked:
3. Decorate the app per-request
D1 bindings live on env, not process.env, so decorate inside the Worker's fetch handler and call app.fetch(req) directly:
- 01Worker fetch(req, env)env.DB is the D1 binding
- 02app.decorate('db', env.DB)attach the binding to app state
- 03app.fetch(req)route handler reads state.db
- 04state.db.prepare(...).all()query D1, no network driver
4. Augment app state
Add the declare module block to the Worker module itself, not to a separate .d.ts file. Declaration files are exempt from type-checking when skipLibCheck is on (the scaffolded default), so a mistake inside a .d.ts fails silently and state.db quietly degrades to any.
Migrations
With Drizzle ORM
With Prisma
Prisma supports D1 via the D1 Driver Adapter. Construct the adapter inside the Worker handler since it needs the runtime binding.
Limitations to know
- D1 only runs in Cloudflare Workers, no Node.js, Lambda, or Edge runtime support.
- Local development uses
wrangler devwith a local SQLite file; behavior is close but not identical to production. - For multi-runtime portability, prefer Turso (libSQL) instead.
See also Turso, Neon, and the database hosting overview.