-
Notifications
You must be signed in to change notification settings - Fork 11.4k
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.
2. Mention the need to include the nodejs_compat flag
In the Postgres tutorial it's mentioned that "Node.js compatibility 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.
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 \
('[email protected]', 'Jane Doe (Local)');" --local
npx wrangler d1 execute prisma-demo-db --command "INSERT INTO \"User\" (\"email\", \"name\") VALUES \
('[email protected]', '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 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, for reference. I hope this is helpful!
Additional information
No response