Skip to content
GitHubXDiscordRSS

D1Database

Learn how to create, query, and manage Cloudflare D1 Databases using Alchemy for serverless SQL databases.

The D1Database component lets you add Cloudflare D1 Databases to your app.

import { D1Database } from "alchemy/cloudflare";
const db = await D1Database("my-db", {
name: "my-db",
});
const worker = await Worker("my-worker", {
entrypoint: "./worker.ts",
bindings: {
DB: db,
},
});

Create a database with SQL migrations.

import { D1Database } from "alchemy/cloudflare";
const db = await D1Database("users-db", {
name: "users-db",
migrationsDir: "./migrations",
});

By default, D1Database uses a table named d1_migrations to track migration history. You can customize this table name using the migrationsTable property, which is useful for compatibility with tools like Drizzle:

import { D1Database } from "alchemy/cloudflare";
// Use custom migration table (compatible with Drizzle)
const db = await D1Database("my-db", {
name: "my-db",
migrationsDir: "./migrations",
migrationsTable: "drizzle_migrations",
});

If you don’t specify migrationsTable, it defaults to d1_migrations:

import { D1Database } from "alchemy/cloudflare";
// Uses default migration table 'd1_migrations'
const db = await D1Database("my-db", {
name: "my-db",
migrationsDir: "./migrations",
});

Create a database with a specific location hint for optimal performance.

import { D1Database } from "alchemy/cloudflare";
const db = await D1Database("eu-db", {
name: "eu-db",
primaryLocationHint: "weur",
readReplication: {
mode: "auto",
},
});

Create a database by cloning data from an existing database. There are three ways to specify the source database:

import { D1Database } from "alchemy/cloudflare";
const clonedDb = await D1Database("clone-db", {
name: "clone-db",
clone: { id: "existing-db-uuid" },
});
import { D1Database } from "alchemy/cloudflare";
const clonedDb = await D1Database("clone-db", {
name: "clone-db",
clone: { name: "source-db-name" },
});
import { D1Database } from "alchemy/cloudflare";
// First create or get the source database
const sourceDb = await D1Database("source-db", {
name: "source-db",
});
// Then create a new database as a clone of the source
const clonedDb = await D1Database("clone-db", {
name: "clone-db",
clone: sourceDb,
});
import { Worker, D1Database } from "alchemy/cloudflare";
const db = await D1Database("my-db", {
name: "my-db",
});
await Worker("my-worker", {
name: "my-worker",
script: "console.log('Hello, world!')",
bindings: {
DB: db,
},
});