-
Notifications
You must be signed in to change notification settings - Fork 10.4k
[D1] Prisma ORM D1 tutorial Deploy to Cloudflare button #22859
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 4 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
5d826fc
Updating to use GitHubCode where possible.
Oxyjun 2a4a054
Adding Deploy to Cloudflare button.
Oxyjun de54ba7
Merge commit '72028b72a7b5ac0465c5959e0e55ce424f267a42' into jun/d1/p…
Oxyjun 4e52d0a
Catching typo
Oxyjun 0e5f8d9
Update src/content/docs/d1/tutorials/d1-and-prisma-orm/index.mdx
Oxyjun File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -11,7 +11,7 @@ languages: | |
| - SQL | ||
| --- | ||
|
|
||
| import { WranglerConfig, FileTree, PackageManagers } from "~/components"; | ||
| import { WranglerConfig, FileTree, PackageManagers, GitHubCode } from "~/components"; | ||
|
|
||
| ## What is Prisma ORM? | ||
|
|
||
|
|
@@ -21,14 +21,24 @@ To learn more about Prisma ORM, refer to the [Prisma documentation](https://www. | |
|
|
||
| ## Query D1 from a Cloudflare Worker using Prisma ORM | ||
|
|
||
| This example shows you how to set up and deploy a Cloudflare Worker that is accessing a D1 database from scratch. | ||
| This tutorial shows you how to set up and deploy a Cloudflare Worker that is accessing a D1 database from scratch. | ||
|
|
||
| ### Prerequisites | ||
| ## Quick start | ||
|
|
||
| If you want to skip the steps and get started quickly, click on the button below. | ||
|
|
||
| [](https://deploy.workers.cloudflare.com/?url=https://github.com/cloudflare/docs-examples/tree/d1-prisma/d1/query-d1-using-prisma) | ||
|
|
||
| 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. | ||
|
|
||
| You may wish to manually follow the steps if you are new to Cloudflare Workers. | ||
|
|
||
| ## Prerequisites | ||
|
|
||
| - [`Node.js`](https://nodejs.org/en/) and [`npm`](https://docs.npmjs.com/getting-started) installed on your machine. | ||
| - A [Cloudflare account](https://dash.cloudflare.com). | ||
|
|
||
| ### 1. Create a Cloudflare Worker | ||
| ## 1. Create a Cloudflare Worker | ||
|
|
||
| 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: | ||
|
|
||
|
|
@@ -41,7 +51,7 @@ In your terminal, you will be asked a series of questions related your project: | |
| 1. Answer `yes` to using TypeScript. | ||
| 2. Answer `no` to deploying your Worker. | ||
|
|
||
| ### 2. Initialize Prisma ORM | ||
| ## 2. Initialize Prisma ORM | ||
|
|
||
| :::note | ||
|
|
||
|
|
@@ -80,15 +90,15 @@ Since you will use the [driver adapter](https://www.prisma.io/docs/orm/overview/ | |
|
|
||
| Open your `schema.prisma` file and adjust the `generator` block to reflect as follows: | ||
|
|
||
| ```diff | ||
| ```prisma title="schema.prisma" | ||
| generator client { | ||
| provider = "prisma-client-js" | ||
| output = "../src/generated/prisma" | ||
| previewFeatures = ["driverAdapters"] | ||
| } | ||
| ``` | ||
|
|
||
| ### 3. Create your D1 database | ||
| ## 3. Create your D1 database | ||
|
|
||
| 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. | ||
|
|
||
|
|
@@ -141,7 +151,7 @@ Replace `<D1_DATABASE_ID>` with the database ID of your D1 instance. If you were | |
|
|
||
| Next, you will create a database table in the database to send queries to D1 using Prisma ORM. | ||
|
|
||
| ### 4. Create a table in the database | ||
| ## 4. Create a table in the database | ||
|
|
||
| [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`. | ||
|
|
||
|
|
@@ -151,7 +161,7 @@ Prisma Migrate for D1 is currently in Early Access. If you want to try it out, y | |
|
|
||
| ::: | ||
|
|
||
| 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. | ||
| 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. | ||
|
|
||
| First, create a new migration using `wrangler`: | ||
|
|
||
|
|
@@ -175,13 +185,16 @@ Next, you need to add the SQL statement that will create a `User` table to that | |
|
|
||
| Open the `schema.prisma` file and add the following `User` model to your schema: | ||
|
|
||
| ```diff | ||
| model User { | ||
| id Int @id @default(autoincrement()) | ||
| email String @unique | ||
| name String? | ||
| } | ||
| ``` | ||
| <GitHubCode | ||
| repo="cloudflare/docs-examples" | ||
| file="d1/query-d1-using-prisma/prisma/schema.prisma" | ||
| commit="c49d24f86dc2eb06a07b1c0b3ede871a1d8e7e92" | ||
| lines="15-19" | ||
| lang="prisma" | ||
| code={{ | ||
| title: "schema.prisma" | ||
| }} | ||
| /> | ||
|
|
||
| Now, run the following command in your terminal to generate the SQL statement that creates a `User` table equivalent to the `User` model above: | ||
|
|
||
|
|
@@ -191,17 +204,16 @@ npx prisma migrate diff --from-empty --to-schema-datamodel ./prisma/schema.prism | |
|
|
||
| This stores a SQL statement to create a new `User` table in your migration file from before, here is what it looks like: | ||
|
|
||
| ```sql | ||
| -- CreateTable | ||
| CREATE TABLE "User" ( | ||
| "id" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, | ||
| "email" TEXT NOT NULL, | ||
| "name" TEXT | ||
| ); | ||
|
|
||
| -- CreateIndex | ||
| CREATE UNIQUE INDEX "User_email_key" ON "User"("email"); | ||
| ``` | ||
| <GitHubCode | ||
| repo="cloudflare/docs-examples" | ||
| file="d1/query-d1-using-prisma/migrations/0001_create_user_table.sql" | ||
| commit="c49d24f86dc2eb06a07b1c0b3ede871a1d8e7e92" | ||
| lang="sql" | ||
| lines="1-9" | ||
| code={{ | ||
| title: "0001_create_user_table.sql" | ||
| }} | ||
| /> | ||
|
|
||
| `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. | ||
|
|
||
|
|
@@ -253,7 +265,7 @@ npx wrangler d1 execute prisma-demo-db --command "INSERT INTO `"User`" (`"email | |
|
|
||
| ::: | ||
|
|
||
| ### 5. Query your database from the Worker | ||
| ## 5. Query your database from the Worker | ||
|
|
||
| To query your database from the Worker using Prisma ORM, you need to: | ||
|
|
||
|
|
@@ -263,33 +275,24 @@ To query your database from the Worker using Prisma ORM, you need to: | |
|
|
||
| Open `src/index.ts` and replace the entire content with the following: | ||
|
|
||
| ```ts | ||
| import { PrismaClient } from './generated/prisma/'; | ||
| import { PrismaD1 } from "@prisma/adapter-d1"; | ||
|
|
||
| export interface Env { | ||
| DB: D1Database; | ||
| } | ||
|
|
||
| export default { | ||
| async fetch(request, env, ctx): Promise<Response> { | ||
| const adapter = new PrismaD1(env.DB); | ||
| const prisma = new PrismaClient({ adapter }); | ||
|
|
||
| const users = await prisma.user.findMany(); | ||
| const result = JSON.stringify(users); | ||
| return new Response(result); | ||
| }, | ||
| } satisfies ExportedHandler<Env>; | ||
| ``` | ||
| <GitHubCode | ||
| repo="cloudflare/docs-examples" | ||
| file="d1/query-d1-using-prisma/src/index.ts" | ||
| commit="c49d24f86dc2eb06a07b1c0b3ede871a1d8e7e92" | ||
| lang="ts" | ||
| code={{ | ||
| title: "index.ts" | ||
| }} | ||
| useTypeScriptExample={true} | ||
| /> | ||
|
|
||
| Before running the Worker, generate Prisma Client with the following command: | ||
|
|
||
| ```sh | ||
| npx prisma generate | ||
| ``` | ||
|
|
||
| ### 6. Run the Worker locally | ||
| ## 6. Run the Worker locally | ||
|
|
||
| Now that you have the database query in place and Prisma Client generated, run the Worker locally: | ||
|
|
||
|
|
@@ -303,7 +306,7 @@ Open your browser at [`http://localhost:8787`](http://localhost:8787/) to check | |
| [{ "id": 1, "email": "[email protected]", "name": "Jane Doe (Local)" }] | ||
| ``` | ||
|
|
||
| ### 7. Deploy the Worker | ||
| ## 7. Deploy the Worker | ||
|
|
||
| To deploy the Worker, run the following command: | ||
|
|
||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.