Skip to content

Generates crud resources (resolver,service,module,test-files) for a prisma, nestjs, graphql setup

License

Notifications You must be signed in to change notification settings

Grishezz/prisma-nestjs-graphql-resource-generator

 
 

Repository files navigation

prisma-nestjs-graphql-resource-generator

Generates crud resources (resolver,service,module,test-files) for a prisma, nestjs, graphql setup

Goal of the npm package (p):

  • 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.

How to use the p:

  • 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 mine models
  • 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 resource
    • npx gmall to generate all prisma models that haven't been generated yet. Here the folder must be named models and the folder must be a direct subfolder of src.
  • 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
     |

 ...

Known issues:

  • 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)

About

Generates crud resources (resolver,service,module,test-files) for a prisma, nestjs, graphql setup

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • TypeScript 97.9%
  • JavaScript 2.1%