Skip to content

Commit f6b9308

Browse files
authored
[D1] D1 Prisma ORM tutorial update (#22487)
* Minor fix to Prisma binding output * Updating the tutorial with more details * Adding explicit example in the note component.
1 parent 846fb64 commit f6b9308

File tree

1 file changed

+40
-20
lines changed
  • src/content/docs/d1/tutorials/d1-and-prisma-orm

1 file changed

+40
-20
lines changed

src/content/docs/d1/tutorials/d1-and-prisma-orm/index.mdx

Lines changed: 40 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
updated: 2024-03-27
2+
updated: 2025-05-16
33
difficulty: Beginner
44
content_type: Tutorial
55
pcx_content_type: tutorial
@@ -11,7 +11,7 @@ languages:
1111
- SQL
1212
---
1313

14-
import { WranglerConfig } from "~/components";
14+
import { WranglerConfig, FileTree } from "~/components";
1515

1616
## What is Prisma ORM?
1717

@@ -39,9 +39,7 @@ npm create cloudflare@latest prisma-d1-example -- --type hello-world
3939
In your terminal, you will be asked a series of questions related your project:
4040

4141
1. Answer `yes` to using TypeScript.
42-
2. Answer `yes` to deploying your Worker.
43-
44-
Once you deploy your Worker, you should be able to preview your Worker at `https://prisma-d1-example.USERNAME.workers.dev`, which displays "Hello World" in the browser.
42+
2. Answer `no` to deploying your Worker.
4543

4644
### 2. Initialize Prisma ORM
4745

@@ -88,6 +86,9 @@ generator client {
8886
+ previewFeatures = ["driverAdapters"]
8987
}
9088
```
89+
:::note
90+
Do not specify an `output` destination in the `generator client` block. Instead, allow prisma to generate the files in the default output path.
91+
:::
9192

9293
### 3. Create your D1 database
9394

@@ -102,14 +103,18 @@ npx wrangler d1 create prisma-demo-db
102103
You should receive the following output on your terminal:
103104

104105
```
105-
✅ Successfully created DB 'prisma-demo-db' in region EEUR
106-
Created your database using D1's new storage backend. The new storage backend is not yet recommended for production workloads, but backs up your data via
107-
point-in-time restore.
108-
109-
[[d1_databases]]
110-
binding = "DB" # i.e. available in your Worker on env.DB
111-
database_name = "prisma-demo-db"
112-
database_id = "__YOUR_D1_DATABASE_ID__"
106+
✅ Successfully created DB 'prisma-demo-db' in region WEUR
107+
Created your new D1 database.
108+
109+
{
110+
"d1_databases": [
111+
{
112+
"binding": "DB",
113+
"database_name": "prisma-demo-db",
114+
"database_id": "<D1_DATABASE_ID>"
115+
}
116+
]
117+
}
113118
```
114119

115120
You now have a D1 database in your Cloudflare account with a binding to your Cloudflare Worker.
@@ -127,12 +132,12 @@ compatibility_flags = ["nodejs_compat"]
127132
[[d1_databases]]
128133
binding = "DB" # i.e. available in your Worker on env.DB
129134
database_name = "prisma-demo-db"
130-
database_id = "__YOUR_D1_DATABASE_ID__"
135+
database_id = "<D1_DATABASE_ID>"
131136
```
132137

133138
</WranglerConfig>
134139

135-
`__YOUR_D1_DATABASE_ID__` should be replaced with the database ID of your D1 instance. If you were not able to fetch this ID from the terminal output, you can also find it in the [Cloudflare dashboard](https://dash.cloudflare.com/), or by running `npx wrangler d1 info prisma-demo-db` in your terminal.
140+
Replace `<D1_DATABASE_ID>` with the database ID of your D1 instance. If you were not able to fetch this ID from the terminal output, you can also find it in the [Cloudflare dashboard](https://dash.cloudflare.com/), or by running `npx wrangler d1 info prisma-demo-db` in your terminal.
136141

137142
Next, you will create a database table in the database to send queries to D1 using Prisma ORM.
138143

@@ -152,10 +157,11 @@ Answer `yes` to creating a new folder called `migrations`.
152157

153158
The command has now created a new directory called `migrations` and an empty file called `0001_create_user_table.sql` inside of it:
154159

155-
```
156-
migrations/
157-
└── 0001_create_user_table.sql
158-
```
160+
<FileTree>
161+
- prisma-d1-example
162+
- migrations
163+
- **0001_create_user_table.sql**
164+
</FileTree>
159165

160166
Next, you need to add the SQL statement that will create a `User` table to that file.
161167

@@ -196,7 +202,9 @@ You now need to use the `wrangler d1 migrations apply` command to send this SQL
196202
- `--local`: Executes the statement against a _local_ version of D1. This local version of D1 is a SQLite database file that will be located in the `.wrangler/state` directory of your project. Use this approach when you want to develop and test your Worker on your local machine. Refer to [Local development](/d1/best-practices/local-development/) to learn more.
197203
- `--remote`: Executes the statement against your _remote_ version of D1. This version is used by your _deployed_ Cloudflare Workers. Refer to [Remote development](/d1/best-practices/remote-development/) to learn more.
198204

199-
In this tutorial, you will do local and remote development. You will test the Worker locally and deploy your Worker afterwards. Open your terminal, and run both commands:
205+
In this tutorial, you will do both local and remote development. You will test the Worker locally, then deploy your Worker afterwards.
206+
207+
Open your terminal, and run both commands:
200208

201209
```sh
202210
# For the local database
@@ -224,6 +232,18 @@ npx wrangler d1 execute prisma-demo-db --command "INSERT INTO \"User\" (\"email
224232
('[email protected]', 'Jane Doe (Remote)');" --remote
225233
```
226234

235+
:::note
236+
If you receive an error to the effect of `Unknown arguments: (\email\,, \name\)...`, you may need to escape the double quotes with backticks (`) instead of backslashes (\\).
237+
238+
Your Wrangler command will then look like:
239+
240+
```sh
241+
# Escape with ` instead of \
242+
npx wrangler d1 execute prisma-demo-db --command "INSERT INTO `"User`" (`"email`", `"name`") VALUES
243+
('[email protected]', 'Jane Doe (Local)');" --<FLAG>
244+
```
245+
:::
246+
227247
### 5. Query your database from the Worker
228248
229249
To query your database from the Worker using Prisma ORM, you need to:

0 commit comments

Comments
 (0)