folks! I am trying to use the Repository pattern with prisma, but I am facing type errors and wrong type inheritance
For this code:
class PostModel {
static async findMany(obj?: Prisma.PostFindManyArgs) {
return await db.post.findMany(obj);
}
}
await PostModel.findUnique({
where: {
id: params.id,
},
include: {
author: true,
}
})
I am getting
Post | null
Instead of:
Post & {author: Author} | null
I tried to referring the type but without success
static async findMany<T extends Prisma.PostFindManyArgs>(
obj: T
): Promise<Prisma.PrismaPromise<Prisma.Result<typeof db.post, T,'findMany'>>> {
return await db.post.findMany(obj);
}
I found a way of doing it
export class PostRepo {
static async findMany<T extends Prisma.PostFindManyArgs>(
args: Prisma.SelectSubset<T, Prisma.PostFindManyArgs>
): Promise<Array<Prisma.PostGetPayload<T>>> {
return await db.post.findMany(args);
}
}