You can build applications on Cloudflare Workers that connect to Prisma Postgres with a few configuration steps! This project is a full sample based on Prisma's sample project.
- Run
npm install
to install project dependencies. - Rename
.dev.vars.example
to.dev.vars
and set yourDATABASE_URL
to the one provided in your Prisma Postgres instructions (this is required for local development using Cloudflare Workers Secrets). - Rename
.env.example
to.env
and set yourDATABASE_URL
to the one provided in your Prisma Postgres instructions (this is required for theprisma
CLI to be able to apply migrations). - Run
npx prisma migrate dev --name init
to create the tables in your Postgres database based on your schema. - Run
npx prisma generate --no-engine
to create a Prisma Client with the Accelerate extension. - Run
npm run dev
to run the project, and navigate tohttp://localhost:8787
to see the results. - Run
npx wrangler secret put DATABASE_URL
to set up your DATABASE_URL secret in Cloudflare. - Run
npm run deploy
to deploy the project.
- Run
npm install prisma @prisma/client@latest @prisma/extension-accelerate
to install the Prisma edge client and the Prisma Accelerate extension. - Run
npx prisma init
to create yourprisma/schema.prisma
schema files, which define the schema of your database. - Create a
.dev.vars
and set aDATABASE_URL
variable to the database URL provided in your Prisma Postgres instructions (this is required for local development using Cloudflare Workers Secrets). - Create a
.env
file and set aDATABASE_URL
variable to the database URL provided in your Prisma Postgres instructions (this is required for theprisma
CLI to be able to apply migrations). - Run
npx prisma migrate dev --name init
to create the tables in your Prisma Postgres database. - Run
npx prisma generate --no-engine
to create a Prisma Client with the Accelerate extension. - Configure your database URL with Cloudflare Workers and Prisma. You need to override the datasourceUrl in the PrismaClient instantiation with the database url provided by Cloudflare environment variables. https://www.prisma.io/docs/orm/reference/prisma-client-reference#programmatically-override-a-datasource-url. See the code in
src/index.ts
export default { async fetch(request, env, ctx): Promise<Response> { const connectionString = `${env.DATABASE_URL}`; console.log(connectionString); const prisma = new PrismaClient({ datasources: { db: { url: env.DATABASE_URL, }, }, }).$extends(withAccelerate()); ... } }
- Run
npm run dev
to run the project, and navigate tohttp://localhost:8787
to see the results. - Run
npx wrangler secret put DATABASE_URL
to set up your DATABASE_URL secret in Cloudflare. - Run
npm run deploy
to deploy the project.
- Is it possible to connect to Prisma Postgres using Cloudflare Hyperdrive? No, Prisma Postgres does not provide direct connection to the underlying Postgres database. It must be accessed through Prisma Accelerate which provides authentication, connection pooling and caching over HTTP.