Skip to content

D1 Database

D1 is Cloudflare’s serverless SQL database. wrangler-deploy creates a separate D1 database per stage. No external database needed.

wrangler-deploy.config.ts
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.jsonc
{
"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.

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);
},
};

After applying, run migrations against the stage’s D1:

Terminal window
wd apply --stage staging
wrangler d1 migrations apply my-database-staging

Each stage gets its own D1 database. wd destroy deletes it completely.