Skip to content

Commit 72e8898

Browse files
DC-5244 Cloudflare Worker example (#8388)
* example project created * test added * alternate dbs removed * exmaple project updated
1 parent add894e commit 72e8898

File tree

13 files changed

+9660
-0
lines changed

13 files changed

+9660
-0
lines changed
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
#!/bin/bash
2+
3+
set -eu
4+
5+
echo "🔍 Starting test setup..."
6+
7+
echo "📂 Current working directory before REPO_ROOT: $(pwd)"
8+
echo "📁 Listing contents:"
9+
ls -la
10+
11+
REPO_ROOT="$(git rev-parse --show-toplevel)"
12+
echo "📌 Detected repo root: $REPO_ROOT"
13+
14+
cd "$REPO_ROOT/orm/cloudflare-workers"
15+
echo "📂 Changed directory to: $(pwd)"
16+
17+
echo "📦 Installing test deps..."
18+
npm install
19+
20+
# Go to Node script dir and install its deps
21+
NODE_SCRIPT_DIR="../../.github/get-ppg-dev"
22+
pushd "$NODE_SCRIPT_DIR" > /dev/null
23+
npm install
24+
25+
# Start Prisma Dev server
26+
LOG_FILE="./ppg-dev-url.log"
27+
rm -f "$LOG_FILE"
28+
touch "$LOG_FILE"
29+
30+
echo "🚀 Starting Prisma Dev in background..."
31+
node index.js >"$LOG_FILE" &
32+
NODE_PID=$!
33+
34+
# Wait for DATABASE_URL
35+
echo "🔎 Waiting for Prisma Dev to emit DATABASE_URL..."
36+
MAX_WAIT=60
37+
WAITED=0
38+
until grep -q '^prisma+postgres://' "$LOG_FILE"; do
39+
sleep 1
40+
WAITED=$((WAITED + 1))
41+
if [ "$WAITED" -ge "$MAX_WAIT" ]; then
42+
echo "❌ Timeout waiting for DATABASE_URL"
43+
cat "$LOG_FILE"
44+
kill "$NODE_PID" || true
45+
exit 1
46+
fi
47+
done
48+
49+
DB_URL=$(grep '^prisma+postgres://' "$LOG_FILE" | tail -1)
50+
export DATABASE_URL="$DB_URL"
51+
echo "✅ DATABASE_URL: $DATABASE_URL"
52+
53+
popd > /dev/null
54+
55+
npm install
56+
npx prisma migrate dev --name init --schema prisma/schema.prisma
57+
npx prisma db seed
58+
npm run dev &
59+
pid=$!
60+
61+
sleep 15
62+
63+
# check frontend
64+
curl --fail 'http://localhost:3000/'
65+
66+
kill "$pid"
67+
echo "🛑 App stopped (PID $pid)"
68+
kill "$NODE_PID"
69+
wait "$NODE_PID" || true

orm/cloudflare-workers/README.md

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
# Cloudflare Workers Example
2+
3+
This example shows how to implement a simple API using [Cloudflare Workers](https://workers.cloudflare.com) and [Prisma ORM](https://www.prisma.io/docs). The example creates a serverless worker that interacts with a PostgreSQL database to create and count users.
4+
5+
## Getting started
6+
7+
### 1. Download the example and navigate to the project directory
8+
9+
Download this example:
10+
11+
```
12+
npx try-prisma@latest --template orm/cloudflare-workers
13+
```
14+
15+
Then navigate to the project directory
16+
17+
```
18+
cd cloudflare-workers
19+
```
20+
21+
<details><summary><strong>Alternative:</strong> Clone the entire repo</summary>
22+
23+
Clone this repository:
24+
25+
```
26+
git clone [email protected]:prisma/prisma-examples.git --depth=1
27+
```
28+
29+
Install npm dependencies:
30+
31+
```
32+
cd prisma-examples/orm/cloudflare-workers
33+
npm install
34+
```
35+
36+
</details>
37+
38+
### 2. Create a Prisma Postgres instance
39+
40+
This example uses a [Prisma Postgres](https://prisma.io/postgres) database by default. To get started with the project, you will need to setup a Prisma Postgres connection string:
41+
42+
1. Set up a new Prisma Postgres instance in the [Prisma Data Platform Console](https://console.prisma.io) and copy the database connection URL.
43+
44+
2. Add your database url to the `.env`
45+
46+
That's it, your project is now configured to use Prisma Postgres!
47+
48+
49+
### 3. Generate Prisma Client
50+
51+
Run the following command to generate the Prisma Client. This is what you will be using to interact with your database.
52+
53+
```
54+
npm run generate
55+
```
56+
57+
### 4. Start the Cloudflare Worker
58+
59+
```
60+
npm run dev
61+
```
62+
63+
The server is now running at http://localhost:8788
64+
65+
### 5. Test the API
66+
67+
The API supports full CRUD operations. Here are some example `curl` commands:
68+
69+
**Create a user:**
70+
```bash
71+
curl -X POST http://localhost:8788/users \
72+
-H "Content-Type: application/json" \
73+
-d '{"email":"[email protected]","name":"John Doe"}'
74+
```
75+
76+
**Get all users:**
77+
```bash
78+
curl http://localhost:8788/users
79+
```
80+
81+
**Get a specific user:**
82+
```bash
83+
curl http://localhost:8788/users/1
84+
```
85+
86+
**Update a user:**
87+
```bash
88+
curl -X PUT http://localhost:8788/users/1 \
89+
-H "Content-Type: application/json" \
90+
-d '{"name":"John Updated"}'
91+
```
92+
93+
**Delete a user:**
94+
```bash
95+
curl -X DELETE http://localhost:8788/users/1
96+
```
97+
98+
## Next steps
99+
100+
- Check out the [Prisma docs](https://www.prisma.io/docs)
101+
- Share your feedback on the [Prisma Discord](https://pris.ly/discord/)
102+
- Create issues and ask questions on [GitHub](https://github.com/prisma/prisma/)
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
{
2+
"name": "prisma-cloudflare-worker-3",
3+
"version": "0.0.0",
4+
"private": true,
5+
"scripts": {
6+
"deploy": "wrangler deploy",
7+
"dev": "wrangler dev",
8+
"start": "wrangler dev",
9+
"test": "vitest",
10+
"cf-typegen": "wrangler types",
11+
"migrate": "prisma migrate dev",
12+
"generate": "prisma generate",
13+
"studio": "prisma studio"
14+
},
15+
"devDependencies": {
16+
"@cloudflare/vitest-pool-workers": "^0.8.19",
17+
"@types/pg": "^8.15.6",
18+
"dotenv-cli": "^11.0.0",
19+
"prisma": "^7.0.0",
20+
"typescript": "^5.5.2",
21+
"vitest": "~3.2.0",
22+
"wrangler": "^4.49.1"
23+
},
24+
"dependencies": {
25+
"@prisma/adapter-pg": "^7.0.0",
26+
"@prisma/client": "^7.0.0",
27+
"dotenv": "^17.2.3",
28+
"pg": "^8.16.3"
29+
}
30+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// This file was generated by Prisma and assumes you have installed the following:
2+
// npm install --save-dev prisma dotenv
3+
import "dotenv/config";
4+
import { defineConfig, env } from "prisma/config";
5+
6+
export default defineConfig({
7+
schema: "prisma/schema.prisma",
8+
migrations: {
9+
path: "prisma/migrations",
10+
},
11+
datasource: {
12+
url: env("DATABASE_URL"),
13+
},
14+
});
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// This is your Prisma schema file,
2+
// learn more about it in the docs: https://pris.ly/d/prisma-schema
3+
4+
// Looking for ways to speed up your queries, or scale easily with your serverless or edge functions?
5+
// Try Prisma Accelerate: https://pris.ly/cli/accelerate-init
6+
7+
generator client {
8+
provider = "prisma-client"
9+
runtime = "cloudflare"
10+
output = "../src/generated/prisma"
11+
}
12+
13+
datasource db {
14+
provider = "postgresql"
15+
}
16+
17+
model User {
18+
id Int @id @default(autoincrement())
19+
email String
20+
name String
21+
}

0 commit comments

Comments
 (0)