Service Classes (e.g. for NestJS)
Problem
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. I really like what you've been working on - Prisma looks great and i can't wait to try it out. Problem In the context of my project i will be building a RESTful API with `NestJS`. Unfortunately, because of various reasons I cannot rely on GraphQL, for example, existing 3rd party clients are not able to "speak" (i.e., work with) GraphQL. In order to reduce boilerplate code in my `Service`s, i thought it may be a good idea to create some kind of _generic_ `CrudService` that offers basic functionality, like `create`, `update`, ... as some kind of wrapper around `prisma`. Having used `typeorm` in projects before, i thought that this may be an easy task. However, i quickly hit some roadblocks, because there are no `Repositories` in Prisma like in typeorm. The next idea was to simply _inject_ (i.e., pass) the corresponding `Delegate` to the `CrudService`. The closest i could get, however, is like this: [code block] and then create a concrete `UserService` like this: [code block] While this works, it has a few drawbacks: 1. all `modelDelegate` methods (i.e, `create()`, `update()`, `findMany()`, ...) are unknown, because the `delegate` is not known 2. the `UserService` with all its generics looks very ugly Suggested solution Regarding the drawbacks discussed earlier, i would sug
Unverified for your environment
Select your OS to check compatibility.
1 Fix
Solution: Service Classes (e.g. for NestJS)
Dear @timsuchanek , thank you very much for getting back to me with this issue. I have found a solution for my problem - although it is very dirty and hacky at the moment. But it works - and i guess that's all i can ask for, right now.. The solution would certainly be a bit prettier, if `Prisma` would auto-generate a few things for me. Creating `Repositories` would certainly help as well, however
Trust Score
8 verifications
- 1
Dear @timsuchanek , thank you very much for getting back to me with this issue.
I have found a solution for my problem - although it is very dirty and hacky at the moment. But it works - and i guess that's all i can ask for, right now..
- 2
The solution would certainly be a bit prettier, if `Prisma` would auto-generate
The solution would certainly be a bit prettier, if `Prisma` would auto-generate a few things for me. Creating `Repositories` would certainly help as well, however, not sure, what the best solution is.
- 3
However, for now, let me explain my current solution to the problem:
However, for now, let me explain my current solution to the problem:
- 4
Solution to create CRUD Services:
Solution to create CRUD Services:
Validation
Resolved in prisma/prisma GitHub issue #5273. Community reactions: 47 upvotes.
Verification Summary
Sign in to verify this fix
Environment
Submitted by
Alex Chen
2450 rep