Prisma ORM
Prisma ORM ↗ 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.
- A Cloudflare account with Workers access
- A PostgreSQL database (such as Prisma Postgres ↗)
- A Hyperdrive configuration to your PostgreSQL database
- An existing Worker project
Install Prisma CLI as a dev dependency:
npm i -D prismayarn add -D prismapnpm add -D prismaInstall the pg driver and Prisma driver adapter for use with Hyperdrive:
npm i pg@>8.13.0 @prisma/adapter-pgyarn add pg@>8.13.0 @prisma/adapter-pgpnpm add pg@>8.13.0 @prisma/adapter-pgIf using TypeScript, install the types package:
npm i -D @types/pgyarn add -D @types/pgpnpm add -D @types/pgAdd the required Node.js compatibility flags and Hyperdrive binding to your wrangler.toml file:
{  "compatibility_flags": [    "nodejs_compat"  ],  "compatibility_date": "2024-09-23",  "hyperdrive": [    {      "binding": "HYPERDRIVE",      "id": "<your-hyperdrive-id-here>"    }  ]}# required for database drivers to functioncompatibility_flags = ["nodejs_compat"]compatibility_date = "2024-09-23"
[[hyperdrive]]binding = "HYPERDRIVE"id = "<your-hyperdrive-id-here>"Initialize Prisma in your application:
npx prisma initThis creates a prisma folder with a schema.prisma file and an .env file.
Define your database schema in the prisma/schema.prisma file:
generator client {  provider        = "prisma-client-js"  previewFeatures = ["driverAdapters"]}
datasource db {  provider = "postgresql"  url      = env("DATABASE_URL")}
model User {  id        Int      @id @default(autoincrement())  name      String  email     String   @unique  createdAt DateTime @default(now())}Add your database connection string to the .env file created by Prisma:
DATABASE_URL="postgres://user:password@host:port/database"Add helper scripts to your package.json:
"scripts": {  "migrate": "npx prisma migrate dev",  "generate": "npx prisma generate --no-engine",  "studio": "npx prisma studio"}Generate the Prisma client with driver adapter support:
npm run generateGenerate and apply the database schema:
npm run migrateWhen prompted, provide a name for the migration (for example, init).
Use your Hyperdrive configuration when using Prisma ORM. Update your src/index.ts file:
import { PrismaPg } from "@prisma/adapter-pg";import { PrismaClient } from "@prisma/client";
export interface Env {  HYPERDRIVE: Hyperdrive;}
export default {  async fetch(request, env, ctx): Promise<Response> {    // Create Prisma client using driver adapter with Hyperdrive connection string    const adapter = new PrismaPg({ connectionString: env.HYPERDRIVE.connectionString });    const prisma = new PrismaClient({ adapter });
    // Sample query to create and fetch users    const user = await prisma.user.create({      data: {        name: "John Doe",        email: `john.doe.${Date.now()}@example.com`,      },    });
    const allUsers = await prisma.user.findMany();
    return Response.json({      newUser: user,      allUsers: allUsers,    });  },} satisfies ExportedHandler<Env>;Deploy your Worker:
npx wrangler deploy- Learn more about How Hyperdrive Works.
- Refer to the troubleshooting guide to debug common issues.
- Understand more about other storage options available to Cloudflare Workers.
Was this helpful?
- Resources
- API
- New to Cloudflare?
- Directory
- Sponsorships
- Open Source
- Support
- Help Center
- System Status
- Compliance
- GDPR
- Company
- cloudflare.com
- Our team
- Careers
- © 2025 Cloudflare, Inc.
- Privacy Policy
- Terms of Use
- Report Security Issues
- Trademark