rembrembdocs

Learn the things you need to know in order to deploy an app that uses Prisma Client for talking to a database to a Cloudflare Worker or to Cloudflare Pages

Questions answered in this page

This section covers general things you need to be aware of when deploying to Cloudflare Workers or Pages and are using Prisma ORM, regardless of the database provider you use.

Using Prisma Postgres

You can use Prisma Postgres and deploy to Cloudflare Workers.

After you create a Worker, run:

Enter a name for your project and choose a database region.

This command:

Using an edge-compatible driver

When deploying a Cloudflare Worker that uses Prisma ORM, you need to use an edge-compatible driver and its respective driver adapter for Prisma ORM.

The edge-compatible drivers for Cloudflare Workers and Pages are:

There's also work being done on the node-mysql2 driver which will enable access to traditional MySQL databases from Cloudflare Workers and Pages in the future as well.

If your application uses PostgreSQL, we recommend using Prisma Postgres. It is fully supported on edge runtimes and does not require a specialized edge-compatible driver. Review the Prisma Postgres serverless driver limitations to understand current constraints.

Setting your database connection URL as an environment variable

First, ensure that your datasource block in your Prisma schema is configured correctly. Database connection URLs are configured in prisma.config.ts:

datasource db {
  provider = "postgresql" // this might also be `mysql` or another value depending on your database
}

prisma.config.ts

import "dotenv/config";
import { defineConfig, env } from "prisma/config";

export default defineConfig({
  schema: "prisma/schema.prisma",
  datasource: {
    url: env("DATABASE_URL"),
  },
});

Development

When using your Worker in development, you can configure your database connection via the .dev.vars file locally.

Assuming you use the DATABASE_URL environment variable from above, you can set it inside .dev.vars as follows:

.dev.vars

DATABASE_URL="your-database-connection-string"

In the above snippet, your-database-connection-string is a placeholder that you need to replace with the value of your own connection string, for example:

.dev.vars

DATABASE_URL="postgresql://admin:mypassword42@somehost.aws.com:5432/mydb"

Note that the .dev.vars file is not compatible with .env files which are typically used by Prisma ORM.

This means that you need to make sure that Prisma ORM gets access to the environment variable when needed, e.g. when running a Prisma CLI command like prisma migrate dev.

There are several options for achieving this:

Production

When deploying your Worker to production, you'll need to set the database connection using the wrangler CLI:

The command is interactive and will ask you to enter the value for the DATABASE_URL env var as the next step in the terminal.

Size limits on free accounts

Cloudflare has a size limit of 3 MB for Workers on the free plan. If your application bundle with Prisma ORM exceeds that size, we recommend upgrading to a paid Worker plan.

Deploying a Next.js app to Cloudflare Pages with @cloudflare/next-on-pages

Cloudflare offers an option to run Next.js apps on Cloudflare Pages with @cloudflare/next-on-pages, see the docs for instructions.

Based on some testing, we found the following:

Feel free to reach out to us on Discord if you find that anything has changed about this.

This section provides database-specific instructions for deploying a Cloudflare Worker with Prisma ORM.

Prerequisites

As a prerequisite for the following section, you need to have a Cloudflare Worker running locally and the Prisma CLI installed.

If you don't have that yet, you can run these commands:

You'll further need a database instance of your database provider of choice available. Refer to the respective documentation of the provider for setting up that instance.

We'll use the default User model for the example below:

model User {
  id    Int     @id @default(autoincrement())
  email String  @unique
  name  String?
}

PostgreSQL (traditional)

If you are using a traditional PostgreSQL database that's accessed via TCP and the pg driver, you need to:

1. Configure Prisma schema & database connection

First, ensure that the database connection is configured properly. Database connection URLs are configured in prisma.config.ts:

schema.prisma

generator client {
  provider = "prisma-client"
  output   = "./generated"
}

datasource db {
  provider = "postgresql"
}

prisma.config.ts

import "dotenv/config";
import { defineConfig, env } from "prisma/config";

export default defineConfig({
  schema: "prisma/schema.prisma",
  datasource: {
    url: env("DATABASE_URL"),
  },
});

Next, you need to set the DATABASE_URL environment variable to the value of your database connection string. You'll do this in a file called .dev.vars used by Cloudflare:

.dev.vars

DATABASE_URL="postgresql://admin:mypassword42@somehost.aws.com:5432/mydb"

Because the Prisma CLI by default is only compatible with .env files, you can adjust your package.json with the following script that loads the env vars from .dev.vars. You can then use this script to load the env vars before executing a prisma command.

Add this script to your package.json:

package.json

{
  // ...
  "scripts": {
    // ....
    "env": "dotenv -e .dev.vars"
  },
  // ...
}

Now you can execute Prisma CLI commands as follows while ensuring that the command has access to the env vars in .dev.vars:

2. Install dependencies

Next, install the required packages:

3. Set node_compat = true in wrangler.toml

In your wrangler.toml file, add the following line:

wrangler.toml

node_compat = true

4. Migrate your database schema (if applicable)

If you ran npx prisma init above, you need to migrate your database schema to create the User table that's defined in your Prisma schema (if you already have all the tables you need in your database, you can skip this step):

5. Use Prisma Client in your Worker to send a query to the database

Here is a sample code snippet that you can use to instantiate PrismaClient and send a query to your database:

import { PrismaClient } from "./generated/client";
import { PrismaPg } from "@prisma/adapter-pg";

export default {
  async fetch(request, env, ctx) {
    const adapter = new PrismaPg({ connectionString: env.DATABASE_URL });
    const prisma = new PrismaClient({ adapter });

    const users = await prisma.user.findMany();
    const result = JSON.stringify(users);
    ctx.waitUntil(prisma.$disconnect());
    return new Response(result);
  },
};

6. Run the Worker locally

To run the Worker locally, you can run the wrangler dev command:

7. Set the DATABASE_URL environment variable and deploy the Worker

To deploy the Worker, you first need to the DATABASE_URL environment variable via the wrangler CLI:

The command is interactive and will ask you to enter the value for the DATABASE_URL env var as the next step in the terminal.

Then you can go ahead then deploy the Worker:

The command will output the URL where you can access the deployed Worker.

PlanetScale

If you are using a PlanetScale database, you need to:

1. Configure Prisma schema & database connection

First, ensure that the database connection is configured properly. Database connection URLs are configured in prisma.config.ts:

schema.prisma

generator client {
  provider = "prisma-client"
  output   = "./generated"
}

datasource db {
  provider     = "mysql"
  relationMode = "prisma" // required for PlanetScale (as by default foreign keys are disabled)
}

prisma.config.ts

import "dotenv/config";
import { defineConfig, env } from "prisma/config";

export default defineConfig({
  schema: "prisma/schema.prisma",
  datasource: {
    url: env("DATABASE_URL"),
  },
});

Next, you need to set the DATABASE_URL environment variable to the value of your database connection string. You'll do this in a file called .dev.vars used by Cloudflare:

.dev.vars

DATABASE_URL="mysql://32qxa2r7hfl3102wrccj:password@us-east.connect.psdb.cloud/demo-cf-worker-ps?sslaccept=strict"

Because the Prisma CLI by default is only compatible with .env files, you can adjust your package.json with the following script that loads the env vars from .dev.vars. You can then use this script to load the env vars before executing a prisma command.

Add this script to your package.json:

package.json

{
  // ...
  "scripts": {
    // ....
    "env": "dotenv -e .dev.vars"
  },
  // ...
}

Now you can execute Prisma CLI commands as follows while ensuring that the command has access to the env vars in .dev.vars:

2. Install dependencies

Next, install the required packages:

3. Migrate your database schema (if applicable)

If you ran npx prisma init above, you need to migrate your database schema to create the User table that's defined in your Prisma schema (if you already have all the tables you need in your database, you can skip this step):

4. Use Prisma Client in your Worker to send a query to the database

Here is a sample code snippet that you can use to instantiate PrismaClient and send a query to your database:

import { PrismaClient } from "./generated/client";
import { PrismaPlanetScale } from "@prisma/adapter-planetscale";

export default {
  async fetch(request, env, ctx) {
    const adapter = new PrismaPlanetScale({
      url: env.DATABASE_URL,
      // see https://github.com/cloudflare/workerd/issues/698
      fetch(url, init) {
        delete init["cache"];
        return fetch(url, init);
      },
    });
    const prisma = new PrismaClient({ adapter });

    const users = await prisma.user.findMany();
    const result = JSON.stringify(users);
    ctx.waitUntil(prisma.$disconnect());
    return new Response(result);
  },
};

6. Run the Worker locally

To run the Worker locally, you can run the wrangler dev command:

7. Set the DATABASE_URL environment variable and deploy the Worker

To deploy the Worker, you first need to the DATABASE_URL environment variable via the wrangler CLI:

The command is interactive and will ask you to enter the value for the DATABASE_URL env var as the next step in the terminal.

Then you can go ahead then deploy the Worker:

The command will output the URL where you can access the deployed Worker.

Neon

If you are using a Neon database, you need to:

1. Configure Prisma schema & database connection

First, ensure that the database connection is configured properly. Database connection URLs are configured in prisma.config.ts:

schema.prisma

generator client {
  provider = "prisma-client"
  output   = "./generated"
}

datasource db {
  provider = "postgresql"
}

prisma.config.ts

import "dotenv/config";
import { defineConfig, env } from "prisma/config";

export default defineConfig({
  schema: "prisma/schema.prisma",
  datasource: {
    url: env("DATABASE_URL"),
  },
});

Next, you need to set the DATABASE_URL environment variable to the value of your database connection string. You'll do this in a file called .dev.vars used by Cloudflare:

.dev.vars

DATABASE_URL="postgresql://janedoe:password@ep-nameless-pond-a23b1mdz.eu-central-1.aws.neon.tech/neondb?sslmode=require"

Because the Prisma CLI by default is only compatible with .env files, you can adjust your package.json with the following script that loads the env vars from .dev.vars. You can then use this script to load the env vars before executing a prisma command.

Add this script to your package.json:

package.json

{
  // ...
  "scripts": {
    // ....
    "env": "dotenv -e .dev.vars"
  },
  // ...
}

Now you can execute Prisma CLI commands as follows while ensuring that the command has access to the env vars in .dev.vars:

2. Install dependencies

Next, install the required packages:

3. Migrate your database schema (if applicable)

If you ran npx prisma init above, you need to migrate your database schema to create the User table that's defined in your Prisma schema (if you already have all the tables you need in your database, you can skip this step):

5. Use Prisma Client in your Worker to send a query to the database

Here is a sample code snippet that you can use to instantiate PrismaClient and send a query to your database:

import { PrismaClient } from "./generated/client";
import { PrismaNeon } from "@prisma/adapter-neon";

export default {
  async fetch(request, env, ctx) {
    const adapter = new PrismaNeon({ connectionString: env.DATABASE_URL });
    const prisma = new PrismaClient({ adapter });

    const users = await prisma.user.findMany();
    const result = JSON.stringify(users);
    ctx.waitUntil(prisma.$disconnect());
    return new Response(result);
  },
};

6. Run the Worker locally

To run the Worker locally, you can run the wrangler dev command:

7. Set the DATABASE_URL environment variable and deploy the Worker

To deploy the Worker, you first need to the DATABASE_URL environment variable via the wrangler CLI:

The command is interactive and will ask you to enter the value for the DATABASE_URL env var as the next step in the terminal.

Then you can go ahead then deploy the Worker:

The command will output the URL where you can access the deployed Worker.

Cloudflare D1