Skip to content

Commit fecc9c3

Browse files
authored
Update Cloudflare D1 Guides (#7273)
1 parent c818511 commit fecc9c3

2 files changed

Lines changed: 85 additions & 189 deletions

File tree

content/200-orm/050-overview/500-databases/950-cloudflare-d1.mdx

Lines changed: 10 additions & 107 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ Many aspects of using Prisma ORM with D1 are just like using Prisma ORM with any
3434

3535
There are a number of differences between D1 and SQLite to consider. You should be aware of the following when deciding to use D1 and Prisma ORM:
3636

37-
- **Making schema changes**. As of [v6.6.0](https://pris.ly/release/6.6.0) and with a `prisma.config.ts` file, you can use `prisma db push`. However, if you prefer a Cloudflare first approach, you can use D1's [migration system](https://developers.cloudflare.com/d1/reference/migrations/) and the [`prisma migrate diff`](/orm/reference/prisma-cli-reference#migrate-diff) command for your migration workflows. See the [Schema migrations with Prisma ORM on D1](#schema-migrations-with-prisma-orm-on-d1) section below for more information.
37+
- **Making schema changes**. With Prisma ORM 7, you need to use D1's [migration system](https://developers.cloudflare.com/d1/reference/migrations/) via the Wrangler CLI combined with the [`prisma migrate diff`](/orm/reference/prisma-cli-reference#migrate-diff) command for your migration workflows. See the [Schema migrations with Prisma ORM on D1](#schema-migrations-with-prisma-orm-on-d1) section below for more information.
3838
- **Local and remote D1 (SQLite) databases**. Cloudflare provides local and remote versions of D1. The [local](https://developers.cloudflare.com/d1/build-with-d1/local-development/) version is managed using the `--local` option of the `wrangler d1` CLI and is located in `.wrangler/state`. The [remote](https://developers.cloudflare.com/d1/build-with-d1/remote-development/) version is managed by Cloudflare and is accessed via HTTP.
3939

4040
## How to connect to D1 in Cloudflare Workers or Cloudflare Pages
@@ -45,106 +45,13 @@ If you want to deploy a Cloudflare Worker with D1 and Prisma ORM, follow these [
4545

4646
## Schema migrations with Prisma ORM on D1
4747

48-
You can use two approaches for migrating your database schema with Prisma ORM and D1:
49-
- Using `prisma db push` via a driver adapter in `prisma.config.ts`
50-
- Using the Wrangler CLI
48+
With Prisma ORM 7, the recommended approach for managing database schema migrations with Cloudflare D1 is to use the Wrangler CLI combined with `prisma migrate diff`.
5149

52-
### Using Prisma Migrate via a driver adapter in Prisma Config
50+
### Using the Wrangler CLI with `prisma migrate diff`
5351

54-
As of [Prisma v6.6.0](https://github.com/prisma/prisma/releases/tag/6.6.0) we added support for the Prisma Config file. You can use the Prisma Config file to perform `prisma db push` and make changes to your database schema. You can learn more about [the Prisma Config file in our reference page](/orm/reference/prisma-config-reference).
52+
Cloudflare D1 comes with its own [migration system](https://developers.cloudflare.com/d1/reference/migrations/) that works via the `wrangler d1` CLI commands.
5553

56-
#### 1. Install the Prisma D1 driver adapter
57-
58-
Run this command in your terminal:
59-
60-
```terminal
61-
npm install @prisma/adapter-d1
62-
```
63-
64-
#### 2. Set environment variables
65-
66-
In order to set up the D1 adapter, you'll need to add a few secrets to a `.env` file:
67-
68-
- `CLOUDFLARE_ACCOUNT_ID`: Your Cloudflare account ID, fetched via `npx wrangler whoami`.
69-
- `CLOUDFLARE_DATABASE_ID`: Retrieved during D1 database creation. If you have an existing D1 database, you can use `npx wrangler d1 list` and `npx wrangler d1 info <database_name>` to get the ID.
70-
- `CLOUDFLARE_D1_TOKEN`: This API token is used by Prisma ORM to communicate with your D1 instance directly. To create it, follow these steps:
71-
1. Visit https://dash.cloudflare.com/profile/api-tokens
72-
2. Click **Create Token**
73-
3. Click **Custom token** template
74-
4. Fill out the template: Make sure you use a recognizable name and add the **Account / D1 / Edit** permission.
75-
5. Click **Continue to summary** and then **Create Token**.
76-
77-
You can then add these to your `.env` file or use them directly if they are stored in a different secret store:
78-
79-
```bash file=.env
80-
CLOUDFLARE_ACCOUNT_ID="0773..."
81-
CLOUDFLARE_DATABASE_ID="01f30366-..."
82-
CLOUDFLARE_D1_TOKEN="F8Cg..."
83-
```
84-
85-
#### 3. Set up Prisma Config file
86-
87-
Make sure that you have a [`prisma.config.ts`](/orm/reference/prisma-config-reference) file for your project. Then, set up the [migration driver adapter](/orm/reference/prisma-config-reference#adapter-removed) to reference D1:
88-
89-
```ts file=prisma.config.ts
90-
import type { PrismaConfig } from 'prisma';
91-
import { PrismaD1 } from '@prisma/adapter-d1';
92-
93-
// import your .env file
94-
import 'dotenv/config';
95-
96-
export default {
97-
// add-start
98-
experimental: {
99-
adapter: true,
100-
},
101-
// add-end
102-
schema: 'prisma/schema.prisma',
103-
// add-start
104-
async adapter() {
105-
return new PrismaD1({
106-
CLOUDFLARE_D1_TOKEN: process.env.CLOUDFLARE_D1_TOKEN!,
107-
CLOUDFLARE_ACCOUNT_ID: process.env.CLOUDFLARE_ACCOUNT_ID!,
108-
CLOUDFLARE_DATABASE_ID: process.env.CLOUDFLARE_DATABASE_ID!,
109-
});
110-
},
111-
// add-end
112-
} satisfies PrismaConfig;
113-
```
114-
115-
:::note
116-
117-
As of [Prisma ORM v6.11.0](https://github.com/prisma/prisma/releases/tag/6.11.0), the D1 adapter has been renamed from `PrismaD1HTTP` to `PrismaD1`.
118-
119-
:::
120-
121-
#### 4. Migrate your database
122-
123-
Prisma Migrate now will run migrations against your remote D1 database based on the configuration provided in `prisma.config.ts`.
124-
125-
To update the remote schema with this workflow, run the following command:
126-
127-
```terminal
128-
npx prisma db push
129-
```
130-
131-
:::note
132-
133-
Note that for querying the database, you keep using the `PrismaD1` driver adapter from the `@prisma/adapter-d1` package:
134-
135-
```ts
136-
import { PrismaD1 } from '@prisma/adapter-d1'
137-
```
138-
139-
:::
140-
141-
### Using the Wrangler CLI
142-
143-
Cloudflare D1 comes with its own [migration system](https://developers.cloudflare.com/d1/reference/migrations/). While we recommend that you use the [native Prisma Migrate workflow](#using-prisma-migrate-via-a-driver-adapter-in-prisma-config), this migration system via the `wrangler d1 migrations` command is available.
144-
145-
This command doesn't help you in figuring out the SQL statements for creating your database schema that need to be put _inside_ of these migration files though. If you want to query your database using Prisma Client, it's important that your database schema maps to your Prisma schema, this is why it's recommended to generate the SQL statements from your Prisma schema.
146-
147-
When using D1, you can use the [`prisma migrate diff`](/orm/reference/prisma-cli-reference#migrate-diff) command for that purpose.
54+
The Wrangler CLI provides the structure for migrations, but you need to generate the SQL statements from your Prisma schema. This is where [`prisma migrate diff`](/orm/reference/prisma-cli-reference#migrate-diff) comes in - it generates SQL statements based on your Prisma schema that you can then apply using Wrangler.
14855

14956
#### Creating an initial migration
15057

@@ -190,15 +97,15 @@ For the initial migration, you can use the special `--from-empty` option though:
19097
```terminal
19198
npx prisma migrate diff \
19299
--from-empty \
193-
--to-schema-datamodel ./prisma/schema.prisma \
100+
--to-schema ./prisma/schema.prisma \
194101
--script \
195102
--output migrations/0001_create_user_table.sql
196103
```
197104

198105
The command above uses the following options:
199106

200107
- `--from-empty`: The source for the SQL statement is an empty schema.
201-
- `--to-schema-datamodel ./prisma/schema.prisma`: The target for the SQL statement is the data model in `./prisma/schema.prisma`.
108+
- `--to-schema ./prisma/schema.prisma`: The target for the SQL statement is the schema in `./prisma/schema.prisma`.
202109
- `--script`: Output the result as SQL. If you omit this option, the "migration steps" will be generated in plain English.
203110
- `--output migrations/0001_create_user_table.sql`: Store the result in `migrations/0001_create_user_table.sql`.
204111

@@ -281,15 +188,15 @@ As explained above, you now need to use `--from-local-d1` instead of `--from-emp
281188
```terminal
282189
npx prisma migrate diff \
283190
--from-local-d1 \
284-
--to-schema-datamodel ./prisma/schema.prisma \
191+
--to-schema ./prisma/schema.prisma \
285192
--script \
286193
--output migrations/0002_create_post_table.sql
287194
```
288195

289196
The command above uses the following options:
290197

291198
- `--from-local-d1`: The source for the SQL statement is the local D1 database file.
292-
- `--to-schema-datamodel ./prisma/schema.prisma`: The target for the SQL statement is the data model in `./prisma/schema.prisma`.
199+
- `--to-schema ./prisma/schema.prisma`: The target for the SQL statement is the schema in `./prisma/schema.prisma`.
293200
- `--script`: Output the result as SQL. If you omit this option, the "migration steps" will be generated in plain English.
294201
- `--output migrations/0002_create_post_table.sql`: Store the result in `migrations/0002_create_post_table.sql`.
295202

@@ -325,8 +232,4 @@ npx wrangler d1 migrations apply __YOUR_DATABASE_NAME__ --remote
325232

326233
### Transactions not supported
327234

328-
Cloudflare D1 currently does not support transactions (see the [open feature request](https://github.com/cloudflare/workers-sdk/issues/2733)). As a result, Prisma ORM does not support transactions for Cloudflare D1. When using Prisma's D1 adapter, implicit & explicit transactions will be ignored and run as individual queries, which breaks the guarantees of the ACID properties of transactions.
329-
330-
### Prisma Migrate only supports remote D1 databases
331-
332-
The Wrangler CLI can distinguish between local and remote D1 (i.e. SQLite) database instances via the `--local` and `--remote` options. This distinction is currently not available with the [native Prisma Migrate workflow](#using-prisma-migrate-via-a-driver-adapter-in-prisma-config).
235+
Cloudflare D1 currently does not support transactions (see the [open feature request](https://github.com/cloudflare/workers-sdk/issues/2733)). As a result, Prisma ORM does not support transactions for Cloudflare D1. When using Prisma's D1 adapter, implicit & explicit transactions will be ignored and run as individual queries, which breaks the guarantees of the ACID properties of transactions.

0 commit comments

Comments
 (0)