D1 Database
D1 is Cloudflare’s serverless SQL database. wrangler-deploy creates a separate D1 database per stage. No external database needed.
Config
Section titled “Config”import { defineConfig, d1, workerEnv } from "wrangler-deploy";
const db = d1("my-database");
export const api = workerEnv({ DB: db,});
export default defineConfig({ version: 1, workers: ["."], resources: { "my-database": { type: "d1", bindings: { ".": "DB" }, }, },});Wrangler config
Section titled “Wrangler config”{ "d1_databases": [ { "binding": "DB", "database_name": "my-database", "database_id": "placeholder" }, ],}The database_id placeholder works for wrangler dev. wrangler-deploy replaces it with the real ID in rendered configs.
Worker code
Section titled “Worker code”import type { api } from "../wrangler-deploy.config.ts";type Env = typeof api.Env;// Env.DB is typed as D1Database
export default { async fetch(req: Request, env: Env) { const { results } = await env.DB.prepare("SELECT * FROM users LIMIT 10").all(); return Response.json(results); },};Migrations
Section titled “Migrations”After applying, run migrations against the stage’s D1:
wd apply --stage stagingwrangler d1 migrations apply my-database-stagingEphemeral
Section titled “Ephemeral”Each stage gets its own D1 database. wd destroy deletes it completely.