You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: src/content/docs/hyperdrive/tutorials/prisma-postgres.mdx
+58-29Lines changed: 58 additions & 29 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -2,7 +2,7 @@
2
2
updated: 2025-08-05
3
3
difficulty: Beginner
4
4
pcx_content_type: tutorial
5
-
title: Prisma Postgres with Cloudflare Hyperdrive
5
+
title: Build a Prisma Postgres application with Cloudflare Hyperdrive
6
6
products:
7
7
- Hyperdrive
8
8
tags:
@@ -12,13 +12,13 @@ languages:
12
12
- Prisma
13
13
---
14
14
15
-
This tutorial shows you how to set up and use Prisma ORM with Cloudflare Hyperdrive to connect to a Prisma Postgres database from your Cloudflare Workers.
15
+
This tutorial shows you how to set up and use Prisma ORM with Cloudflare Hyperdrive to connect to a Prisma Postgres database from your Cloudflare Workers. By the end of this tutorial, you'll have a fully functional Worker that can interact with a Prisma Postgres database using Prisma.
16
16
17
17
## Step 1: Create a new Cloudflare Workers project
18
18
19
19
Create a new directory and initialize a Cloudflare Workers project:
- Do you want to use git for version control? → `Yes`
32
32
- Do you want to deploy your application to Cloudflare? → `No`
33
33
34
-
## Step 2: Install dependencies
34
+
## Step 2: Install Prisma and Hyperdrive dependencies
35
35
36
36
Install the required dependencies for Prisma and Hyperdrive:
37
37
38
-
```bash
38
+
```bash title="Install dependencies"
39
39
npm install @prisma/client @prisma/adapter-pg
40
40
npm install -D prisma
41
41
```
42
42
43
-
## Step 3: Set up Prisma
43
+
## Step 3: Configure Prisma for your project
44
44
45
-
### 1. Initialize Prisma in your project
45
+
### Initialize Prisma in your project
46
46
47
47
To initialize Prisma in your project, run the following command:
48
48
49
-
```bash
49
+
```bash title="Initialize Prisma"
50
50
npx prisma init
51
51
```
52
52
53
-
### 2. Update your `prisma/schema.prisma` file
53
+
### Update your Prisma schema
54
54
55
55
Update your `prisma/schema.prisma` file to have the `driverAdapters` preview feature enabled so that we can use the `@prisma/adapter-pg` package. Then, add a `Note` model:
56
56
57
-
```prisma
57
+
```prisma title="prisma/schema.prisma"
58
58
generator client {
59
59
provider = "prisma-client-js"
60
60
output = "../src/generated/prisma"
@@ -73,47 +73,51 @@ model Note {
73
73
}
74
74
```
75
75
76
-
### 3. Set up environment variables
76
+
### Set up environment variables
77
77
78
-
To get a Prisma Postgres connection string, you can use `npx create-db@latest` to create a new database. This database **must** be claimed within 24 hours or it will be deleted.
78
+
To get a Prisma Postgres connection string, you can use `npx create-db@latest` to create a new database.
79
+
80
+
:::caution[Database expiration]
81
+
This database **must** be claimed within 24 hours or it will be deleted.
82
+
:::
79
83
80
84
Once you have a database, copy the database labeled **_Use this database for everything else_**. Then, open the `.env` file and set the `DATABASE_URL` variable to your database connection string.
81
85
82
-
```env
86
+
```txt title=".env"
83
87
DATABASE_URL="..."
84
88
```
85
89
86
-
### 4. Generate Prisma client and run migrations
90
+
### Generate Prisma client and run migrations
87
91
88
92
Generate the Prisma client with the custom output path:
89
93
90
-
```bash
94
+
```bash title="Generate Prisma client"
91
95
npx prisma generate
92
96
```
93
97
94
98
Create and run your database migrations:
95
99
96
-
```bash
100
+
```bash title="Create and run migrations"
97
101
npx prisma migrate dev --name init
98
102
```
99
103
100
104
This will create the `Note` table in your database.
101
105
102
-
## Step 4: Create a Hyperdrive database
106
+
## Step 4: Create a Hyperdrive database connection
103
107
104
108
Create a Hyperdrive database by running the wrangler hyperdrive create command and passing in your database connection string:
@@ -134,15 +138,15 @@ Update your `wrangler.jsonc` file to include the Hyperdrive binding, and set the
134
138
135
139
Replace `<YOUR-HYPERDRIVE-DATABASE-ID>` with the actual ID from your Hyperdrive database returned by the `wrangler hyperdrive create` command in the previous step.
136
140
137
-
### 2. Create your Worker code
141
+
### Create your Worker code
138
142
139
143
Create your main Worker file at `src/index.ts`. This Worker will create a new note every time it's called and return all existing notes.
140
144
141
-
###3. Set up imports and environment interface
145
+
####Set up imports and environment interface
142
146
143
147
To start, we'll set up the imports and environment interface.
144
148
145
-
```typescript
149
+
```typescript title="src/index.ts - Imports and environment"
146
150
import { PrismaPg } from"@prisma/adapter-pg";
147
151
import { PrismaClient } from"./generated/prisma";
148
152
@@ -151,23 +155,23 @@ export interface Env {
151
155
}
152
156
```
153
157
154
-
###4. Create a database connection helper
158
+
####Create a database connection helper
155
159
156
160
Next, we'll create a database connection helper using the `PrismaPg` adapter.
The console will display a URL for your worker. Visit that URL to see a JSON response containing your note. On every refresh, a new note will be created.
202
+
203
+
## Next steps
204
+
205
+
You have successfully created a Cloudflare Worker that uses Prisma ORM with Hyperdrive to connect to a Prisma Postgres database. Your Worker can now:
206
+
207
+
- Create new notes in the database
208
+
- Retrieve all existing notes
209
+
- Handle database connections efficiently through Hyperdrive
If you have any questions, need assistance, or would like to share your project, join the Cloudflare Developer community on [Discord](https://discord.cloudflare.com) to connect with other developers and the Cloudflare team.
0 commit comments