Hey guys, is there anyway I can forward the `inclu...
# prisma-client
l
Hey guys, is there anyway I can forward the
include
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:
Copy code
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);
Copy code
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.
n
In one of the projects I work on we created a wrapper repository class for every Prisma model we have, in those repositories we did the following to get dynamic typing, I believe the
<T extends FindMany{MODEL}Args>
part is essential here.
Copy code
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
.
❤️ 3
l
@Nino Vrijman That did the trick! Thank you so much 😄
n
Glad I could help 😄