Skip to content
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

fix: incorrect typing when Prisma client extension is used with logical client #1557

Merged
merged 2 commits into from
Jul 6, 2024
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
17 changes: 6 additions & 11 deletions packages/schema/src/plugins/enhancer/enhance/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,12 +147,7 @@ export function enhance<DbClient extends object>(prisma: DbClient, context?: Enh

private createLogicalPrismaImports(prismaImport: string, logicalPrismaClientDir: string) {
return `import { Prisma as _Prisma, PrismaClient as _PrismaClient } from '${prismaImport}';
import type {
InternalArgs,
TypeMapDef,
TypeMapCbDef,
DynamicClientExtensionThis,
} from '${prismaImport}/runtime/library';
import type { InternalArgs, DynamicClientExtensionThis } from '${prismaImport}/runtime/library';
import type * as _P from '${logicalPrismaClientDir}/index-fixed';
import type { Prisma, PrismaClient } from '${logicalPrismaClientDir}/index-fixed';
`;
Expand All @@ -173,11 +168,11 @@ export function enhance<ExtArgs extends Record<string, any> & InternalArgs>(
};

// overload for extended PrismaClient
export function enhance<TypeMap extends TypeMapDef, TypeMapCb extends TypeMapCbDef, ExtArgs extends Record<string, any> & InternalArgs${
hasClientOptions ? ', ClientOptions' : ''
}>(
prisma: DynamicClientExtensionThis<TypeMap, TypeMapCb, ExtArgs${hasClientOptions ? ', ClientOptions' : ''}>,
context?: EnhancementContext<${authTypeParam}>, options?: EnhancementOptions): DynamicClientExtensionThis<Prisma.TypeMap, Prisma.TypeMapCb, ExtArgs${
export function enhance<ExtArgs extends Record<string, any> & InternalArgs${hasClientOptions ? ', ClientOptions' : ''}>(
prisma: DynamicClientExtensionThis<_Prisma.TypeMap<ExtArgs>, _Prisma.TypeMapCb, ExtArgs${
hasClientOptions ? ', ClientOptions' : ''
}>,
context?: EnhancementContext<${authTypeParam}>, options?: EnhancementOptions): DynamicClientExtensionThis<Prisma.TypeMap<ExtArgs>, Prisma.TypeMapCb, ExtArgs${
hasClientOptions ? ', ClientOptions' : ''
}>${this.generatePermissionChecker ? ' & ModelCheckers' : ''};

Expand Down
66 changes: 66 additions & 0 deletions tests/regression/tests/issue-1493.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import { loadSchema } from '@zenstackhq/testtools';
describe('issue 1493', () => {
it('regression', async () => {
await loadSchema(
`
datasource db {
provider = 'sqlite'
url = 'file:./dev.db'
}

generator js {
provider = 'prisma-client-js'
}

plugin enhancer {
provider = '@core/enhancer'
output = './zenstack'
}

model User {
id Int @id
email String
posts Post[]
}

model Post {
id Int @id
title String
content String
author User @relation(fields: [authorId], references: [id])
authorId Int @default(auth().id)
}
`,
{
addPrelude: false,
compile: true,
getPrismaOnly: true,
extraSourceFiles: [
{
name: 'main.ts',
content: `
import { PrismaClient } from '@prisma/client';
import { enhance } from './zenstack/enhance';
const prisma = new PrismaClient().$extends({
result: {
user: {
gravatarUrl: {
needs: { email: true },
compute(user) {
return user.email + 'hash';
},
},
},
},
});

prisma.user.findFirst().then((user) => user?.gravatarUrl);
const db = enhance(prisma, undefined, { kinds: [] });
db.user.findFirst().then((user) => user?.gravatarUrl);
`,
},
],
}
);
});
});
Loading