Generate @nestjs/graphql entities and create/update inputs
pnpm add @jmondi/prisma-generator-nestjs-graphql
generator custom_generator {
provider = "generate-nestjs-models"
clientPath = "@prisma/client"
output = "../generated"
}
See the generated examples for sample output.
The special sauce here is the type-safe constructors.
constructor(model: UserConstructor) {
this.id = model.id ?? uuid();
this.email = model.email;
this.passwordHash = model.passwordHash ?? null;
this.tokenVersion = model.tokenVersion ?? 0;
this.lastLoginAt = model.lastLoginAt ?? null;
this.createdIP = model.createdIP;
this.createdAt = model.createdAt ?? new Date();
this.updatedAt = model.updatedAt ?? null;
this.posts = model.posts ?? null;
}
The following are all the default options, feel free to change them to your liking.
generator custom_generator {
provider = "generate-nestjs-models"
clientPath = "@prisma/client"
output = "../generated"
modelPrefix = "@prisma/client"
modelPrefix = ""
modelSuffix = ""
modelFileSuffix = ""
useAbstractModels = true
compileJs = false
withPaginatorInput = false
}
Create your own entities by extending the generated base entities.
export class User extends BaseUser {}
Extend your custom entities with additional fields.
export abstract class UserToken extends BaseUserToken {
@Field(() => User, { nullable: true })
user!: null | User;
constructor(props: UserTokenConstructor) {
props.user = props.user ? new User(props.user) : undefined;
super(props);
}
}
DEBUG=* pnpm prisma generate