TypeScript
---
title: Drizzle on D1
description: Deploy committed SQLite migrations to D1 and query the database from a Worker with Drizzle.
---
D1 uses a native Worker binding, so it needs neither a connection string nor Hyperdrive. For external Postgres/MySQL, use [Worker + Drizzle](/cloudflare/data/drizzle).
## Install
```sh
pnpm add @alchemy.run/drizzle [email protected] @effect/sql-d1
pnpm add -D [email protected]
```
These versions match Alchemy's current Effect-compatible Drizzle integration.
## Define the schema
```typescript
// src/schema.ts
import { integer, sqliteTable, text } from "drizzle-orm/sqlite-core";
export const users = sqliteTable("users", {
id: integer("id").primaryKey({ autoIncrement: true }),
name: text("name").notNull(),
});
```
## Configure generation
```typescript
// drizzle.config.ts
import { defineConfig } from "drizzle-kit";
export default defineConfig({
dialect: "sqlite",
schema: "./src/schema.ts",
out: "./drizzle",
});
```
## Generate and commit SQL
```sh
pnpm exec drizzle-kit generate
git diff -- drizzle
git add src/schema.ts drizzle.config.ts drizzle
git commit -m "Create users table"
```
Generate migrations when the schema changes; CI deploys these committed files.
## Wire the schema into the database
```typescript
// src/Db.ts
import * as Cloudflare from "@alchemy.run/cloudflare";
export const Database = Cloudflare.D1.Database("Database", {
migrations: "./drizzle",
});
```
D1 applies pending SQL during deployment. [D1 migrations](/cloudflare/data/d1#migrations) covers database-specific behavior; [Drizzle migrations](/sql/drizzle/migrations) covers file generation.
## Query from the Worker with `Drizzle.D1`
```typescript
// src/Api.ts
import * as Cloudflare from "@alchemy.run/cloudflare";
import * as Drizzle from "@alchemy.run/drizzle";
import * as Effect from "effect/Effect";
import * as HttpServerResponse from "effect/http/HttpServerResponse";
import { Database } from "./Db.ts";
import { users } from "./schema.ts";
export default class Api extends Cloudflare.Worker<Api>()(
"Api",
{ main: import.meta.url },
Effect.gen(function* () {
const database = yield* Database;
const binding = yield* Cloudflare.D1.QueryDatabase(database);
const db = yield* Drizzle.D1(binding);
return {
fetch: Effect.gen(function* () {
const rows = yield* db.select().from(users).pipe(Effect.orDie);
return yield* HttpServerResponse.json({ users: rows });
}),
};
}).pipe(Effect.provide(Cloudflare.D1.QueryDatabaseBinding)),
) {}
```
## Define the stack
```typescript
// alchemy.run.ts
import * as Alchemy from "alchemy";
import * as Cloudflare from "@alchemy.run/cloudflare";
import * as Effect from "effect/Effect";
import Api from "./src/Api.ts";
export default Alchemy.Stack(
"D1Drizzle",
{ providers: Cloudflare.providers(), state: Alchemy.localState() },
Effect.gen(function* () {
const api = yield* Api;
return { url: api.url };
}),
);
```
## Deploy
```sh
pnpm alchemy deploy
curl "$WORKER_URL"
# {"users":[]}
```
Set `WORKER_URL` to the deployed URL; keep `.alchemy` for later deployments and cleanup.
## Local dev
```sh
pnpm alchemy dev
```
The same Worker and binding use local D1 during development.
## Queries and transactions
[Drizzle on D1](/sql/drizzle/d1) covers relational queries; [Effect SQL on D1](/sql/effect-sql/d1) covers tagged-template SQL. D1 uses batch operations rather than interactive transactions or streaming queries.
## Where next
See [SQL databases](/sql/databases), the [D1 resource](/cloudflare/data/d1), or the [existing D1 example](https://github.com/alchemy-run/alchemy/tree/main/examples/cloudflare-d1-drizzle).