Prisma using repository pattern
What do you think about using prisma with a repository pattern to encapsulate the logic?
4 Replies
@Nurul could you give your opinion about the subject? should i use or not, what's the pros and cons...
Hey @Rev 👋
Thank you for starting this post. You can definitely consider using Prisma with a repository pattern. Do you intend to use this in a Nest.js application? What is your use case?
This topic has been discussed before as well, did you had a chance to look at these discussions?
https://github.com/prisma/prisma/discussions/3929
https://github.com/prisma/prisma/issues/5273
Here is an example that might be useful as well:
https://github.com/LaunchWare/prisma-generator-vault
GitHub
Is a repository pattern with generics possible? · prisma prisma · D...
Is there a way to use generics for repository patterns using a base repository (example A), or should i do it just like example B, where i have to duplicate all the create, update, e.d. methods? Ex...
GitHub
Service Classes (e.g. for NestJS) · Issue #5273 · prisma/prisma
Dear Prisma Team, for my upcoming project, i would like to use Prisma, since it is ready to be used in production. I have been around for a year or so, but now finally use it in a concrete project....
GitHub
GitHub - LaunchWare/prisma-generator-vault: A Repository Pattern fo...
A Repository Pattern for Prisma. Contribute to LaunchWare/prisma-generator-vault development by creating an account on GitHub.
Yes, i'm using nestjs and i didn't know these discussions, i'm implementing myself case of base repository with fully typed returns for all uses in my white label using prisma + nestjs for fully generated modules using user CLI inputs after +/- 3 years working with prisma
I'm following this model
async findBy<T extends PrismaModelMapping<'User'>[CrudType.SELECT]>(data: {
where: PrismaModelMapping<'User'>[CrudType.WHERE];
select: T;
orderBy?: PrismaModelMapping<'User'>[CrudType.ORDER_BY];
}): Promise<GetModelPayload<'User', { select: T }>> {
const { where, select, orderBy } = data;
const user = await this.userRepository.findBy({
where,
select,
orderBy,
});
return user;
}
export interface PrismaModelMapping<T extends keyof Prisma.TypeMap['model']> {
select: Prisma.TypeMap['model'][T]['operations']['findFirst']['args']['select'];
}
export type GetModelPayload<
M extends Prisma.ModelName,
S > = M extends keyof TModelPayloadMap<S> ? TModelPayloadMap<S>[M] : never;
type TModelPayloadMap<S> = {
User: Prisma.UserGetPayload<S>;
};

And i using a generator handler ( prisma/generator-helper ) to generate typed GetPayload entities for GetModelPayload function to have full typed base entityThis is great! Thank you for sharing your use case and the way you are implementing it 🙌