Skip to content

Commit c042ac9

Browse files
authored
feat: Support prisma >6.7 in standard starter (#457)
## Context * Updates the standard starter * to use prisma 6.7 * to use the new `prisma-client` generator: prisma will now generate code to a gitignored `generated/prisma` dir * to use the new `queryCompiler` flag to reduce the size of the WASM (by around ~350KB) --- ## 🔁 Prisma >6.7 migration guide for existing projects Prisma introduced a new client generator ([`prisma-client`](https://www.prisma.io/docs/orm/prisma-schema/overview/generators#prisma-client-early-access)) that lets us generate a client specific to Cloudflare workers (via workerd runtime) and ESM - this makes for much simpler and less error prone Vite integration. This guide walks you through upgrading an existing RedwoodSDK Standard Starter project. ### **1\. ✅ Update Dependencies** Update the following packages in your package.json: ``` - "@prisma/adapter-d1": "~6.5.0", - "@prisma/client": "~6.5.0", - "prisma": "~6.5.0", + "@prisma/adapter-d1": "~6.8.2", + "@prisma/client": "~6.8.2", + "prisma": "~6.8.2", ``` Also upgrade wrangler to at least ^4.16.0. ``` - "wrangler": "^4.14.1" + "wrangler": "^4.16.0" ``` * * * * * ### **2\. 🧬 Update ** ### **schema.prisma** Change the generator block to use the new prisma-client: ``` -generator client { - provider = "prisma-client-js" - previewFeatures = ["driverAdapters"] - output = "../node_modules/.prisma/client" +generator client { + provider = "prisma-client" + runtime = "workerd" + moduleFormat = "esm" + generatedFileExtension = "ts" + importFileExtension = "ts" + output = "../generated/prisma" + previewFeatures = ["queryCompiler", "driverAdapters"] } ``` * * * * * ### **3\. 🗂️ Adjust TypeScript Paths** In tsconfig.json: ``` - ".prisma/*": ["./node_modules/.prisma/*"] +"@/*": ["./src/*"], + "@generated/*": ["./generated/*"] ``` Then update imports from `@prisma/client` to `@/db.` * * * * * ### **4\. 🛠️ Refactor DB Client Usage** Replace your `src/db.ts` with the latest in the standard starter: https://github.com/redwoodjs/sdk/blob/main/starters/standard/src/db.ts * * * * * ### 5\. 📁 Add to **.gitignore** ``` +generated/ ``` * * * * * ### **6\. 📦 Re-generate Client** After updating schema.prisma: ``` pnpm prisma generate ``` * * * * * ### **7\. 🧪 Test Everything** Check your queries work in development and deployments. --- ## Notes ### Steps taken https://gist.github.com/justinvdm/24a9f316f7c5148be852c4660e5f0481 ### Update: 21 May 2025 - Submitted PR for the relevant fix required in `@cloudflare/vite-plugin`: cloudflare/workers-sdk#9322 - Got this PR working on both prisma <6.7 and >6.7 (assuming cloudflare/workers-sdk#9322 fix is in and PR upgraded to use it) - See [action log](https://gist.github.com/justinvdm/24a9f316f7c5148be852c4660e5f0481) for more detail on steps taken ### Update: 19 May 2025 - **Goal**: ==Upgrade to Prisma >=6.7, remove Prisma-specific Vite logic from RedwoodSDK, avoid WASM if possible, and maintain backwards compatibility. - **Steps Taken**: - Upgraded to Prisma 6.8.2 with queryCompiler and driverAdapters. - Tried out different vite configurations and aliases to target new Prisma client structure. - Tested various entry points (edge.js, index.js, wasm.js) and generation configs - Validated new `prisma-client` generator for `workerd` + `esm `output. - **Current Status**: Generated client works and loads in dev with SDK changes. DB client instantiation succeeds. Queries fail due to WASM module resolution error during runtime. - **Blocking Issue**: Cloudflare's Vite plugin fails to resolve Prisma's WASM module path, likely due to pnpm's path format (`__`) interfering with Cloudflare's module detection regex. Investigating patch to regex logic in `@cloudflare/vite-plugin`. - **Findings**: - **Not WASM free (yet?):** Prisma is not WASM free yet, but they're gradually moving the components out of rust into ts. There's still a **1.66MB** wasm file we must work with. Previously it was **2MB**. - **Cloudflare's plugin appears to mangle WASM imports** when the file path includes double underscores (__), likely due to greedy regex - **Switching to Prisma's new ESM generator is promising** for us to avoid complexities - e.g. we can avoid having to get optimizeDeps to esm-ify the `require`s in the generated prisma client (though we'll still need some logic for <6.7) - **Next Step**: Investigate patch, then corresponding PR for Cloudflare's Vite plugin to fix its WASM module resolution regex handling for __ in paths.
1 parent 60f7fd8 commit c042ac9

File tree

11 files changed

+4696
-6058
lines changed

11 files changed

+4696
-6058
lines changed

pnpm-lock.yaml

Lines changed: 65 additions & 672 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

sdk/src/vite/checkIsUsingPrisma.mts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
import enhancedResolve from "enhanced-resolve";
2+
import { pathExists } from "fs-extra";
3+
import { resolve } from "path";
24

35
export type PrismaCheckResult = {
46
isUsingPrisma: boolean;

starters/minimal/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
"@types/react-dom": "^19.1.2",
3131
"typescript": "^5.8.3",
3232
"vite": "^6.2.6",
33-
"wrangler": "^4.14.1"
33+
"wrangler": "^4.16.0"
3434
},
3535
"pnpm": {
3636
"onlyBuiltDependencies": [

starters/standard/.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,4 +63,6 @@ coverage
6363
*.tmp
6464
*.temp
6565

66-
.wrangler
66+
.wrangler
67+
68+
generated/

starters/standard/package.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@
2727
"prepare": "vibe-rules install cursor"
2828
},
2929
"dependencies": {
30-
"@prisma/adapter-d1": "~6.5.0",
31-
"@prisma/client": "~6.5.0",
30+
"@prisma/adapter-d1": "~6.8.2",
31+
"@prisma/client": "~6.8.2",
3232
"@simplewebauthn/browser": "^13.1.0",
3333
"@simplewebauthn/server": "^13.1.1",
3434
"rwsdk": "0.0.84-test.20250522031443"
@@ -37,11 +37,11 @@
3737
"@types/node": "^22.14.0",
3838
"@types/react": "^19.1.2",
3939
"@types/react-dom": "^19.1.2",
40-
"prisma": "~6.5.0",
40+
"prisma": "~6.8.2",
4141
"typescript": "^5.8.3",
4242
"vibe-rules": "^0.2.31",
4343
"vite": "^6.2.6",
44-
"wrangler": "^4.14.1"
44+
"wrangler": "^4.16.0"
4545
},
4646
"pnpm": {
4747
"onlyBuiltDependencies": [

starters/standard/prisma/schema.prisma

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,15 @@
55
// Try Prisma Accelerate: https://pris.ly/cli/accelerate-init
66

77
generator client {
8-
provider = "prisma-client-js"
9-
previewFeatures = ["driverAdapters"]
10-
output = "../node_modules/.prisma/client"
8+
provider = "prisma-client"
119
10+
runtime = "workerd"
11+
moduleFormat = "esm"
12+
generatedFileExtension = "ts"
13+
importFileExtension = "ts"
14+
15+
output = "../generated/prisma"
16+
previewFeatures = ["queryCompiler", "driverAdapters"]
1217
}
1318

1419
datasource db {

starters/standard/src/db.ts

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,23 @@
1-
import { PrismaClient } from "@prisma/client";
1+
import { PrismaClient } from "@generated/prisma";
22
import { PrismaD1 } from "@prisma/adapter-d1";
3-
import { env } from "cloudflare:workers";
3+
4+
export type * from "@generated/prisma";
45

56
export let db: PrismaClient;
67

7-
// context(justinvdm, 21-05-2025): For prisma-client-js generator or cases where
8+
// context(justinvdm, 21-05-2025):
9+
// We need to instantiate the client via a function rather that at the module level for several reasons:
10+
// * For prisma-client-js generator or cases where
811
// there are dynamic import to the prisma wasm modules, we need to make sure we
912
// are instantiating the prisma client later in the flow when the wasm would
10-
// have been initialized.
11-
export const setupDb = async () => {
13+
// have been initialized
14+
// * So that we can encapsulate workarounds, e.g. see `SELECT 1` workaround below
15+
export const setupDb = async (env: Env) => {
1216
db = new PrismaClient({
17+
// context(justinvdm, 21-05-2025): prisma-client generated type appears to
18+
// consider D1 adapter incompatible, though in runtime (dev and production)
19+
// it works
20+
// @ts-ignore
1321
adapter: new PrismaD1(env.DB),
1422
});
1523

starters/standard/src/scripts/seed.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
import { defineScript } from "rwsdk/worker";
22
import { db, setupDb } from "@/db";
3+
import { env } from "cloudflare:workers";
34

45
export default defineScript(async () => {
5-
await setupDb();
6+
await setupDb(env);
67

78
await db.$executeRawUnsafe(`\
89
DELETE FROM User;

starters/standard/src/worker.tsx

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,7 @@ import { setCommonHeaders } from "@/app/headers";
66
import { userRoutes } from "@/app/pages/user/routes";
77
import { sessions, setupSessionStore } from "./session/store";
88
import { Session } from "./session/durableObject";
9-
import { db, setupDb } from "./db";
10-
import type { User } from "@prisma/client";
9+
import { type User, db, setupDb } from "@/db";
1110
import { env } from "cloudflare:workers";
1211
export { SessionDurableObject } from "./session/durableObject";
1312

@@ -19,8 +18,8 @@ export type AppContext = {
1918
export default defineApp([
2019
setCommonHeaders(),
2120
async ({ ctx, request, headers }) => {
21+
await setupDb(env);
2222
setupSessionStore(env);
23-
await setupDb();
2423

2524
try {
2625
ctx.session = await sessions.load(request);

starters/standard/tsconfig.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
],
2222
"paths": {
2323
"@/*": ["./src/*"],
24-
".prisma/*": ["./node_modules/.prisma/*"]
24+
"@generated/*": ["./generated/*"]
2525
},
2626
/* Enable importing .json files */
2727
"resolveJsonModule": true,

0 commit comments

Comments
 (0)