<aside> ℹ️ These are instructions on how to use Prisma ORM on Cloudflare Workers or Pages with Cloudflare D1 as the database. This is part of the Prisma + Edge Functions Early Access.

</aside>

Introduction

Welcome to the Early Access of edge function deployment support in Prisma ORM! This Early Access lets you use Prisma Client on Cloudflare Workers or Cloudflare Pages with Cloudflare D1 as the database.

image (18).png

If you want to use Prisma on Cloudflare with another database (for example PostgreSQL, Neon, PlanetScale, or Turso), or on Vercel Edge Functions or Middleware, please check out our general Prisma + Edge Functions Early Access page.

How to use it

To use the Early Access version to test D1 on Cloudflare Workers or Pages with Prisma, follow these instructions:

Installation

During the early access, you use special @early-access versions of Prisma CLI and Prisma Client. Later, when we reach Preview, this will be available as part of our normal, stable releases.

npm install prisma@early-access --save-dev
npm install @prisma/client@early-access
npm install @prisma/adapter-d1@early-access
npm install @cloudflare/workers-types --save-dev
npm install wrangler --save-dev

Usage

To enable support for edge functions deployments on Cloudflare and the D1 driver adapter, you need to use the driverAdapters preview feature in your generator block in your Prisma schema:

// schema.prisma
generator client {
  provider        = "prisma-client-js"
+	previewFeatures = ["driverAdapters"]
}

datasource db {
  provider = "sqlite"
  url      = "file:./dev.db"
}

Run npx prisma generate to re-generate your Prisma Client.

Then set up your D1 database in your wrangler.toml:

[[d1_databases]]
binding = "DATABASE"    # i.e. available in the Worker on env.DATABASE
database_name = "database_name"
database_id = "<unique-ID-for-your-database>"

(For Cloudflare Pages, the bindings are configured slightly different.)