Prisma ORM adds native D1 support for Cloudflare Workers

Database work can slow down even experienced teams. As data models grow beyond basic CRUD operations, developers face harder iteration cycles, more complex migrations, and trickier debugging. Cloudflare Workers and D1 address the infrastructure side of that problem, while Prisma ORM focuses on the data interaction layer. With Prisma ORM version 5.12.0 and later, developers can now use the Prisma Client API directly against D1 from Cloudflare Workers — support that is currently in Preview.

Prisma ORM combines an intuitive data modeling language, an automated migration workflow, and a type-safe JavaScript/TypeScript client. The ecosystem also includes Accelerate and Pulse, products running on Cloudflare that handle connection pooling, query caching, and real-time database subscriptions. The new D1 adapter is available via the @prisma/adapter-d1 package.

Setting up a Worker with Prisma and D1

Start from a new Cloudflare Workers project scaffolded with TypeScript:

npm create cloudflare@latest

Name the project (for example, my-d1-prisma-app) and choose the "Hello World" TypeScript template. Skip deployment for now and install dependencies:

cd my-d1-prisma-app && npm install

Create a D1 database for the app, then add the [[d1_databases]] binding configuration to wrangler.toml. That binding is what lets the Worker communicate with D1. Next, install the Prisma packages:

npm install prisma@latest @prisma/client@latest @prisma/adapter-d1

Initialize the Prisma schema with SQLite as the datasource provider, since D1 uses SQLite's SQL dialect:

npx prisma init --datasource-provider sqlite

In the generated prisma/schema.prisma file, enable the driverAdapters Preview feature and define a model. A simple example tracks site visits:

model Visit {
  id String @id @default(cuid())
  time DateTime @default(now())
}

Creating and applying migrations

With the schema in place, generate an empty migration file using Wrangler, then have Prisma fill it in. When prompted, choose to create a migrations folder at the project root. Use the prisma migrate diff command to capture the difference between the empty database and the Prisma schema, then save that to the migration directory:

npx prisma migrate diff --script --from-empty --to-schema-datamodel prisma/schema.prisma --output migrations/0001_init.sql

Now apply the migration to both your local and remote D1 instances using Wrangler, and regenerate the Prisma Client so it's ready to query:

npx prisma generate

Querying D1 from a Worker

In the Worker code, import PrismaClient and PrismaD1, then define the D1 binding and instantiate the client. A sample request handler can report the current visitor count like this:

export default {
  async fetch(request, env) {
    const prisma = new PrismaClient({ adapter: new PrismaD1(env.DB) });
    // query using prisma.visit.count() or prisma.visit.create(...)
  }
}

With one route that returns the visitor count and another that creates a new visit record whenever someone hits it, the app is fully wired: Cloudflare Workers handles the edge request, D1 persists the data, and Prisma ORM handles the query logic with full TypeScript type safety.

For more advanced workflows, refer to the Prisma documentation covering D1 deployment, data migration patterns, and extending the Prisma Client for project-specific needs.