# Use Cloudflare D1 with ViteHub and Drizzle

Use a native D1 binding when your app runs on Cloudflare Workers. ViteHub
writes the binding into the generated Worker config.

Start with [Save your first Database row](https://vitehub.dev/docs/database/get-started) and keep
its schema and notes routes. Create a new D1 database and note its name and
ID. Do not use a database that has its own migration history.

## Select Cloudflare and identify the database

Set `preset: 'cloudflare'` and `database: true` in your ViteHub config. Add
this `cloudflare` block to the Database Definition:

```ts [server/databases/config.ts]
import { defineDatabase } from 'vite-hub/database'
import { integer, sqliteTable, text } from 'drizzle-orm/sqlite-core'

export default defineDatabase({
	cloudflare: {
		binding: 'DB',
		databaseId: process.env.CLOUDFLARE_D1_DATABASE_ID,
		databaseName: process.env.CLOUDFLARE_D1_DATABASE_NAME,
	},
	schema: {
		notes: sqliteTable('notes', {
			id: integer('id').primaryKey({ autoIncrement: true }),
			title: text('title').notNull(),
		}),
	},
})
```

Set both variables before you build. The binding name alone does not identify
the remote database.

## Generate and apply the D1 migration

Install Wrangler if you do not have it, then generate and build:

```bash [Terminal]
pnpm add -D wrangler
pnpm vitehub database generate
pnpm build
```

Before you apply SQL, check the ID, name, binding, and migrations directory in
the `d1_databases` entry of `.output/server/wrangler.json`.

```bash [Terminal]
pnpm wrangler d1 migrations list "$CLOUDFLARE_D1_DATABASE_NAME" \
  --remote --config .output/server/wrangler.json
pnpm wrangler d1 migrations apply "$CLOUDFLARE_D1_DATABASE_NAME" \
  --remote --config .output/server/wrangler.json
```

Wrangler applies migrations for native bindings, not `vitehub database migrate`.
Review each pending migration first.

Deploy with `pnpm exec nitro deploy --prebuilt`. Call the notes POST and GET
routes on the Worker URL, and check that the row is in D1.

## Separate local and remote development

Without `cloudflare.http`, local dev uses SQLite. A local row does not prove
that remote D1 has the same schema or data.

Use [D1 over HTTP](https://vitehub.dev/docs/database/hosts) only on purpose. It shares
Cloudflare's admin API limits. For steady traffic from outside Workers, use an
authenticated proxy or a hosted database such as [Turso](https://vitehub.dev/docs/guides/turso).

See [Database configuration](https://vitehub.dev/docs/database/configure) for resource inheritance
and the [Cloudflare D1 docs](https://developers.cloudflare.com/d1/) for quotas
and migration tools.

<!-- vitehub-docs:section-links -->
## In this section

- [Overview](https://vitehub.dev/raw/docs/guides.md)
- [Agents](https://vitehub.dev/raw/docs/guides/deploy-agent-cloudflare-vercel.md)
- [Chat bots](https://vitehub.dev/raw/docs/guides/slack-ai-bot.md)
- [Storage](https://vitehub.dev/raw/docs/guides/upstash-redis.md)
- Databases
  - [Turso](https://vitehub.dev/raw/docs/guides/turso.md)
  - [Neon Postgres](https://vitehub.dev/raw/docs/guides/neon-postgres.md)
  - [Cloudflare D1](https://vitehub.dev/raw/docs/guides/cloudflare-d1.md) (this page)
- [Server work](https://vitehub.dev/raw/docs/guides/resend.md)
- [Better Auth on Cloudflare](https://vitehub.dev/raw/docs/guides/better-auth-cloudflare.md)
- [Concepts](https://vitehub.dev/raw/docs/guides/rate-limiting-algorithms.md)

Next: [Cloudflare R2](https://vitehub.dev/raw/docs/guides/cloudflare-r2.md)

All pages: [llms.txt](https://vitehub.dev/llms.txt)
