|
| 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" /> |
0 commit comments