Skip to content

Commit eb6fbca

Browse files
committed
feat: export Enhanced type to infer typeof enhanced PrismaClient
1 parent be57ad1 commit eb6fbca

File tree

4 files changed

+75
-2
lines changed

4 files changed

+75
-2
lines changed

packages/runtime/res/enhance.d.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
export { auth, enhance, type PrismaClient } from '.zenstack/enhance';
1+
export { auth, enhance, type PrismaClient, type Enhanced } from '.zenstack/enhance';

packages/runtime/src/enhance.d.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
// @ts-expect-error stub for re-exporting generated code
2-
export { auth, enhance } from '.zenstack/enhance';
2+
export { auth, enhance, type PrismaClient, type Enhanced } from '.zenstack/enhance';

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

+13
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,19 @@ export function enhance(prisma: any, context?: EnhancementContext<${authTypePara
266266
...options
267267
}, context);
268268
}
269+
270+
/**
271+
* Infers the type of PrismaClient with ZenStack's enhancements.
272+
* @example
273+
* type EnhancedPrismaClient = Enhanced<typeof prisma>;
274+
*/
275+
export type Enhanced<Client> =
276+
Client extends _PrismaClient<infer _ClientOptions, infer _U, infer ExtArgs> ? PrismaClient :
277+
Client extends DynamicClientExtensionThis<infer _TypeMap, infer _TypeMapCb, infer ExtArgs${
278+
hasClientOptions ? ', infer ClientOptions' : ''
279+
}> ? DynamicClientExtensionThis<Prisma.TypeMap<ExtArgs>, Prisma.TypeMapCb, ExtArgs${
280+
hasClientOptions ? ', ClientOptions' : ''
281+
}> : Client;
269282
`;
270283
}
271284

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
import { loadSchema } from '@zenstackhq/testtools';
2+
3+
describe('Enhancement typing tests', () => {
4+
it('infers correct typing', async () => {
5+
await loadSchema(
6+
`
7+
model User {
8+
id Int @id @default(autoincrement())
9+
posts Post[]
10+
}
11+
12+
model Post {
13+
id Int @id @default(autoincrement())
14+
title String
15+
author User @relation(fields: [authorId], references: [id])
16+
authorId Int @default(auth().id)
17+
}
18+
`,
19+
{
20+
pushDb: false,
21+
compile: true,
22+
extraSourceFiles: [
23+
{
24+
name: 'main.ts',
25+
content: `
26+
import { PrismaClient } from '@prisma/client';
27+
import type { Enhanced } from '.zenstack/enhance';
28+
29+
async function withoutClientExtension() {
30+
const prisma = new PrismaClient();
31+
const db = {} as any as Enhanced<typeof prisma>;
32+
// note that "author" becomes optional
33+
const r = await db.post.create({ data: { title: 'Post1' }});
34+
console.log(r);
35+
}
36+
37+
async function withClientExtension() {
38+
const prisma = (new PrismaClient())
39+
.$extends({
40+
client: {
41+
$log: (message: string) => {
42+
console.log(message);
43+
},
44+
},
45+
});
46+
const db = {} as any as Enhanced<typeof prisma>;
47+
// note that "author" becomes optional
48+
const r = await db.post.create({ data: { title: 'Post1' }});
49+
console.log(r);
50+
51+
// note that "$log" is preserved
52+
db.$log('hello');
53+
}
54+
`,
55+
},
56+
],
57+
}
58+
);
59+
});
60+
});

0 commit comments

Comments
 (0)