Skip to content

Commit 15b13f5

Browse files
authored
fix: regression with client extension typing (#2182)
1 parent 05e8485 commit 15b13f5

File tree

2 files changed

+128
-1
lines changed

2 files changed

+128
-1
lines changed

packages/schema/src/plugins/enhancer/enhance/index.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -243,8 +243,14 @@ export function enhance<DbClient extends object>(prisma: DbClient, context?: Enh
243243

244244
private createLogicalPrismaImports(prismaImport: string, prismaClientImport: string, target: string) {
245245
const prismaTargetImport = target === 'edge' ? `${prismaImport}/edge` : prismaImport;
246+
const runtimeLibraryImport = this.isNewPrismaClientGenerator
247+
? // new generator has these typed only in "@prisma/client"
248+
'@prisma/client/runtime/library'
249+
: // old generator has these types generated with the client
250+
`${prismaImport}/runtime/library`;
251+
246252
return `import { Prisma as _Prisma, PrismaClient as _PrismaClient } from '${prismaTargetImport}';
247-
import type { InternalArgs, DynamicClientExtensionThis } from '@prisma/client/runtime/library';
253+
import type { InternalArgs, DynamicClientExtensionThis } from '${runtimeLibraryImport}';
248254
import type * as _P from '${prismaClientImport}';
249255
import type { Prisma, PrismaClient } from '${prismaClientImport}';
250256
export type { PrismaClient };
Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
import { loadSchema } from '@zenstackhq/testtools';
2+
3+
describe('issue 2175', () => {
4+
it('regression standard generator', async () => {
5+
await loadSchema(
6+
`
7+
model User {
8+
id Int @id @default(autoincrement())
9+
email String @unique
10+
posts Post[]
11+
}
12+
13+
model Post {
14+
id Int @id @default(autoincrement())
15+
title String
16+
author User? @relation(fields: [authorId], references: [id])
17+
authorId Int? @default(auth().id)
18+
}
19+
20+
`,
21+
{
22+
compile: true,
23+
extraSourceFiles: [
24+
{
25+
name: 'main.ts',
26+
content: `
27+
import { PrismaClient } from "@prisma/client";
28+
import { enhance } from ".zenstack/enhance";
29+
30+
const prisma = new PrismaClient();
31+
const prismaExtended = prisma.$extends({
32+
model: {
33+
user: {
34+
async signUp(email: string) {
35+
return prisma.user.create({ data: { email } });
36+
},
37+
},
38+
},
39+
});
40+
41+
const dbExtended = enhance(prismaExtended);
42+
43+
async function main() {
44+
const newUser = await dbExtended.user.signUp("[email protected]");
45+
console.log(newUser);
46+
}
47+
48+
main();
49+
`,
50+
},
51+
],
52+
}
53+
);
54+
});
55+
56+
it('regression new generator', async () => {
57+
await loadSchema(
58+
`
59+
datasource db {
60+
provider = "sqlite"
61+
url = "file:./test.db"
62+
}
63+
64+
generator client {
65+
provider = "prisma-client"
66+
output = "../generated/prisma"
67+
moduleFormat = "cjs"
68+
}
69+
70+
model User {
71+
id Int @id @default(autoincrement())
72+
email String @unique
73+
posts Post[]
74+
}
75+
76+
model Post {
77+
id Int @id @default(autoincrement())
78+
title String
79+
author User? @relation(fields: [authorId], references: [id])
80+
authorId Int? @default(auth().id)
81+
}
82+
83+
`,
84+
{
85+
addPrelude: false,
86+
compile: true,
87+
extraSourceFiles: [
88+
{
89+
name: 'main.ts',
90+
content: `
91+
import { PrismaClient } from "./generated/prisma/client";
92+
import { enhance } from "./generated/zenstack/enhance";
93+
94+
const prisma = new PrismaClient();
95+
const prismaExtended = prisma.$extends({
96+
model: {
97+
user: {
98+
async signUp(email: string) {
99+
return prisma.user.create({ data: { email } });
100+
},
101+
},
102+
},
103+
});
104+
105+
const dbExtended = enhance(prismaExtended);
106+
107+
async function main() {
108+
const newUser = await dbExtended.user.signUp("[email protected]");
109+
console.log(newUser);
110+
}
111+
112+
main();
113+
`,
114+
},
115+
],
116+
output: './generated/zenstack',
117+
prismaLoadPath: './generated/prisma/client',
118+
}
119+
);
120+
});
121+
});

0 commit comments

Comments
 (0)