Skip to content

Prisma ORM

Last updated View as MarkdownAgent setup

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.

Prerequisites

1. Install Prisma ORM

Initialize Prisma ORM in your Worker project:

npx prisma@latest orm init --yes --target postgres --authoring psl --write-env

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

2. Configure Prisma ORM

2.1. Define a data model

Define your data model in the src/prisma/contract.prisma file:

src/prisma/contract.prismaprisma
// use prisma-8

model User {
  id        Int      @id @default(autoincrement())
  name      String
  email     String   @unique
  createdAt DateTime @default(now()) @onCreate(now())
}

2.2. Set up environment variables

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:

.envtxt
DATABASE_URL="postgres://user:password@host:port/database"

2.3. Emit the contract and create the tables

Emit the contract files that the Worker imports, then create the tables in your database:

npx prisma contract emit
npx prisma db init

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

3. Connect Prisma ORM to Hyperdrive

Use your Hyperdrive configuration when using Prisma ORM. Update your src/index.ts file:

src/index.tsts
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.

4. Deploy your Worker

Deploy your Worker:

npx wrangler deploy

Next steps

Was this helpful?