As my previous attempt (described in prisma/prisma#5273) to create a "prisma crud service" failed to provide a typesafe service, i decided to take another route.
I then thought about creating a custom generator that can be invoked via the schema.prisma
file. The generator, in turn, will now create a x.service.ts
file, for each model x
in the schema.prisma
file (dmmf). This service, in turn, contains all CRUD methods that are properly typed. Note that this service than can easily extended or injected to provide this feature.
The current approach looks like this:
FILE 1) This serves as main entry-point for the generator. It then simply calls the generate method from the other file
FILE 2) This provides some basic ideas, i.e., create the output folder and then calls the function to create all the files.
FILE 3)
This is a stub
file, i.e., it is the blueprint for all services. Note the __Class__
and __class__
placeholders, that are then replaced when loading this file
FILE 4)
This is a (simplified) version of the basic logic. It, for example, transforms all dmmf.models
to a respective x.service.ts
file, and saves them to the specified folder. Finally, it also creates a barrel file (i.e., the index.ts
) in this folder.
Simply add a new generator
to the schema.prisma
file, similar to this:
generator nestjs {
provider = "node ./node_modules/@trackyourhealth/nestjs-restful-generator"
output = "./your/custom/output/path"
serviceStub = "./path/to/custom/stub/from/root/directory.txt"
}
I have already published a version of this generator to npm, so feel free to try this sample! The package can be downloaded via @trackyourhealth/nestjs-restful-generator
!
I have tested this approach in a playground application and it certainly works. My workflow looks like this:
- install package
@trackyourhealth/nestjs-restful-generator
- add custom generator like described above and set a valid output path
- run
prisma generate
to create models and services - in a nestjs module create a new
user.service.ts
and let it extend the autogeneratedUserCrudService
from this generated package - Inject this service wherever needed
This approach certainly has limitations. For now, i see the following drawbacks (compared to my first approach described in the GitHub Issue)
I (as the package maintainer) will decide how the(this has been added inx.crud.service
will look like - and not you (as the developer using this package). I will try to upgrade the generator to allow for custom stub templates. Had no time for this yet :Dv 0.0.5
of this package)- The developer cannot adjust the code (i.e., change one particular method of one particular service) to better fit the application scenario. Once you re-run the generator your code will be overwritten! In order achieve this, you will need to
extend
andoverwrite
!
But most importantly:
- You will not reduce boilerplate code. You will just use a generator to do the dirty work for you.
Not sure if this the right approach, but lets discuss!
All the best and thanks for your time
Dear @FredrikBorgstrom , take a look at https://github.com/prisma-utils/prisma-utils and especially the
prisma-crud-generator
library.Respective library is a fully fledged crud generator, developed by me:
Take a look at the documentation here https://github.com/prisma-utils/prisma-utils/tree/main/libs/prisma-crud-generator
The library can be found and installed via npm:
https://www.npmjs.com/package/@prisma-utils/prisma-crud-generator
All the best