Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 10 additions & 28 deletions content/800-guides/410-cloudflare-workers.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ This will create:

Cloudflare Workers needs Node.js compatibility enabled to work with Prisma. Add the `nodejs_compat` compatibility flag to your `wrangler.jsonc`:

```jsonc file=wrangler.jsonc
```json file=wrangler.jsonc
{
"name": "prisma-cloudflare-worker",
"main": "src/index.ts",
Expand All @@ -80,33 +80,9 @@ Cloudflare Workers needs Node.js compatibility enabled to work with Prisma. Add
}
```

### 2.3. Add environment types to Prisma config

Add the `Env` interface to your `prisma.config.ts` to provide type safety for environment variables:
### 2.3. Define your Prisma Schema

```typescript file=prisma.config.ts
import "dotenv/config";
import { defineConfig, env } from "prisma/config";

//add-start
export interface Env {
DATABASE_URL: string;
}
//add-end

import { defineConfig, env } from 'prisma/config';
export default defineConfig({
schema: 'prisma/schema.prisma',
migrations: {
path: 'prisma/migrations',
},
datasource: {
url: env('DATABASE_URL'),
},
});
```

### 2.4. Define your Prisma schema

In the `prisma/schema.prisma` file, add the following `User` model and set the runtime to `cloudflare`:

Expand All @@ -131,9 +107,15 @@ model User {
//add-end
```

:::note

Both the `cloudflare` and `workerd` runtimes are supported. Read more about runtimes [here](/orm/prisma-schema/overview/generators#field-reference).

:::

This creates a `User` model with an auto-incrementing ID, email, and name.

### 2.5. Configure Prisma scripts
### 2.4. Configure Prisma scripts

Add the following scripts to your `package.json` to work with Prisma in the Cloudflare Workers environment:

Expand All @@ -150,7 +132,7 @@ Add the following scripts to your `package.json` to work with Prisma in the Clou
}
```

### 2.6. Run migrations and generate Prisma Client
### 2.5. Run migrations and generate Prisma Client

Now, run the following command to create the database tables:

Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading