Repository navigation
docs(cloudflare): update the Prisma ORM how-to for Prisma 7 - #227
ankur-arch wants to merge 1 commit into
Conversation
Prisma ORM 7 uses the prisma-client generator, which requires an output directory and runtime = "cloudflare" to run in workerd. Keep the existing instructions for the prisma-client-js generator and scope the serverExternalPackages advice to adapter versions before 1.20.15.
| } | ||
| ``` | ||
|
|
||
| Run `npx prisma generate` before building your app, then import `PrismaClient` from the output directory as shown in the examples below. |
There was a problem hiding this comment.
nit: the examples below import from ../generated/prisma/client, which only resolves when lib/db.ts is at the project root and the schema is in prisma/. Many apps use a src/ layout. For example, examples/prisma-7 in the adapter repo uses output = "../src/generated/prisma". Maybe make this explicit:
| Run `npx prisma generate` before building your app, then import `PrismaClient` from the output directory as shown in the examples below. | |
| Run `npx prisma generate` before building your app, then import `PrismaClient` from the `client` entrypoint in the output directory as shown in the examples below. The import path is relative to the file that imports it, so adjust it to your project layout (or use the `@/` path alias). |
|
|
||
| If you leave out `runtime`, the app still builds but every query fails in the Worker with `CompileError: WebAssembly.Module(): Wasm code generation disallowed by embedder`. | ||
|
|
||
| The generated client imports a WebAssembly module. This works with Turbopack, which is the default bundler since Next.js 16. A webpack build (`next build --webpack`) fails on `query_compiler_fast_bg.wasm?module` with a `Module parse failed` error. If you build with webpack, use the `prisma-client-js` generator described below. |
There was a problem hiding this comment.
nit: on Next.js 15, webpack is still the default bundler, so people using prisma-client there need to opt into Turbopack. Something like:
| The generated client imports a WebAssembly module. This works with Turbopack, which is the default bundler since Next.js 16. A webpack build (`next build --webpack`) fails on `query_compiler_fast_bg.wasm?module` with a `Module parse failed` error. If you build with webpack, use the `prisma-client-js` generator described below. | |
| The generated client imports a WebAssembly module. This works with Turbopack, which is the default bundler since Next.js 16 (on Next.js 15, use `next build --turbopack`). A webpack build (`next build --webpack`) fails on `query_compiler_fast_bg.wasm?module` with a `Module parse failed` error. If you build with webpack, use the `prisma-client-js` generator described below. |
|
|
||
| `prisma-client-js` is the default generator in Prisma ORM 6 and is still available in Prisma ORM 7. When using this generator, you do not want to provide an output directory for the generated client. | ||
|
|
||
| ```prisma filename="schema.prisma" |
There was a problem hiding this comment.
nit: the filename here is different from the block above (prisma/schema.prisma).
| ```prisma filename="schema.prisma" | |
| ```prisma filename="prisma/schema.prisma" |
| provider = "prisma-client-js" | ||
| previewFeatures = ["driverAdapters"] |
There was a problem hiding this comment.
nit: the snippet still enables driverAdapters, but line 140 tells readers to remove it on 6.16+. It might read better if the snippet showed what most readers should use, with the preview feature called out as only needed on older versions
|
|
||
| This is because the generated client needs to be patched by OpenNext to work with Cloudflare Workers. If you provide an output directory, OpenNext will not be able to patch the client and it will not work. | ||
|
|
||
| With this generator, replace the `../generated/prisma/client` import with `@prisma/client` in the examples below. Prisma ORM 6.16 and later print a deprecation warning for the `driverAdapters` preview feature, which you can then remove. |
There was a problem hiding this comment.
| With this generator, replace the `../generated/prisma/client` import with `@prisma/client` in the examples below. Prisma ORM 6.16 and later print a deprecation warning for the `driverAdapters` preview feature, which you can then remove. | |
| With this generator, replace the `../generated/prisma/client` import with `@prisma/client` in the examples below. On Prisma ORM versions before 6.16, you also need to add `previewFeatures = ["driverAdapters"]` to the generator. |
The Prisma ORM section of the database how-to says that you should not provide an output directory for the generated client. Prisma ORM 7 sets up the
prisma-clientgenerator by default, and that generator requiresoutput, so readers who start fromprisma initget conflicting instructions. This PR documents the Prisma 7 generator and keeps the existing instructions forprisma-client-js, which are still correct for that generator.Changes
Only the
## Prisma ORMsection ofpages/cloudflare/howtos/db.mdxchanges.schema.prisma: shows theprisma-clientgenerator withoutputandruntime = "cloudflare", names the error you get whenruntimeis missing (CompileError: WebAssembly.Module(): Wasm code generation disallowed by embedder), and notes that the generated client needs a Turbopack build because webpack fails on the.wasm?moduleimport.prisma-client-jsgenerator heading. They hold on Prisma ORM 6 and on 7 with that generator.next.config.ts:serverExternalPackagesis now described as needed only before@opennextjs/cloudflare1.20.15, matching whathowtos/workerd.mdxalready says.lib/db.tsexamples importPrismaClientfrom the generated directory. The "Instead of that" D1 example now passes the adapter toPrismaClient, and the PostgreSQL paragraph names the adapter that the code uses (PrismaPg).Testing
I tested with a fresh Next.js 16.4.5 app,
@opennextjs/cloudflare1.20.19, wrangler 4.170.0 and Prisma ORM 7.10.1. Each case was built withopennextjs-cloudflare buildand run in workerd withopennextjs-cloudflare preview, then exercised with a route handler that writes and reads.prisma-clientwithruntime = "cloudflare"works with@prisma/adapter-d1on local D1 and with@prisma/adapter-pgon a hosted Postgres database, using the code blocks from this page as they are.runtime = "workerd"works too.runtime, the build succeeds and every query fails with the error quoted in the page.next build --webpack, the build fails onquery_compiler_fast_bg.wasm?module.prisma-client-jswithoutoutputworks on Prisma ORM 7.10.1 and 6.19.3, with and withoutserverExternalPackages, and also with a webpack build on 7.10.1. With a customoutputit fails at query time on both versions.PrismaClientwithPrismaPgshared across requests hangs on the second request, so the per-request guidance still applies.bun run prettier:checkandbun next buildpass.Limitations
I could not deploy to Cloudflare from my machine, so D1 and Hyperdrive were only tested with wrangler's local emulation. I did not test adapter versions older than 1.20.15 or Next.js 15; the 1.20.15 boundary comes from
howtos/workerd.mdx.Disclosure: I work at Prisma.