Generates crud resources (resolver,service,module,test-files) for a prisma, nestjs, graphql setup
- Working with prisma, nestjs and graphql is a great thing (imo).
- Nest.js resource generation is somewhat inefficient if you use types generated by prisma-nestjs-graphql (https://github.com/unlight/prisma-nestjs-graphql).
- This p (should be) able to auto-generate crud resolvers for your models defined in prisma.schema.
- Second, the p should register the new crud models in a module file that has to be provided by you.
- Install it:
npm i -D prisma-nestjs-graphql-resource-generator
- It needs you to also use https://www.npmjs.com/package/prisma-json-schema-generator as generator for your prisma setup.
- The p will look for a file called prisma/generated/json-schema.json which should be generated by the prisma-json-schema-generator
- Therefore the generator should look somewhat like that:
...
generator jsonSchema {
provider = "prisma-json-schema-generator"
output = "./generated/"
}
...
- After
npx prisma generate
you should decide on a folder you want to place the resource-files in, i will call minemodels
- Inside this folder a
models.module.ts
file has to be created including somewhat like:
import { Module } from '@nestjs/common';
@Module({
imports: [],
exports: [],
})
export class ModelsModule {}
- Execute
npx gm models/person
to generate only one resource, here the person resourcenpx gmall
to generate all prisma models that haven't been generated yet. Here the folder must be namedmodels
and the folder must be a direct subfolder ofsrc
.
- The result should look like:
src
|
|-models <== creation of this one is in your responsibility
|
|- models.module.ts <== creation of this one is in your responsibility, it's name must be "models.module.ts"
|
|- person <== first generated crud resource folder
|
|- person.module.ts
|- person.resolver.spec.ts
|- person.resolver.ts
|- person.service.spec.ts
|- person.service.ts
|
|- address <== second generated crud resource folder
|
|- address.module.ts
|- address.resolver.spec.ts
|- address.resolver.ts
|- address.service.spec.ts
|- address.service.ts
|
...
- Imports are added automatically (and naively) => if an import in a resolver file already exists before linting and the same one gets added by the linter => it will exist twice => error
- Just delete the second (generated) import.
- file names and source file paths are rigid and cannot be configured (atm)