Skip to content

feat: export Enhanced type to infer typeof enhanced PrismaClient #2010

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 24, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/runtime/res/enhance.d.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export { auth, enhance, type PrismaClient } from '.zenstack/enhance';
export { auth, enhance, type PrismaClient, type Enhanced } from '.zenstack/enhance';
2 changes: 1 addition & 1 deletion packages/runtime/src/enhance.d.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
// @ts-expect-error stub for re-exporting generated code
export { auth, enhance } from '.zenstack/enhance';
export { auth, enhance, type PrismaClient, type Enhanced } from '.zenstack/enhance';
13 changes: 13 additions & 0 deletions packages/schema/src/plugins/enhancer/enhance/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,19 @@ export function enhance(prisma: any, context?: EnhancementContext<${authTypePara
...options
}, context);
}

/**
* Infers the type of PrismaClient with ZenStack's enhancements.
* @example
* type EnhancedPrismaClient = Enhanced<typeof prisma>;
*/
export type Enhanced<Client> =
Client extends _PrismaClient<infer _ClientOptions, infer _U, infer ExtArgs> ? PrismaClient :
Client extends DynamicClientExtensionThis<infer _TypeMap, infer _TypeMapCb, infer ExtArgs${
hasClientOptions ? ', infer ClientOptions' : ''
}> ? DynamicClientExtensionThis<Prisma.TypeMap<ExtArgs>, Prisma.TypeMapCb, ExtArgs${
hasClientOptions ? ', ClientOptions' : ''
}> : Client;
`;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import { loadSchema } from '@zenstackhq/testtools';

describe('Enhancement typing tests', () => {
it('infers correct typing', async () => {
await loadSchema(
`
model User {
id Int @id @default(autoincrement())
posts Post[]
}

model Post {
id Int @id @default(autoincrement())
title String
author User @relation(fields: [authorId], references: [id])
authorId Int @default(auth().id)
}
`,
{
pushDb: false,
compile: true,
extraSourceFiles: [
{
name: 'main.ts',
content: `
import { PrismaClient } from '@prisma/client';
import type { Enhanced } from '.zenstack/enhance';

async function withoutClientExtension() {
const prisma = new PrismaClient();
const db = {} as any as Enhanced<typeof prisma>;
// note that "author" becomes optional
const r = await db.post.create({ data: { title: 'Post1' }});
console.log(r);
}

async function withClientExtension() {
const prisma = (new PrismaClient())
.$extends({
client: {
$log: (message: string) => {
console.log(message);
},
},
});
const db = {} as any as Enhanced<typeof prisma>;
// note that "author" becomes optional
const r = await db.post.create({ data: { title: 'Post1' }});
console.log(r);

// note that "$log" is preserved
db.$log('hello');
}
`,
},
],
}
);
});
});
Loading