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