I am working on creating a Prisma Client Extension that can insert specific imports into a model.
const postExtention = Prisma.defineExtension((prisma) =>
prisma.$extends({
name: 'postExtention',
query: {
post: {
$allOperations({ operation, args, query }) {
if (
operation === 'createMany' ||
operation === 'updateMany' ||
operation === 'deleteMany' ||
operation === 'aggregate' ||
operation === 'count' ||
operation === 'groupBy'
)
return query(args)
return query({
...args,
include: {
...(args.include || {}),
user: true
}
})
}
}
}
})
)
// Usage
function getPost (id: string) {
return prisma.$extends(postExtention).post.findUnique({ where: { id } })
}
// NO user in PostOutput
type PostOutput = Awaited<ReturnType<typeof getPost>>
The issue I am facing is that the PostOutput
type is just a basic Prisma.Post
type, without the user
relation included.
I have tried experimenting with different methods within the extension, like using findUnique
instead of $allOperations
, but it did not resolve the problem as expected.
The only workaround I discovered was adjusting a generic of the findUnique
method:
prisma.$extends(postExtention).post.findUnique<{where: {id: string}, include: {user: boolean}}>({ where: { id } })
However, this approach is not ideal for me.
Can someone provide guidance on how I can modify the extension to ensure that it includes all the specified includes in the result type without having to add to the generic findUnique
? Any help would be greatly appreciated.