Lucas Munhoz
10/16/2020, 7:53 AMinclude argument to a wrapper class that I have around prisma and still get the dynamic types in the inferred return type?
The usecase is that I have a multi tenant application, and in the upper level of the api calls we have a tenantId that we want to inject in all where arguments.
Example:
public find = ({ where, ...args }: FindManyProductArgs) =>
this.prisma.product.findMany({
where: { ...where, tenantId: this.identity.tenantId, ...args }
})
So this allow to safely call this function passing arguments without having to worry it will access another tenant data. But the problem is that the caller of find, when it passes includes, it doesn't correcly add the dynamic types includes. This only works if I pass the include directly to prisma instance.
I can give more examples if is not clear, but my workaround to get this working is to do this: (is very ugly);
public find: PrismaClient["product"]["findMany"] = ({ where, ...args }) =>
this.prisma.product.findMany({
where: { ...where, tenantId: this.identity.tenantId, ...args } as any
}) as any
I have to add any everywhere to make TS happy.Nino Vrijman
10/16/2020, 10:33 AM<T extends FindMany{MODEL}Args> part is essential here.
export class UserRepository {
constructor(private readonly prisma: PrismaClient) {}
findMany<T extends FindManyUserArgs>(
args: Subset<T, FindManyUserArgs>
) {
return this.prisma.user.findMany(args);
}
}
Subset is imported from Prisma’s generated TypeScript client, as is FindManyUserArgs.Lucas Munhoz
10/16/2020, 11:21 AMNino Vrijman
10/16/2020, 12:09 PM