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
Initialize Prisma ORM in your Worker project:
npx prisma@latest orm init --yes --target postgres --authoring psl --write-envyarn dlx prisma@latest orm init --yes --target postgres --authoring psl --write-envpnpx prisma@latest orm init --yes --target postgres --authoring psl --write-envThe command installs @prisma/orm-postgres, which includes the pg driver, and the Prisma CLI. It also creates src/prisma/contract.prisma for your data model, prisma.config.ts, which reads DATABASE_URL from .env, and a .env file with a placeholder DATABASE_URL.
Add the required Node.js compatibility flags and Hyperdrive binding to your Wrangler configuration file:
{
// required for database drivers to function
"compatibility_flags": [
"nodejs_compat"
],
// Set this to today's date
"compatibility_date": "2026-11-12",
"hyperdrive": [
{
"binding": "HYPERDRIVE",
"id": "<your-hyperdrive-id-here>"
}
]
}compatibility_flags = [ "nodejs_compat" ]
# Set this to today's date
compatibility_date = "2026-11-12"
[[hyperdrive]]
binding = "HYPERDRIVE"
id = "<your-hyperdrive-id-here>"Define your data model in the src/prisma/contract.prisma file:
// use prisma-8
model User {
id Int @id @default(autoincrement())
name String
email String @unique
createdAt DateTime @default(now()) @onCreate(now())
}Replace the placeholder DATABASE_URL in the .env file with your database connection string. The Prisma CLI uses it to create and update tables, connecting to the database directly rather than through Hyperdrive:
DATABASE_URL="postgres://user:password@host:port/database"Emit the contract files that the Worker imports, then create the tables in your database:
npx prisma contract emit
npx prisma db initcontract emit writes src/prisma/contract.json and src/prisma/contract.d.ts next to your contract. Run it again whenever you change the model, followed by npx prisma db update to apply the change to the database.
Use your Hyperdrive configuration when using Prisma ORM. Update your src/index.ts file:
import postgres from "@prisma/orm-postgres/runtime";
import { contractFromJson } from "@prisma/orm-postgres/static";
import type { Contract } from "./prisma/contract.d";
import contractJson from "./prisma/contract.json" with { type: "json" };
const contract = contractFromJson<Contract>(contractJson);
export default {
async fetch(request, env, ctx): Promise<Response> {
// Create a Prisma ORM client with the Hyperdrive connection string
const db = postgres({ contract, url: env.HYPERDRIVE.connectionString });
try {
// Sample query to create and fetch users
const user = await db.orm.public.User.create({
name: "John Doe",
email: `john.doe.${Date.now()}@example.com`,
});
const allUsers = await db.orm.public.User.all();
return Response.json({
newUser: user,
allUsers: allUsers,
});
} finally {
// Close the connection after the response is sent
ctx.waitUntil(db.close());
}
},
} satisfies ExportedHandler<Env>;Run npx wrangler types so that the generated Env type includes the HYPERDRIVE binding.
To run the Worker locally with wrangler dev, set a localConnectionString for the binding so that the Worker connects directly to your database.
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.