> ## Documentation Index
> Fetch the complete documentation index at: https://a5eb32f-docs-2.manicule.dev/llms.txt
> Use this file to discover all available pages before exploring further.
> For every page in one file, fetch https://a5eb32f-docs-2.manicule.dev/llms-full.txt (large).
> To search and read these docs as tools, connect the MCP server at https://a5eb32f-docs-2.manicule.dev/api/mcp.
> Every agent surface: https://a5eb32f-docs-2.manicule.dev/.well-known/llms-manifest.json

# Connect a Worker to Cloudflare D1 (/databases/cloudflare-d1)

<!-- agent-signals: reading_time_min: 2 · est_tokens: 696 · updated: 2026-11-25 -->
Related: [Choose a Database for Your Project](/databases/overview.md), [Connect Your App to Neon Postgres](/databases/neon-postgres.md), [Connect Your App to Supabase](/databases/supabase-postgres.md), [Review and Apply Database Migrations](/databases/migrations.md)

> Bind a D1 database to your Cloudflare Worker, define a SQLite schema with Drizzle, and verify the connection.

Cloudflare D1 is a serverless relational database that uses SQLite and integrates with Cloudflare Workers. Choose it for a Worker-backed app that needs persistent records without a separate Postgres service. Your Worker needs a binding to the actual D1 database. Selecting D1 in Cloud Studio doesn't supply that binding.

## Bind your project's database

<Steps>
<Step title="Create a D1 database">Create the database in Cloudflare for the application you plan to deploy. Keep its name and ID handy so you can distinguish it from other D1 databases in your account.</Step>

<Step title="Bind it to the Worker">Add the D1 binding to the Worker's configuration. Use the same binding name in your code and in the configuration for the environment you're testing. A binding gives the Worker access to D1 without putting a database connection string in the frontend.</Step>

<Step title="Apply the schema and check a query">Run the SQLite migration against the database the Worker binding points to. Then run a query from the Worker to confirm it can reach the database and that the expected table exists.</Step>
</Steps>

## Define the schema with Drizzle

Use Drizzle's SQLite schema helpers for typed queries against D1. This example stores prices as integer cents and maps the integer timestamp to a date in TypeScript.

```typescript
import { sqliteTable, text, integer } from "drizzle-orm/sqlite-core";

export const products = sqliteTable("products", {
  id: text("id").primaryKey(),
  name: text("name").notNull(),
  priceInCents: integer("price_in_cents").notNull(),
  createdAt: integer("created_at", { mode: "timestamp" }).notNull(),
});
```

The TypeScript definition describes the table for your queries. Apply a migration before using it to read or write data. If a query reports a missing table, check both the applied migrations and the target binding before changing the query. A migration applied locally doesn't change the remote database.

## Use SQLite features

Use joins to read related records, indexes for columns your queries filter on, and SQLite JSON operators when a record contains JSON. When multiple statements need to succeed or fail together, use D1's transactional batch API. For a read-heavy app, evaluate read replication against your own workload rather than assuming every query runs near the user.

<Note>The D1 binding belongs to the Worker runtime. A working browser preview doesn't prove that the deployed Worker has the right binding or that its D1 database has the expected schema.</Note>

## Next

* [Review and apply database migrations](/databases/migrations).
* [Deploy your app to BhaiCode Edge](/deployments/bhaicode-edge).
