cloudflare / cloudflare/cloudflare-docs

Update Workers Prisma/D1 tutorial for Prisma 7.2

Open
#27,388 3 comments 0 reactions 10 assignees Claimed by @rozenmd View on GitHub
content:edit documentation product:d1 product:workers stale
Dominant language
MDX
Stars
5.2k
Forks
16.7k
Avg merge
2d 6h
Merged PRs (30d)
337

Description

### Existing documentation URL(s)

https://developers.cloudflare.com/d1/tutorials/d1-and-prisma-orm/

### What changes are you suggesting?

I was just following the D1/Prisma tutorial, but I encountered a few issues, some of which related to the fact that there is a new major version of Prisma (7) which differs from the one used at the time the tutorial was written. I would suggest the following changes:

### 1. Update generator block

In the generator block in `schema.prisma`:

```
generator client {
provider = "prisma-client-js"
output = "../src/generated/prisma"
previewFeatures = ["driverAdapters"]
}
```

should become:

```
generator client {
provider = "prisma-client-js"
output = "../src/generated/prisma"
runtime = "cloudflare"
}
```

This is because the "driverAdapters" preview feature flag is no longer necessary, and the worker will break (locally at least) without the "cloudflare" runtime flag. [This is specified in Prisma's documentation.](https://www.prisma.io/docs/guides/cloudflare-d1)

### 2. Mention the need to include the nodejs_compat flag

In the [Postgres tutorial](https://developers.cloudflare.com/workers/tutorials/postgres/) it's mentioned that "[Node.js compatibility](https://developers.cloudflare.com/workers/runtime-apis/nodejs/) is required for database drivers, including Postgres.js, and needs to be configured for your Workers project." This is also applicable for this tutorial, but is not mentioned anywhere, with the only reference to this flag being in the example `wrangler.jsonc` file (with no info being provided about it).

### 3. Outdated Prisma migration command

`npx prisma migrate diff --from-empty --to-schema-datamodel ./prisma/schema.prisma --script --output migrations/0001_create_user_table.sql`
now fails with
```
Error:
`--to-schema-datamodel` was removed. Please use `--[from/to]-schema` instead.
```

Replace with:

```
npx prisma migrate diff \
--from-empty \
--to-schema ./prisma/schema.prisma \
--script \
--output migrations/0001_create_user_table.sql
```

As specified in the [current Prisma Docs](https://www.prisma.io/docs/orm/overview/databases/cloudflare-d1).

### 4. Other commands fail on copy paste due to line break

If keeping them in two lines for readability (which makes sense), add " \ " at the end to escape of the line so they are correctly processed as a single command:

```
npx wrangler d1 execute prisma-demo-db --command "INSERT INTO \"User\" (\"email\", \"name\") VALUES \
('jane@prisma.io', 'Jane Doe (Local)');" --local

npx wrangler d1 execute prisma-demo-db --command "INSERT INTO \"User\" (\"email\", \"name\") VALUES \
('jane@prisma.io', 'Jane Doe (Remote)');" --remote
```

### 5. Add a client disconnection at the end of the process for better memory management

This is mentioned by the [Prisma docs](https://www.prisma.io/docs/guides/cloudflare-d1#6-implement-the-worker) as well and seems like good practice, even for this kind of basic tutorial.

`ctx.waitUntil(prisma.$disconnect()); // or just await prisma.$disconnect()`

You can find my repo with these changes [here](https://github.com/diogoascarneiro/prisma-7.2-d1-example), for reference. I hope this is helpful!

### Additional information

_No response_

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.