Skip to content

Commit b0285e7

Browse files
committed
add prisma postgres to hyperdrive (draft, needs configurable connection count)
1 parent 3db7a18 commit b0285e7

File tree

5 files changed

+484
-15
lines changed

5 files changed

+484
-15
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: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
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 } 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. Navigate to your database settings and copy the connection string for your Prisma Postgres database.
23+
3. Ensure the connection string includes the necessary credentials and database name.
24+
25+
With the connection string, you can now create a Hyperdrive database configuration.
26+
27+
## 2. Create a database configuration
28+
29+
<Render file="create-hyperdrive-config" product="hyperdrive" />
30+
31+
:::note
32+
33+
When configuring Hyperdrive for Prisma Postgres, set the maximum connections to 15 to stay within Prisma Postgres connection limits. You can configure this in the Cloudflare dashboard under Hyperdrive settings, or contact support if you need to adjust connection pooling settings.
34+
35+
:::
36+
37+
:::note
38+
39+
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 will perform global connection pooling and fast query routing by connecting directly to your database.
40+
41+
:::
42+
43+
<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 next-generation 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+
20+
## 1. Install Prisma ORM
21+
22+
Install Prisma CLI as a dev dependency:
23+
24+
<PackageManagers pkg="prisma" dev />
25+
26+
Install the [`dotenv-cli` package](https://www.npmjs.com/package/dotenv-cli) to load environment variables from `.dev.vars`:
27+
28+
<PackageManagers pkg="dotenv-cli" dev />
29+
30+
Install the `pg` driver for use with Hyperdrive:
31+
32+
<PackageManagers pkg="pg@>8.13.0" />
33+
34+
If using TypeScript, install the types package:
35+
36+
<PackageManagers pkg="@types/pg" dev />
37+
38+
Add the required Node.js compatibility flags and Hyperdrive binding to your `wrangler.toml` file:
39+
40+
<Render file="hyperdrive-node-compatibility-requirement" product="hyperdrive" />
41+
42+
## 2. Configure Prisma ORM
43+
44+
### 2.1. Initialize Prisma
45+
46+
Initialize Prisma in your application:
47+
48+
```sh
49+
npx prisma init
50+
```
51+
52+
This creates a `prisma` folder with a `schema.prisma` file and an `.env` file.
53+
54+
### 2.2. Define a schema
55+
56+
Define your database schema in the `prisma/schema.prisma` file:
57+
58+
```prisma title="prisma/schema.prisma"
59+
generator client {
60+
provider = "prisma-client-js"
61+
}
62+
63+
datasource db {
64+
provider = "postgresql"
65+
url = env("DATABASE_URL")
66+
}
67+
68+
model User {
69+
id Int @id @default(autoincrement())
70+
name String
71+
email String @unique
72+
createdAt DateTime @default(now())
73+
}
74+
```
75+
76+
### 2.3. Set up environment variables
77+
78+
Rename the `.env` file to `.dev.vars` (since Workers doesn't support `.env` files):
79+
80+
```sh
81+
mv .env .dev.vars
82+
```
83+
84+
Add your database connection string to `.dev.vars`:
85+
86+
```env title=".dev.vars"
87+
DATABASE_URL="postgres://user:password@host:port/database"
88+
```
89+
90+
Add helper scripts to your `package.json`:
91+
92+
```json title="package.json"
93+
"scripts": {
94+
"migrate": "dotenv -e .dev.vars -- npx prisma migrate dev",
95+
"generate": "dotenv -e .dev.vars -- npx prisma generate --no-engine",
96+
"studio": "dotenv -e .dev.vars -- npx prisma studio"
97+
}
98+
```
99+
100+
### 2.4. 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 { PrismaClient } from "@prisma/client/edge";
116+
117+
export interface Env {
118+
HYPERDRIVE: Hyperdrive;
119+
}
120+
121+
export default {
122+
async fetch(request, env, ctx): Promise<Response> {
123+
// Create Prisma client using Hyperdrive connection string
124+
const prisma = new PrismaClient({
125+
datasourceUrl: env.HYPERDRIVE.connectionString,
126+
});
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 connect directly to the underlying PostgreSQL database through Hyperdrive's connection pooling. This means you don't need Prisma-specific connection pooling extensions like Prisma Accelerate, since Hyperdrive handles the connection pooling at the network level.
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" />

src/content/docs/workers/databases/third-party-integrations/planetscale.mdx

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,20 +9,16 @@ import { Render, PackageManagers, Tabs, TabItem } from "~/components";
99

1010
:::note
1111

12-
You can connect to PlanetScale using [Hyperdrive](/hyperdrive) (recommended), or using the PlanetScale serverless driver, `@planetscale/database`. Both provide connection pooling and reduce the amount of round trips required to create a secure connection from Workers to your database.
12+
You can connect to PlanetScale using the PlanetScale serverless driver, `@planetscale/database`, or [Hyperdrive](/hyperdrive). Both approaches provide comparable performance with different tradeoffs.
1313

14-
Hyperdrive can provide lower latencies because it performs the database connection setup and connection pooling across Cloudflare's network. Hyperdrive supports native database drivers, libraries, and ORMs, and is included in all [Workers plans](/hyperdrive/platform/pricing/). Learn more about Hyperdrive in [How Hyperdrive Works](/hyperdrive/configuration/how-hyperdrive-works/).
14+
The PlanetScale serverless driver provides connection pooling and is designed specifically for PlanetScale's platform, offering simplicity and built-in optimizations for the PlanetScale ecosystem.
15+
16+
Hyperdrive provides the lowest possible latencies due to its globally distributed nature and allows you to use any standard MySQL driver. Hyperdrive performs database connection setup and connection pooling across Cloudflare's network and supports native database drivers, libraries, and ORMs. Learn more about Hyperdrive in [How Hyperdrive Works](/hyperdrive/configuration/how-hyperdrive-works/).
1517

1618
:::
1719

1820
<Tabs>
19-
<TabItem label="Hyperdrive (recommended)">
20-
21-
To connect to PlanetScale using [Hyperdrive](/hyperdrive), follow these steps:
22-
23-
<Render file="planetscale-partial" product="hyperdrive"/>
24-
</TabItem>
25-
<TabItem label="PlanetScale serverless driver">
21+
<TabItem label="PlanetScale serverless driver">
2622

2723
## Set up an integration with PlanetScale
2824

@@ -103,5 +99,12 @@ To set up an integration with PlanetScale:
10399

104100
To learn more about PlanetScale, refer to [PlanetScale's official documentation](https://docs.planetscale.com/).
105101

106-
</TabItem>
102+
</TabItem>
103+
<TabItem label="Hyperdrive">
104+
105+
To connect to PlanetScale using [Hyperdrive](/hyperdrive), follow these steps:
106+
107+
<Render file="planetscale-partial" product="hyperdrive"/>
108+
109+
</TabItem>
107110
</Tabs>

0 commit comments

Comments
 (0)