Skip to content

Commit 0fedbda

Browse files
style guide updates
1 parent fbb8c57 commit 0fedbda

File tree

1 file changed

+58
-29
lines changed

1 file changed

+58
-29
lines changed

src/content/docs/hyperdrive/tutorials/prisma-postgres.mdx

Lines changed: 58 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
updated: 2025-08-05
33
difficulty: Beginner
44
pcx_content_type: tutorial
5-
title: Prisma Postgres with Cloudflare Hyperdrive
5+
title: Build a Prisma Postgres application with Cloudflare Hyperdrive
66
products:
77
- Hyperdrive
88
tags:
@@ -12,13 +12,13 @@ languages:
1212
- Prisma
1313
---
1414

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.
1616

1717
## Step 1: Create a new Cloudflare Workers project
1818

1919
Create a new directory and initialize a Cloudflare Workers project:
2020

21-
```bash
21+
```bash title="Create a new Workers project"
2222
npm create cloudflare@latest hyperdrive-prisma-postgres
2323
cd hyperdrive-prisma-postgres
2424
```
@@ -31,30 +31,30 @@ Choose the following options:
3131
- Do you want to use git for version control? → `Yes`
3232
- Do you want to deploy your application to Cloudflare? → `No`
3333

34-
## Step 2: Install dependencies
34+
## Step 2: Install Prisma and Hyperdrive dependencies
3535

3636
Install the required dependencies for Prisma and Hyperdrive:
3737

38-
```bash
38+
```bash title="Install dependencies"
3939
npm install @prisma/client @prisma/adapter-pg
4040
npm install -D prisma
4141
```
4242

43-
## Step 3: Set up Prisma
43+
## Step 3: Configure Prisma for your project
4444

45-
### 1. Initialize Prisma in your project
45+
### Initialize Prisma in your project
4646

4747
To initialize Prisma in your project, run the following command:
4848

49-
```bash
49+
```bash title="Initialize Prisma"
5050
npx prisma init
5151
```
5252

53-
### 2. Update your `prisma/schema.prisma` file
53+
### Update your Prisma schema
5454

5555
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:
5656

57-
```prisma
57+
```prisma title="prisma/schema.prisma"
5858
generator client {
5959
provider = "prisma-client-js"
6060
output = "../src/generated/prisma"
@@ -73,47 +73,51 @@ model Note {
7373
}
7474
```
7575

76-
### 3. Set up environment variables
76+
### Set up environment variables
7777

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+
:::
7983

8084
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.
8185

82-
```env
86+
```txt title=".env"
8387
DATABASE_URL="..."
8488
```
8589

86-
### 4. Generate Prisma client and run migrations
90+
### Generate Prisma client and run migrations
8791

8892
Generate the Prisma client with the custom output path:
8993

90-
```bash
94+
```bash title="Generate Prisma client"
9195
npx prisma generate
9296
```
9397

9498
Create and run your database migrations:
9599

96-
```bash
100+
```bash title="Create and run migrations"
97101
npx prisma migrate dev --name init
98102
```
99103

100104
This will create the `Note` table in your database.
101105

102-
## Step 4: Create a Hyperdrive database
106+
## Step 4: Create a Hyperdrive database connection
103107

104108
Create a Hyperdrive database by running the wrangler hyperdrive create command and passing in your database connection string:
105109

106-
```bash
110+
```bash title="Create Hyperdrive database"
107111
npx wrangler hyperdrive create hyperdrive-prisma-postgres --connection-string="..."
108112
```
109113

110114
## Step 5: Configure your Worker
111115

112-
### 1. Update your `wrangler.jsonc` file
116+
### Update your Wrangler configuration
113117

114118
Update your `wrangler.jsonc` file to include the Hyperdrive binding, and set the `compatibility_flags` to `nodejs_compat`:
115119

116-
```jsonc
120+
```jsonc title="wrangler.jsonc"
117121
{
118122
"$schema": "node_modules/wrangler/config-schema.json",
119123
"name": "hyper-test",
@@ -134,15 +138,15 @@ Update your `wrangler.jsonc` file to include the Hyperdrive binding, and set the
134138

135139
Replace `<YOUR-HYPERDRIVE-DATABASE-ID>` with the actual ID from your Hyperdrive database returned by the `wrangler hyperdrive create` command in the previous step.
136140

137-
### 2. Create your Worker code
141+
### Create your Worker code
138142

139143
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.
140144

141-
### 3. Set up imports and environment interface
145+
#### Set up imports and environment interface
142146

143147
To start, we'll set up the imports and environment interface.
144148

145-
```typescript
149+
```typescript title="src/index.ts - Imports and environment"
146150
import { PrismaPg } from "@prisma/adapter-pg";
147151
import { PrismaClient } from "./generated/prisma";
148152

@@ -151,23 +155,23 @@ export interface Env {
151155
}
152156
```
153157

154-
### 4. Create a database connection helper
158+
#### Create a database connection helper
155159

156160
Next, we'll create a database connection helper using the `PrismaPg` adapter.
157161

158-
```typescript
162+
```typescript title="src/index.ts - Database connection helper"
159163
export const getDb = (env: Env) => {
160164
const connectionString = env.HYPERDRIVE.connectionString;
161165
const adapter = new PrismaPg({ connectionString, maxUses: 1 });
162166
return new PrismaClient({ adapter });
163167
};
164168
```
165169

166-
### 5. Implement the Worker handler
170+
#### Implement the Worker handler
167171

168172
Finally, we'll implement the Worker handler which will create a new note every time it's called and return all existing notes.
169173

170-
```typescript
174+
```typescript title="src/index.ts - Worker handler"
171175
export default {
172176
async fetch(request, env, ctx): Promise<Response> {
173177
const prisma = getDb(env);
@@ -186,12 +190,37 @@ export default {
186190
} satisfies ExportedHandler<Env>;
187191
```
188192

189-
## Step 6: Deploy to Cloudflare
193+
## Step 6: Deploy your Worker to Cloudflare
190194

191195
Deploy your Worker to Cloudflare:
192196

193-
```bash
197+
```bash title="Deploy to Cloudflare"
194198
npm run deploy
195199
```
196200

197201
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
210+
211+
To extend your application, consider:
212+
213+
- Adding more CRUD operations (update, delete)
214+
- Implementing user authentication
215+
- Adding input validation
216+
- Creating a frontend interface
217+
- Setting up proper error handling
218+
219+
## Related resources
220+
221+
- [Hyperdrive documentation](/hyperdrive/)
222+
- [Prisma documentation](https://www.prisma.io/docs)
223+
- [Cloudflare Workers documentation](/workers/)
224+
- [More Hyperdrive tutorials](/hyperdrive/tutorials/)
225+
226+
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

Comments
 (0)