This guide shows you how to use Prisma ORM with Cloudflare D1, a serverless SQL database that runs on Cloudflare's edge network. You'll learn how to set up Prisma ORM with D1, handle migrations, and deploy your application to Cloudflare Workers. You can find a deployment-ready example on GitHub.
In your Prisma schema, set the provider of the datasource to sqlite. If you just bootstrapped the Prisma schema with prisma init, also be sure to add the runtime = "cloudflare" to the generator block and the following User model:
prisma/schema.prisma
generator client { provider = "prisma-client" output = "../src/generated/prisma" runtime = "cloudflare"}datasource db { provider = "sqlite"}model User { id Int @id @default(autoincrement()) email String @unique name String?}
Also, be sure to use a version of the Wrangler CLI that's above wrangler@^3.39.0, otherwise the --remote flag that's used in the next sections won't be available.
This command will authenticate you with Cloudflare and ask you to select a Cloudflare account. After that, it will create a new D1 database and output the database ID and name:
Bash
✅ Successfully created DB '__YOUR_D1_DATABASE_NAME__'in region __REGION__
Created your new D1 database.
{
"d1_databases": [
{
"binding": "DB",
"database_name": "__YOUR_D1_DATABASE_NAME__",
"database_id": "<unique-ID-for-your-database>"
}
]
}
Copy the terminal output and add the content to your wrangler.jsonc file. This file is used to configure your Cloudflare Worker and its bindings.
To connect your Workers with the D1 instance, add the following binding to your wrangler.jsonc:
wrangler.jsonc
{
"$schema": "node_modules/wrangler/config-schema.json",
"name": "d1-tutorial",
"main": "src/index.ts",
"compatibility_date": "2025-08-05",
"d1_databases": [
{
"binding": "DB",
"database_name": "__YOUR_D1_DATABASE_NAME__", // to be replaced"database_id": "__YOUR_D1_DATABASE_ID__"// to be replaced
}
]
}
For Cloudflare D1, you'll use Prisma's migration workflow combined with Wrangler CLI to manage your database schema. Since D1 is a serverless SQLite database, we'll use prisma migrate diff to generate migration SQL and then apply it using Wrangler.
This command generates a SQL file that contains the statements needed to create your database tables. You can inspect the generated SQL in prisma/migrations/0001_init.sql.
Insert a sample user into each database so the Worker has something to return:
bun
# For the local database
bunx wrangler d1 execute __YOUR_D1_DATABASE_NAME__ --command="INSERT INTO \"User\" (\"email\", \"name\") VALUES ('jane@prisma.io', 'Jane Doe (Local)');" --local# For the remote database
bunx wrangler d1 execute __YOUR_D1_DATABASE_NAME__ --command="INSERT INTO \"User\" (\"email\", \"name\") VALUES ('jane@prisma.io', 'Jane Doe (Remote)');" --remote
pnpm
# For the local database
pnpm dlx wrangler d1 execute __YOUR_D1_DATABASE_NAME__ --command="INSERT INTO \"User\" (\"email\", \"name\") VALUES ('jane@prisma.io', 'Jane Doe (Local)');" --local# For the remote database
pnpm dlx wrangler d1 execute __YOUR_D1_DATABASE_NAME__ --command="INSERT INTO \"User\" (\"email\", \"name\") VALUES ('jane@prisma.io', 'Jane Doe (Remote)');" --remote
yarn
# For the local database
yarn dlx wrangler d1 execute __YOUR_D1_DATABASE_NAME__ --command="INSERT INTO \"User\" (\"email\", \"name\") VALUES ('jane@prisma.io', 'Jane Doe (Local)');" --local# For the remote database
yarn dlx wrangler d1 execute __YOUR_D1_DATABASE_NAME__ --command="INSERT INTO \"User\" (\"email\", \"name\") VALUES ('jane@prisma.io', 'Jane Doe (Remote)');" --remote
npm
# For the local database
npx wrangler d1 execute __YOUR_D1_DATABASE_NAME__ --command="INSERT INTO \"User\" (\"email\", \"name\") VALUES ('jane@prisma.io', 'Jane Doe (Local)');" --local# For the remote database
npx wrangler d1 execute __YOUR_D1_DATABASE_NAME__ --command="INSERT INTO \"User\" (\"email\", \"name\") VALUES ('jane@prisma.io', 'Jane Doe (Remote)');" --remote
Before adding a Prisma Client query to your Worker, you need to generate Prisma Client with the following command:
bun
bunx prisma generate
pnpm
pnpm prisma generate
yarn
yarn prisma generate
npm
npx prisma generate
In order to query your database from the Worker using Prisma ORM, you need to:
Add the DB binding to the Env interface. This DB name matches the binding name you configured in wrangler.jsonc. (Alternatively, you can run npx wrangler types to generate the Env type from the binding in a separate file called worker-configuration.d.ts.)
Instantiate PrismaClient using the PrismaD1 driver adapter, passing env.DB which accesses your D1 database binding.
Send a query using Prisma Client and return the result.
Open src/index.ts and replace the entire content with the following:
With the database query in place and Prisma Client generated, you can run the Worker locally.
If your Worker needs any environment variables, create a .dev.vars file in the root of your project. For this example, we don't need any additional environment variables since the D1 binding is already configured in wrangler.jsonc.
Now run the Worker locally:
bun
bun run dev
pnpm
pnpm run dev
yarn
yarn dev
npm
npm run dev
Now you can open your browser at http://localhost:8787 to see the result of the database query:
no-copy
[{ id: 1, email: "jane@prisma.io", name: "Jane Doe (Local)" }];
Your deployed Worker is accessible via https://d1-tutorial.USERNAME.workers.dev (replace USERNAME with your Cloudflare account username). If you navigate your browser to that URL, you should see the following data that's queried from your remote D1 database:
no-copy
[{ id: 1, email: "jane@prisma.io", name: "Jane Doe (Remote)" }];