Skip to content

Commit fbe174d

Browse files
thomasgauvinOxyjun
authored andcommitted
[Hyperdrive] Add Prisma Postgres and Prisma ORM to docs (#24492)
* thomasgauvin: prisma postgres + prisma orm for hyperdrive * PCX review * Update src/content/docs/hyperdrive/examples/connect-to-postgres/postgres-database-providers/prisma-postgres.mdx * Update src/content/docs/hyperdrive/examples/connect-to-postgres/postgres-database-providers/prisma-postgres.mdx * Update src/content/docs/hyperdrive/examples/connect-to-postgres/postgres-database-providers/prisma-postgres.mdx * Update src/content/docs/hyperdrive/examples/connect-to-postgres/postgres-database-providers/prisma-postgres.mdx --------- Co-authored-by: Jun Lee <[email protected]>
1 parent f6c5642 commit fbe174d

File tree

3 files changed

+243
-1
lines changed

3 files changed

+243
-1
lines changed

src/content/docs/hyperdrive/examples/connect-to-postgres/postgres-database-providers/google-cloud-sql.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ summary: Connect Hyperdrive to a Google Cloud SQL for Postgres database instance
44
pcx_content_type: example
55
title: Google Cloud SQL
66
sidebar:
7-
order: 12
7+
order: 13
88
description: Connect Hyperdrive to a Google Cloud SQL for Postgres database instance.
99
---
1010

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
---
2+
type: example
3+
summary: Connect Hyperdrive to a Prisma Postgres database.
4+
pcx_content_type: example
5+
title: Prisma Postgres
6+
sidebar:
7+
order: 12
8+
description: Connect Hyperdrive to a Prisma Postgres database.
9+
---
10+
11+
import { Render, TabItem, Tabs, Steps } from "~/components";
12+
13+
This example shows you how to connect Hyperdrive to a [Prisma Postgres](https://www.prisma.io/postgres) database.
14+
15+
## 1. Allow Hyperdrive access
16+
17+
You can connect Hyperdrive to any existing Prisma Postgres database by using your existing database connection string.
18+
19+
### Prisma Data Platform
20+
21+
1. Go to the [**Prisma Data Platform Console**](https://console.prisma.io/) and select the project (database) you wish to connect to.
22+
2. Select **Connect to your database** > **Any client**.
23+
3. Select **Generate database credentials**. Copy the connection string for your Prisma Postgres database.
24+
4. Edit the connection string to make it compatible with Hyperdrive.
25+
- Add the database name after the port. You may remove any query parameters, such as `?sslmode=require`.
26+
- The final string will look like:
27+
28+
```txt ins="database_name"
29+
postgres://USERNAME:PASSWORD@HOSTNAME_OR_IP_ADDRESS:PORT/database_name
30+
```
31+
32+
With this connection string, you can now create a Hyperdrive database configuration.
33+
34+
## 2. Create a database configuration
35+
36+
<Render file="create-hyperdrive-config" product="hyperdrive" />
37+
38+
## 4. Configure Hyperdrive maximum connections
39+
40+
Prisma Postgres has limits on the number of direct connections that can be made to the database using Hyperdrive. Refer to [Prisma Postgres limits](https://www.prisma.io/docs/postgres/database/direct-connections?utm_source=website&utm_medium=postgres-page#connection-limit).
41+
42+
:::note
43+
There are two limits to consider here.
44+
45+
- Origin database's connection limit, set by the origin database provider. This is the maximum number of direct database connections that can be made to the origin database.
46+
- Hyperdrive's origin connection limit, set by Hyperdrive. This is the maximum number of database connections that Hyperdrive can make to your origin database (in this case, Prisma Postgres).
47+
48+
Hyperdrive's origin connection limit should be lower than the Prisma Postgres connection limit, since Hyperdrive's origin connection limit is a soft limit, and Hyperdrive may create more connections if there are network disruptions that prevent existing connections from being used.
49+
:::
50+
51+
<Tabs>
52+
53+
<TabItem label={"Dashboard"}>
54+
<Steps>
55+
1. From the [Cloudflare Hyperdrive dashboard](https://dash.cloudflare.com/?to=/:account/workers/hyperdrive), select your newly created Hyperdrive configuration.
56+
2. Go to **Settings**.
57+
3. In **Origin connection limit**, select **Edit Settings**, and set your maximum connections to a number that is lower than your Prisma connection limit.
58+
</Steps>
59+
</TabItem>
60+
<TabItem label={"Wrangler CLI"}>
61+
<Steps>
62+
1. Edit your existing Hyperdrive configuration with the `--origin-connection-limit` parameter:
63+
```bash
64+
npx wrangler hyperdrive update <HYPERDRIVE_ID> --origin-connection-limit=10
65+
```
66+
67+
Replace `<HYPERDRIVE_ID>` with your Hyperdrive configuration ID and set the connection limit to a number that is less than your Prisma connection limit.
68+
2. Verify the configuration change:
69+
```bash
70+
npx wrangler hyperdrive get <HYPERDRIVE_ID>
71+
```
72+
</Steps>
73+
</TabItem>
74+
</Tabs>
75+
76+
:::note
77+
78+
When connecting to a Prisma Postgres database with Hyperdrive, you should use a driver like [node-postgres (pg)](/hyperdrive/examples/connect-to-postgres/postgres-drivers-and-libraries/node-postgres/) or [Postgres.js](/hyperdrive/examples/connect-to-postgres/postgres-drivers-and-libraries/postgres-js/) to connect directly to the underlying PostgreSQL database, instead of the Prisma Accelerate extension. Hyperdrive is optimized for database access for Workers, and connects directly to your database to perform global connection pooling and fast query routing.
79+
80+
:::
81+
82+
<Render file="create-hyperdrive-config-next-steps" product="hyperdrive" />
Lines changed: 160 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
1+
---
2+
pcx_content_type: example
3+
title: Prisma ORM
4+
sidebar:
5+
order: 4
6+
meta:
7+
title: Using Prisma ORM with Hyperdrive for PostgreSQL
8+
---
9+
10+
import { Render, Steps, PackageManagers } from "~/components";
11+
12+
[Prisma ORM](https://www.prisma.io/docs) is a Node.js and TypeScript ORM with a focus on type safety and developer experience. This example demonstrates how to use Prisma ORM with PostgreSQL via Cloudflare Hyperdrive in a Workers application.
13+
14+
## Prerequisites
15+
16+
- A Cloudflare account with Workers access
17+
- A PostgreSQL database (such as [Prisma Postgres](https://www.prisma.io/postgres))
18+
- A [Hyperdrive configuration to your PostgreSQL database](/hyperdrive/get-started/#3-connect-hyperdrive-to-a-database)
19+
- An existing [Worker project](/workers/get-started/guide/)
20+
21+
## 1. Install Prisma ORM
22+
23+
Install Prisma CLI as a dev dependency:
24+
25+
<PackageManagers pkg="prisma" dev />
26+
27+
Install the `pg` driver and Prisma driver adapter for use with Hyperdrive:
28+
29+
<PackageManagers pkg="pg@>8.13.0 @prisma/adapter-pg" />
30+
31+
If using TypeScript, install the types package:
32+
33+
<PackageManagers pkg="@types/pg" dev />
34+
35+
Add the required Node.js compatibility flags and Hyperdrive binding to your `wrangler.toml` file:
36+
37+
<Render file="hyperdrive-node-compatibility-requirement" product="hyperdrive" />
38+
39+
## 2. Configure Prisma ORM
40+
41+
### 2.1. Initialize Prisma
42+
43+
Initialize Prisma in your application:
44+
45+
```sh
46+
npx prisma init
47+
```
48+
49+
This creates a `prisma` folder with a `schema.prisma` file and an `.env` file.
50+
51+
### 2.2. Define a schema
52+
53+
Define your database schema in the `prisma/schema.prisma` file:
54+
55+
```prisma title="prisma/schema.prisma"
56+
generator client {
57+
provider = "prisma-client-js"
58+
previewFeatures = ["driverAdapters"]
59+
}
60+
61+
datasource db {
62+
provider = "postgresql"
63+
url = env("DATABASE_URL")
64+
}
65+
66+
model User {
67+
id Int @id @default(autoincrement())
68+
name String
69+
email String @unique
70+
createdAt DateTime @default(now())
71+
}
72+
```
73+
74+
### 2.3. Set up environment variables
75+
76+
Add your database connection string to the `.env` file created by Prisma:
77+
78+
```env title=".env"
79+
DATABASE_URL="postgres://user:password@host:port/database"
80+
```
81+
82+
Add helper scripts to your `package.json`:
83+
84+
```json title="package.json"
85+
"scripts": {
86+
"migrate": "npx prisma migrate dev",
87+
"generate": "npx prisma generate --no-engine",
88+
"studio": "npx prisma studio"
89+
}
90+
```
91+
92+
### 2.4. Generate Prisma Client
93+
94+
Generate the Prisma client with driver adapter support:
95+
96+
```sh
97+
npm run generate
98+
```
99+
100+
### 2.5. Run migrations
101+
102+
Generate and apply the database schema:
103+
104+
```sh
105+
npm run migrate
106+
```
107+
108+
When prompted, provide a name for the migration (for example, `init`).
109+
110+
## 3. Connect Prisma ORM to Hyperdrive
111+
112+
Use your Hyperdrive configuration when using Prisma ORM. Update your `src/index.ts` file:
113+
114+
```ts title="src/index.ts"
115+
import { PrismaPg } from "@prisma/adapter-pg";
116+
import { PrismaClient } from "@prisma/client";
117+
118+
export interface Env {
119+
HYPERDRIVE: Hyperdrive;
120+
}
121+
122+
export default {
123+
async fetch(request, env, ctx): Promise<Response> {
124+
// Create Prisma client using driver adapter with Hyperdrive connection string
125+
const adapter = new PrismaPg({ connectionString: env.HYPERDRIVE.connectionString });
126+
const prisma = new PrismaClient({ adapter });
127+
128+
// Sample query to create and fetch users
129+
const user = await prisma.user.create({
130+
data: {
131+
name: "John Doe",
132+
email: `john.doe.${Date.now()}@example.com`,
133+
},
134+
});
135+
136+
const allUsers = await prisma.user.findMany();
137+
138+
return Response.json({
139+
newUser: user,
140+
allUsers: allUsers,
141+
});
142+
},
143+
} satisfies ExportedHandler<Env>;
144+
```
145+
146+
:::note
147+
148+
When using Prisma ORM with Hyperdrive, you must use driver adapters to properly utilize the Hyperdrive connection string. The `@prisma/adapter-pg` driver adapter allows Prisma ORM to work with the `pg` driver and Hyperdrive's connection pooling. This approach provides connection pooling at the network level through Hyperdrive, so you don't need Prisma-specific connection pooling extensions like Prisma Accelerate.
149+
150+
:::
151+
152+
## 4. Deploy your Worker
153+
154+
Deploy your Worker:
155+
156+
```bash
157+
npx wrangler deploy
158+
```
159+
160+
<Render file="create-hyperdrive-config-next-steps" product="hyperdrive" />

0 commit comments

Comments
 (0)