@@ -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,7 +21,9 @@ 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.
25+
26+
2527
2628### Prerequisites
2729
@@ -80,7 +82,7 @@ Since you will use the [driver adapter](https://www.prisma.io/docs/orm/overview/
8082
8183Open your ` schema.prisma ` file and adjust the ` generator ` block to reflect as follows:
8284
83- ``` diff
85+ ``` prisma title="schema.prisma"
8486generator client {
8587 provider = "prisma-client-js"
8688+ previewFeatures = ["driverAdapters"]
@@ -170,13 +172,16 @@ Next, you need to add the SQL statement that will create a `User` table to that
170172
171173Open the ` schema.prisma ` file and add the following ` User ` model to your schema:
172174
173- ``` diff
174- model User {
175- id Int @id @default(autoincrement())
176- email String @unique
177- name String?
178- }
179- ```
175+ <GitHubCode
176+ repo = " cloudflare/docs-examples"
177+ file = " d1/query-d1-using-prisma/prisma/schema.prisma"
178+ commit = " c49d24f86dc2eb06a07b1c0b3ede871a1d8e7e92"
179+ lines = " 15-19"
180+ lang = " prisma"
181+ code = { {
182+ title: " schema.prisma"
183+ }}
184+ />
180185
181186Now, run the following command in your terminal to generate the SQL statement that creates a ` User ` table equivalent to the ` User ` model above:
182187
@@ -186,17 +191,16 @@ npx prisma migrate diff --from-empty --to-schema-datamodel ./prisma/schema.prism
186191
187192This stores a SQL statement to create a new ` User ` table in your migration file from before, here is what it looks like:
188193
189- ``` sql
190- -- CreateTable
191- CREATE TABLE "User " (
192- " id" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
193- " email" TEXT NOT NULL ,
194- " name" TEXT
195- );
196-
197- -- CreateIndex
198- CREATE UNIQUE INDEX "User_email_key " ON " User" (" email" );
199- ```
194+ <GitHubCode
195+ repo = " cloudflare/docs-examples"
196+ file = " d1/query-d1-using-prisma/migrations/0001_create_user_table.sql"
197+ commit = " c49d24f86dc2eb06a07b1c0b3ede871a1d8e7e92"
198+ lang = " sql"
199+ lines = " 1-9"
200+ code = { {
201+ title: " 0001_create_user_table.sql"
202+ }}
203+ />
200204
201205` 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.
202206
@@ -258,25 +262,16 @@ To query your database from the Worker using Prisma ORM, you need to:
258262
259263Open ` src/index.ts` and replace the entire content with the following:
260264
261- ` ` ` ts
262- import { PrismaClient } from " @prisma/client" ;
263- import { PrismaD1 } from " @prisma/adapter-d1" ;
264-
265- export interface Env {
266- DB: D1Database;
267- }
268-
269- export default {
270- async fetch(request, env, ctx): Promise< Response> {
271- const adapter = new PrismaD1(env.DB);
272- const prisma = new PrismaClient({ adapter });
273-
274- const users = await prisma.user.findMany ();
275- const result = JSON.stringify(users);
276- return new Response(result);
277- },
278- } satisfies ExportedHandler< Env> ;
279- ` ` `
265+ <GitHubCode
266+ repo=" cloudflare/docs-examples"
267+ file=" d1/query-d1-using-prisma/src/index.ts"
268+ commit=" c49d24f86dc2eb06a07b1c0b3ede871a1d8e7e92"
269+ lang=" ts"
270+ code={{
271+ title: " index.ts"
272+ }}
273+ useTypeScriptExample={true}
274+ />
280275
281276Before running the Worker, generate Prisma Client with the following command:
282277
0 commit comments