Skip to content

Commit fddd1c4

Browse files
OxyjunMaddy-Cloudflare
authored andcommitted
[D1] Prisma ORM D1 tutorial Deploy to Cloudflare button (cloudflare#22859)
* Updating to use GitHubCode where possible. * Adding Deploy to Cloudflare button. * Catching typo * Update src/content/docs/d1/tutorials/d1-and-prisma-orm/index.mdx Co-authored-by: Maddy <[email protected]> --------- Co-authored-by: Maddy <[email protected]>
1 parent e1540d4 commit fddd1c4

File tree

1 file changed

+52
-49
lines changed
  • src/content/docs/d1/tutorials/d1-and-prisma-orm

1 file changed

+52
-49
lines changed

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

Lines changed: 52 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ languages:
1111
- SQL
1212
---
1313

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

1616
## What is Prisma ORM?
1717

@@ -21,14 +21,24 @@ To learn more about Prisma ORM, refer to the [Prisma documentation](https://www.
2121

2222
## Query D1 from a Cloudflare Worker using Prisma ORM
2323

24-
This example shows you how to set up and deploy a Cloudflare Worker that is accessing a D1 database from scratch.
24+
This tutorial shows you how to set up and deploy a Cloudflare Worker that is accessing a D1 database from scratch.
2525

26-
### Prerequisites
26+
## Quick start
27+
28+
If you want to skip the steps and get started quickly, select **Deploy to Cloudflare** below.
29+
30+
[![Deploy to Cloudflare](https://deploy.workers.cloudflare.com/button)](https://deploy.workers.cloudflare.com/?url=https://github.com/cloudflare/docs-examples/tree/d1-prisma/d1/query-d1-using-prisma)
31+
32+
This creates a repository in your GitHub account and deploys the application to Cloudflare Workers. Use this option if you are familiar with Cloudflare Workers, and wish to skip the step-by-step guidance.
33+
34+
You may wish to manually follow the steps if you are new to Cloudflare Workers.
35+
36+
## Prerequisites
2737

2838
- [`Node.js`](https://nodejs.org/en/) and [`npm`](https://docs.npmjs.com/getting-started) installed on your machine.
2939
- A [Cloudflare account](https://dash.cloudflare.com).
3040

31-
### 1. Create a Cloudflare Worker
41+
## 1. Create a Cloudflare Worker
3242

3343
Open your terminal, and run the following command to create a Cloudflare Worker using Cloudflare's [`hello-world`](https://github.com/cloudflare/workers-sdk/tree/4fdd8987772d914cf50725e9fa8cb91a82a6870d/packages/create-cloudflare/templates/hello-world) template:
3444

@@ -41,7 +51,7 @@ In your terminal, you will be asked a series of questions related your project:
4151
1. Answer `yes` to using TypeScript.
4252
2. Answer `no` to deploying your Worker.
4353

44-
### 2. Initialize Prisma ORM
54+
## 2. Initialize Prisma ORM
4555

4656
:::note
4757

@@ -80,15 +90,15 @@ Since you will use the [driver adapter](https://www.prisma.io/docs/orm/overview/
8090

8191
Open your `schema.prisma` file and adjust the `generator` block to reflect as follows:
8292

83-
```diff
93+
```prisma title="schema.prisma"
8494
generator client {
8595
provider = "prisma-client-js"
8696
output = "../src/generated/prisma"
8797
previewFeatures = ["driverAdapters"]
8898
}
8999
```
90100

91-
### 3. Create your D1 database
101+
## 3. Create your D1 database
92102

93103
In this step, you will set up your D1 database. You can create a D1 database via the [Cloudflare dashboard](https://dash.cloudflare.com), or via `wrangler`. This tutorial will use the `wrangler` CLI.
94104

@@ -141,7 +151,7 @@ Replace `<D1_DATABASE_ID>` with the database ID of your D1 instance. If you were
141151

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

144-
### 4. Create a table in the database
154+
## 4. Create a table in the database
145155

146156
[Prisma Migrate](https://www.prisma.io/docs/orm/prisma-migrate/understanding-prisma-migrate/overview) does not support D1 yet, so you cannot follow the default migration workflows using `prisma migrate dev` or `prisma db push`.
147157

@@ -151,7 +161,7 @@ Prisma Migrate for D1 is currently in Early Access. If you want to try it out, y
151161

152162
:::
153163

154-
D1 uses [migrations](/d1/reference/migrations) for managing schema changes, and the Prisma CLI can help generate the necessary SQL for those updates. In the steps below, you'll use both tools to create and apply a migration to your database.
164+
D1 uses [migrations](/d1/reference/migrations) for managing schema changes, and the Prisma CLI can help generate the necessary SQL for those updates. In the steps below, you will use both tools to create and apply a migration to your database.
155165

156166
First, create a new migration using `wrangler`:
157167

@@ -175,13 +185,16 @@ Next, you need to add the SQL statement that will create a `User` table to that
175185

176186
Open the `schema.prisma` file and add the following `User` model to your schema:
177187

178-
```diff
179-
model User {
180-
id Int @id @default(autoincrement())
181-
email String @unique
182-
name String?
183-
}
184-
```
188+
<GitHubCode
189+
repo="cloudflare/docs-examples"
190+
file="d1/query-d1-using-prisma/prisma/schema.prisma"
191+
commit="c49d24f86dc2eb06a07b1c0b3ede871a1d8e7e92"
192+
lines="15-19"
193+
lang="prisma"
194+
code={{
195+
title: "schema.prisma"
196+
}}
197+
/>
185198

186199
Now, run the following command in your terminal to generate the SQL statement that creates a `User` table equivalent to the `User` model above:
187200

@@ -191,17 +204,16 @@ npx prisma migrate diff --from-empty --to-schema-datamodel ./prisma/schema.prism
191204

192205
This stores a SQL statement to create a new `User` table in your migration file from before, here is what it looks like:
193206

194-
```sql
195-
-- CreateTable
196-
CREATE TABLE "User" (
197-
"id" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
198-
"email" TEXT NOT NULL,
199-
"name" TEXT
200-
);
201-
202-
-- CreateIndex
203-
CREATE UNIQUE INDEX "User_email_key" ON "User"("email");
204-
```
207+
<GitHubCode
208+
repo="cloudflare/docs-examples"
209+
file="d1/query-d1-using-prisma/migrations/0001_create_user_table.sql"
210+
commit="c49d24f86dc2eb06a07b1c0b3ede871a1d8e7e92"
211+
lang="sql"
212+
lines="1-9"
213+
code={{
214+
title: "0001_create_user_table.sql"
215+
}}
216+
/>
205217

206218
`UNIQUE INDEX` on `email` was created because the `User` model in your Prisma schema is using the [`@unique`](https://www.prisma.io/docs/orm/reference/prisma-schema-reference#unique) attribute on its `email` field.
207219

@@ -253,7 +265,7 @@ npx wrangler d1 execute prisma-demo-db --command "INSERT INTO `"User`" (`"email
253265
254266
:::
255267
256-
### 5. Query your database from the Worker
268+
## 5. Query your database from the Worker
257269
258270
To query your database from the Worker using Prisma ORM, you need to:
259271
@@ -263,33 +275,24 @@ To query your database from the Worker using Prisma ORM, you need to:
263275
264276
Open `src/index.ts` and replace the entire content with the following:
265277
266-
```ts
267-
import { PrismaClient } from './generated/prisma/';
268-
import { PrismaD1 } from "@prisma/adapter-d1";
269-
270-
export interface Env {
271-
DB: D1Database;
272-
}
273-
274-
export default {
275-
async fetch(request, env, ctx): Promise<Response> {
276-
const adapter = new PrismaD1(env.DB);
277-
const prisma = new PrismaClient({ adapter });
278-
279-
const users = await prisma.user.findMany();
280-
const result = JSON.stringify(users);
281-
return new Response(result);
282-
},
283-
} satisfies ExportedHandler<Env>;
284-
```
278+
<GitHubCode
279+
repo="cloudflare/docs-examples"
280+
file="d1/query-d1-using-prisma/src/index.ts"
281+
commit="c49d24f86dc2eb06a07b1c0b3ede871a1d8e7e92"
282+
lang="ts"
283+
code={{
284+
title: "index.ts"
285+
}}
286+
useTypeScriptExample={true}
287+
/>
285288
286289
Before running the Worker, generate Prisma Client with the following command:
287290
288291
```sh
289292
npx prisma generate
290293
```
291294
292-
### 6. Run the Worker locally
295+
## 6. Run the Worker locally
293296
294297
Now that you have the database query in place and Prisma Client generated, run the Worker locally:
295298
@@ -303,7 +306,7 @@ Open your browser at [`http://localhost:8787`](http://localhost:8787/) to check
303306
[{ "id": 1, "email": "[email protected]", "name": "Jane Doe (Local)" }]
304307
```
305308
306-
### 7. Deploy the Worker
309+
## 7. Deploy the Worker
307310
308311
To deploy the Worker, run the following command:
309312

0 commit comments

Comments
 (0)