1.8.13 • Published 9 months ago

@theoxfordhealthcompany/prisma-generator-nestjs-graphql v1.8.13

Weekly downloads
-
License
MIT
Repository
github
Last release
9 months ago

prisma-generator-nestjs-graphql

Prisma generator used to create models, enums, and repositories for use with NestJS and GraphQL.

Generator Options

OptionTypeDefaultDescription
outputstring"../generated"Where to place the generated files relative to the schema file
genRepositoriesbooleantrueShould prisma repositories be generated
modelSuffixstring"GraphQL"Sting to append to each model and enum name

Schema Tags

For the repository generator to better match your work flow, there are several extra tags that can be added to your schema.

TagDescriptionExtra
@archivableChanges the delete behavior of the model to archive instead of delete in the repositoryThe fields archivedAt (date) and archivedBy (user.id) to be present on the model
@no-deleteRemoves the delete mutation from the repository
@no-paginationWon't paginate the result, will instead return the array of entities
@interfaceCreate the model with @InterfaceType, instead of @ObjectType
@skipOmits the field from the generated model
@nullableForces the field to be nullable in the generated model

Example Schema

An example schema can be found here, along with the generated outputs: Example Schema

Models

The generated models are designed to be used with NestJS and GraphQL. Each model is automatically tagged with @ObjectType() from @nestjs/graphql. When a field is a relation (e.g. A user's posts), the typescript type is set to optional, even if the field is required in the schema. This is because when you are working with an instance of the model, there is no guarantee that the relation will be populated.

Enums

The generated enums are designed to be used with NestJS and GraphQL. Each enum is registered with registerEnumType() from @nestjs/graphql.

Repositories

The generator option genRepositories must be omiited or set to true for repositories to be generated.

The generated repositories are designed to be used with NestJS and Prisma. Each repository is automatically tagged with @Injectable() from @nestjs/common. Each method can take a PrismaTransaction instance as an optional parameter, which will be used instead of the default transaction.

Each repository has the following methods: | Method | Return Type | Description | Available if | | -- | -- | -- | -- | | create | Promise\<T> | Creates a new instance of the model | Always | | createMany | Promise<{ count: number }> | Creates many instances of the model | Always | | getForRelation | "Raw prisma findUniqueOrThrow result" | Gets a single instance of the model, where the relation methods can be called on it | Always | | get | Promise\<T> | Gets a single instance of the model | Always | | exists | Promise\<boolean> | Checks if an instance of the model exists | Always | | getMany | Promise\<T[]> | Gets all instances of the model, with optional search parameters | @no-pagination is present | | getMany | Promise\<{ count: number, items: T[]}> | Gets all instances of the model, along with the number of matches, with optional search parameters | @no-pagination is not present | | count | Promise | Gets the number of instances of the model, with optional search parameters | Always | | update | Promise\<T> | Updates an instance of the model | Always | | updateMany | Promise\<{ count: number }> | Updates many instances of the model | Always | | delete | Promise\<T> | Deletes an instance of the model | @no-delete is not present | | deleteMany | Promise\<{ count: number }> | Deletes many instances of the model | @no-delete is not present | | archive | Promise\<T> | Archives an instance of the model | @archivable is present | | archiveMany | Promise\<{ count: number }> | Archives many instances of the model | @archivable is present | | restore | Promise\<T> | Restores an instance of the model | @archivable is present | | restoreMany | Promise\<{ count: number }> | Restores many instances of the model | @archivable is present |

A repository module is also created which provides and registers all of the repositories.

This generator was bootstraped using create-prisma-generator