@brakebein/prisma-generator-nestjs-dto

中文文档
1.30.0发布于 2026/06/18Apache-2.0TypeScriptCommonJS近 30 天下载 3.8万

Generates DTO and Entity classes from Prisma Schema for NestJS

原始使用文档
文档目录14 节
  1. What is it?
  2. Usage?
  3. Parameters
  4. Annotations
  5. Schema Object annotations
  6. Validation decorators
  7. Example
  8. Principles
  9. ConnectDTO
  10. CreateDTO
  11. UpdateDTO
  12. Entity
  13. DTO
  14. License

Prisma Generator NestJS DTO

Release License

  1. What is it?
  2. Usage
  3. Annotations
  4. Example
  5. Principles
  6. License

What is it?

Generates ConnectDTO, CreateDTO, UpdateDTO, DTO, and Entity classes for models in your Prisma Schema. This is useful if you want to leverage OpenAPI in your NestJS application - but also helps with GraphQL resources as well). NestJS Swagger requires input parameters in controllers to be described through classes because it leverages TypeScript's emitted metadata and Reflection to generate models/components for the OpenAPI spec. It does the same for response models/components on your controller methods.

These classes can also be used with the built-in ValidationPipe and Serialization.

This is a fork of @vegardit/prisma-generator-nestjs-dto and adds multiple features:

  • enhance fields with additional schema information, e.g., description, to generate a @ApiProperty() decorator (see Schema Object annotations)
  • optionally add validation decorators
  • support for composite types
  • control output format with additional flags flatResourceStructure, noDependencies, and outputType
  • optionally auto-format output with prettier

See CHANGELOG for all improvements and changes.

Usage?

npm install --save-dev @brakebein/prisma-generator-nestjs-dto
generator nestjsDto {
  provider                        = "prisma-generator-nestjs-dto"
  output                          = "../src/generated/nestjs-dto"
  prismaClientImportPath          = ""
  outputToNestJsResourceStructure = "false"
  flatResourceStructure           = "false"
  exportRelationModifierClasses   = "true"
  reExport                        = "false"
  generateFileTypes               = "all"
  createDtoPrefix                 = "Create"
  updateDtoPrefix                 = "Update"
  dtoSuffix                       = "Dto"
  entityPrefix                    = ""
  entitySuffix                    = ""
  classValidation                 = "false"
  fileNamingStyle                 = "camel"
  noDependencies                  = "false"
  outputType                      = "class"
  definiteAssignmentAssertion     = "false"
  requiredResponseApiProperty     = "true"
  jsdoc                           = "false"
  prettier                        = "false"
  wrapRelationsAsType             = "false"
  showDefaultValues               = "false"
}

Parameters

All parameters are optional.

Parameter = defaultDescription
output = "../src/generated/nestjs-dto"output path relative to your schema.prisma file
prismaClientImportPath = ""Override import path of Prisma client. Defaults to either @prisma/client or a relative path based on your generator client output config (see #73).
outputToNestJsResourceStructure = "false"writes dtos and entities to subfolders aligned with NestJS CRUD generator. Resource module name is derived from lower-cased model name in schema.prisma
flatResourceStructure = "false"If outputToNestJsResourceStructure is true, subfolders dtos and entities are created within the resource folder. Setting this to true will flatten the hierarchy.
exportRelationModifierClasses = "true"Should extra classes generated for relationship field operations on DTOs be exported?
reExport = "false"Should an index.ts be created for every folder?
generateFileTypes = "all"all: generate both DTO and Entity files, dto: generate only DTO files, entity: generate only Entity files (not possible in combination with complex types)
createDtoPrefix = "Create"phrase to prefix every CreateDTO class with
updateDtoPrefix = "Update"phrase to prefix every UpdateDTO class with
dtoSuffix = "Dto"phrase to suffix every CreateDTO and UpdateDTO class with
entityPrefix = ""phrase to prefix every Entity class with
entitySuffix = ""phrase to suffix every Entity class with
fileNamingStyle = "camel"How to name generated files. Valid choices are "camel", "pascal", "kebab" and "snake".
classValidation = "false"Add validation decorators from class-validator. Not compatible with noDependencies = "true" and outputType = "interface".
noDependencies = "false"Any imports and decorators that are specific to NestJS and Prisma are omitted, such that there are no references to external dependencies. This is useful if you want to generate appropriate DTOs for the frontend.
outputType = "class"Output the DTOs as class or as interface. interface should only be used to generate DTOs for the frontend.
definiteAssignmentAssertion = "false"Add a definite assignment assertion operator ! to required fields, which is required if strict and/or strictPropertyInitialization is set true in your tsconfig.json's compilerOptions.
requiredResponseApiProperty = "true"If false, add @ApiRequired({ required: false }) to response DTO properties. Otherwise, use required defaults always to true unless field is optional.
jsdoc = "false"Add basic JSDoc section with description and default value.
prettier = "false"Stylize output files with prettier.
wrapRelationsAsType = "false"If true, import relations as types to solve circular reference issues with SWC (see #39).
showDefaultValues = "false"If true, makes fields with @default attribute visible by default, instead of using @DtoCreateOptional and @DtoUpdateOptional each time (see #51).

Annotations

Annotations provide additional information to help this generator understand your intentions. They are applied as tripple slash comments to a field node in your Prisma Schema. You can apply multiple annotations to the same field.

model Post {
  /// @DtoCreateOptional
  /// @DtoUpdateHidden
  createdAt   DateTime @default(now())
  /// @DtoOverrideType(DurationLike, luxon)
  timeUntilExpires Json?
}
AnnotationDescription
@DtoReadOnlyomits field in CreateDTO and UpdateDTO
@DtoCreateHiddenomits field in CreateDTO
@DtoUpdateHiddenomits field in UpdateDTO
@DtoEntityHiddenomits field in Entity
@DtoConnectHiddenomits field in ConnectDto (applies to @id and @unique fields)
@DtoApiHiddenadds @ApiHideProperty decorator to hide field in documentation, class validation remains untouched
@DtoCreateOptionaladds field optionally to CreateDTO - useful for fields that would otherwise be omitted (e.g. @id, @updatedAt)
@DtoCreateRequiredmarks field required in CreateDTO that is otherwise optional - also adds field that would otherwise be omitted (e.g. @id, @updatedAt)
@DtoUpdateOptionaladds field optionally to UpdateDTO - useful for fields that would otherwise be omitted (e.g. @id, @updatedAt)
@DtoUpdateRequiredmarks field required in UpdateDTO that is otherwise optional - also adds field that would otherwise be omitted (e.g. @id, @updatedAt)
@DtoRelationRequiredmarks relation required in Entity although it's optional in PrismaSchema - useful when you don't want (SQL) ON DELETE CASCADE behavior - but your logical data schema sees this relation as required (Note: becomes obsolete once referentialActions are released and stable)
@DtoRelationCanCreateOnCreateadds create option on a relation field in the generated CreateDTO - useful when you want to allow to create related model instances
@DtoRelationCanConnectOnCreateadds connect option on a relation field in the generated CreateDTO - useful when you want/need to connect to an existing related instance
@DtoRelationCanCreateOnUpdateadds create option on a relation field in the generated UpdateDTO - useful when you want to allow to create related model instances
@DtoRelationCanConnectOnUpdateadds connect option on a relation field in the generated UpdateDTO - useful when you want/need to connect to an existing related instance
@DtoRelationCanUpdateOnUpdateadds update option on a relation field in the generated UpdateDTO - useful when you want/need to update an existing related instance, only applicable to "-to-one" relations
@DtoRelationCanDisconnectOnUpdateadds disconnect option on a relation field in the generated UpdateDTO - useful when you want/need to disconnect to an existing related instance
@DtoRelationIncludeIdinclude ID of a relation field that is otherwise omitted (use instead of CanCreate/CanConnect annotations, if you just want to pass the IDs)
@DtoTypeFullUpdatein the generated UpdateDTO, use the CreateDTO of the composite type to enforce a complete replacement the old values (see #2)
@DtoOverrideType(...)in all the generated entities, forces a provided type for this field. Especially useful if you need to cast Json fields at read/write. Note, you must provide at least one and may provide up to 3 arguments in the (...) with this annotation - the first is the name of the type to force for this field, the second and third can be used to add an import for that type at the top of the entity file. For example:
  • @DtoOverrideType(MyType) will cast the field as MyType but add no import
  • @DtoOverrideType(MyType, some-package) will cast the field as MyType and add import {MyType} from "some-package"
  • @DtoOverrideType(MyType, ../types, default) will cast and add import MyType from "../types"
  • @DtoOverrideType(MyTypeInterface, ../types, MyType) will cast as MyTypeInterface and add import {MyType as MyTypeInterface} from "../types"
@DtoOverrideApiPropertyType(...)same as @DtoOverrideType but for @ApiProperty() type property
@DtoCreateValidateIf(...)adds @ValidateIf(...) decorator for field in CreateDTO (for conditional validation by class-validator)
@DtoUpdateValidateIf(...)adds @ValidateIf(...) decorator for field in UpdateDTO (for conditional validation by class-validator)

Schema Object annotations

With @nestjs/swagger, you can generate an API specification from code. Routes, request bodies, query parameters, etc., are annotated with special decorators. Properties can be annotated with the @ApiProperty() decorator to add schema object information. They are partially added at runtime, which will then include type, nullable, etc. But additional information, such as description, need to be added manually.

If using a generator like this, any custom @ApiProperty() annotation would be overridden when updating the DTOs. To enhance a field with additional schema information, add the schema property prefixed with @ to the comment section above the field.

Currently, following schema properties are supported:

  • description
  • minimum
  • maximum
  • exclusiveMinimum
  • exclusiveMaximum
  • minLength
  • maxLength
  • minItems
  • maxItems
  • example

Additionally, special data types are inferred and annotated as well:

  • Int: { type: 'integer', format: 'int32' }
  • BigInt: { type: 'integer', format: 'int64' }
  • Float: { type: 'number', format: 'float' }
  • Decimal: { type: 'number', format: 'double' }
  • DateTime: { type: 'string', format: 'date-time' }

Example

This example using @description and @minimum tags

/// @description Number of reviews
/// @minimum 9
reviewCount Int     @default(0)

will generate @ApiProperty() decorator with description and minimum as properties as well as type and format to specify the data type.

@ApiProperty({
  description: 'Number of reviews',
  minimum: 9,
  type: 'integer',
  format: 'int32',
})
reviewCount: number;

Default values are added in CreateDTO and UpdateDTO. However, a field with a @default() attribute is hidden by default in CreateDTO and UpdateDTO, hence requires @DtoCreateOptional and/or @DtoUpdateOptional to be present.

Validation decorators

If classValidation = "true", the generator will add validation decorators from class-validator to each field of CreateDTO and UpdateDTO that can then be used in combination with the NestJS ValidationPipe (see NestJS Auto-validation).

Some decorators will be inferred from the field's attributes. If the field is optional, it will add @IsOptional(), otherwise @IsNotEmpty(). If the field is a list, it will add @IsArray(). Type validators are inferred from the field's type:

  • String → @IsString()
  • Boolean → @IsBoolean()
  • Int → @IsInt()
  • BigInt → @IsInt()
  • Float: → @IsNumber()
  • Decimal: → @IsDecimal()
  • DateTime → @IsDateString()

All remaining validation decorators can be added in the comment/documentation section above the field. The parentheses can be omitted if not passing a value.

Example

Prisma Schema

/// @Contains('Product')
name        String   @db.VarChar(255)
reviewCount Int      @default(0)
/// @ArrayNotEmpty
tags        String[]
score       Float?

Generated output

@IsNotEmpty()
@IsString()
@Contains('Product')
name: string;
@IsOptional()
@IsInt()
reviewCount?: number;
@IsNotEmpty()
@IsArray()
@ArrayNotEmpty()
tags: string[];
@IsOptional()
@IsNumber()
score?: number;

Example

Prisma Schema
generator nestjsDto {
  provider = "prisma-generator-nestjs-dto"
  output = "../src"
  outputToNestJsResourceStructure = "true"
}

model Question {
  id          String @id @default(dbgenerated("gen_random_uuid()")) @db.Uuid
  /// @DtoReadOnly
  createdAt   DateTime @default(now())
  /// @DtoRelationRequired
  createdBy   User? @relation("CreatedQuestions", fields: [createdById], references: [id])
  createdById String? @db.Uuid
  updatedAt   DateTime @updatedAt
  /// @DtoRelationRequired
  updatedBy   User? @relation("UpdatedQuestions", fields: [updatedById], references: [id])
  updatedById String? @db.Uuid

  /// @DtoRelationRequired
  /// @DtoRelationCanConnectOnCreate
  category   Category? @relation(fields: [categoryId], references: [id])
  categoryId String?   @db.Uuid

  /// @DtoCreateOptional
  /// @DtoRelationCanCreateOnCreate
  /// @DtoRelationCanConnectOnCreate
  /// @DtoRelationCanCreateOnUpdate
  /// @DtoRelationCanConnectOnUpdate
  tags Tag[]

  title     String
  content   String
  responses Response[]
}
Generated results
// src/question/dto/connect-question.dto.ts
export class ConnectQuestionDto {
  id: string;
}
// src/question/dto/create-question.dto.ts
import { ApiExtraModels } from '@nestjs/swagger';
import { ConnectCategoryDto } from '../../category/dto/connect-category.dto';
import { CreateTagDto } from '../../tag/dto/create-tag.dto';
import { ConnectTagDto } from '../../tag/dto/connect-tag.dto';

export class CreateQuestionCategoryRelationInputDto {
  connect: ConnectCategoryDto;
}
export class CreateQuestionTagsRelationInputDto {
  create?: CreateTagDto[];
  connect?: ConnectTagDto[];
}

@ApiExtraModels(
  ConnectCategoryDto,
  CreateQuestionCategoryRelationInputDto,
  CreateTagDto,
  ConnectTagDto,
  CreateQuestionTagsRelationInputDto,
)
export class CreateQuestionDto {
  category: CreateQuestionCategoryRelationInputDto;
  tags?: CreateQuestionTagsRelationInputDto;
  title: string;
  content: string;
}
// src/question/dto/update-question.dto.ts
import { ApiExtraModels } from '@nestjs/swagger';
import { CreateTagDto } from '../../tag/dto/create-tag.dto';
import { ConnectTagDto } from '../../tag/dto/connect-tag.dto';

export class UpdateQuestionTagsRelationInputDto {
  create?: CreateTagDto[];
  connect?: ConnectTagDto[];
}

@ApiExtraModels(CreateTagDto, ConnectTagDto, UpdateQuestionTagsRelationInputDto)
export class UpdateQuestionDto {
  tags?: UpdateQuestionTagsRelationInputDto;
  title?: string;
  content?: string;
}
// src/question/entities/question.entity.ts
import { User } from '../../user/entities/user.entity';
import { Category } from '../../category/entities/category.entity';
import { Tag } from '../../tag/entities/tag.entity';
import { Response } from '../../response/entities/response.entity';

export class Question {
  id: string;
  createdAt: Date;
  createdBy?: User;
  createdById: string;
  updatedAt: Date;
  updatedBy?: User;
  updatedById: string;
  category?: Category;
  categoryId: string;
  tags?: Tag[];
  title: string;
  content: string;
  responses?: Response[];
}

Principles

Generally we read field properties from the DMMF.Field information provided by @prisma/generator-helper. Since a few scenarios don't become quite clear from that, we also check for additional annotations (or decorators) in a field's documentation (that is anything provided as a tripple slash comments for that field in your prisma.schema).

Initially, we wanted DTO classes to implement Prisma.<ModelName><(Create|Update)>Input but that turned out to conflict with required relation fields.

ConnectDTO

This kind of DTO represents the structure of input-data to expect from 'outside' (e.g. REST API consumer) when attempting to connect to a model through a relation field.

A Models ConnectDTO class is composed from a unique'd list of isId and isUnique scalar fields. If the ConnectDTO class has exactly one property, the property is marked as required. If there are more than one properties, all properties are optional (since setting a single one of them is already sufficient for a unique query) - you must however specify at least one property.

ConnectDTOs are used for relation fields in CreateDTOs and UpdateDTOs.

CreateDTO

This kind of DTO represents the structure of input-data to expect from 'outside' (e.g. REST API consumer) when attempting to create a new instance of a Model. Typically the requirements for database schema differ from what we want to allow users to do. As an example (and this is the opinion represented in this generator), we don't think that relation scalar fields should be exposed to users for create, update, or delete activities (btw. TypeScript types generated in PrismaClient exclude these fields as well). If however, your schema defines a required relation, creating an entity of that Model would become quite difficult without the relation data. In some cases you can derive information regarding related instances from context (e.g. HTTP path on the rest endpoint /api/post/:postid/comment to create a Comment with relation to a Post). For all other cases, we have the

  • @DtoRelationCanCreateOnCreate
  • @DtoRelationCanConnectOnCreate
  • @DtoRelationCanCreateOnUpdate
  • @DtoRelationCanConnectOnUpdate

annotations that generate corresponding input properties on CreateDTO and UpdateDTO (optional or required - depending on the nature of the relation).

When generating a Models CreateDTO class, field that meet any of the following conditions are omitted (order matters):

  • isReadOnly OR is annotated with @DtoReadOnly (Note: this apparently includes relation scalar fields)
  • field represents a relation (field.kind === 'object') and is not annotated with @DtoRelationCanCreateOnCreate or @DtoRelationCanConnectOnCreate
  • field is a relation scalar
  • field is not annotated with @DtoCreateOptional AND
    • isId && hasDefaultValue (id fields are not supposed to be provided by the user)
    • isUpdatedAt (Prisma will inject value)
    • isRequired && hasDefaultValue (for schema-required fields that fallback to a default value when empty. Think: createdAt timestamps with @default(now()) (see now()))

UpdateDTO

When generating a Models UpdateDTO class, field that meet any of the following conditions are omitted (order matters):

  • field is annotated with @DtoUpdateOptional
  • isReadOnly OR is annotated with @DtoReadOnly (Note: this apparently includes relation scalar fields)
  • isId (id fields are not supposed to be updated by the user)
  • field represents a relation (field.kind === 'object') and is not annotated with @DtoRelationCanCreateOnUpdate or @DtoRelationCanConnectOnUpdate
  • field is a relation scalar
  • field is not annotated with @DtoUpdateOptional AND
    • isId (id fields are not supposed to be updated by the user)
    • isUpdatedAt (Prisma will inject value)
    • isRequired && hasDefaultValue (for schema-required fields that fallback to a default value when empty. Think: createdAt timestamps with @default(now()) (see now()))

Entity

When generating a Models Entity class, only fields annotated with @DtoEntityHidden are omitted. All other fields are only manipulated regarding their isRequired and isNullable flags.

By default, every scalar field in an entity is required meaning it doesn't get the TypeScript "optional member flag" ? next to it's name. Fields that are marked as optional in PrismaSchema are treated as nullable - meaning their TypeScript type is a union of field.type and null (e.g. string | null).

Relation and relation scalar fields are treated differently. If you don't specifically include a relation in your query, those fields will not exist in the response.

  • every relation field is always optional (isRequired = false)
  • relations are nullable except when
    • the relation field is a one-to-many or many-to-many (i.e. list) type (would return empty array if no related records found)
    • the relation was originally flagged as required (isRequired = true)
    • the relation field is annotated with @DtoRelationRequired (do this when you mark a relation as optional in PrismaSchema because you don't want (SQL) ON DELETE CASCADE behavior - but your logical data schema sees this relation as required)
  • default values are not added to the @ApiProperty() decorator

DTO

The plain DTO class is almost the same as Entity with the difference that all relation fields are omitted. This is useful if your response is the plain entity without any (optional) relations.

License

All files are released under the Apache License 2.0.

版本与发布记录

@brakebein/prisma-generator-nestjs-dto 共发布 71 个版本,首次发布于 2022/03/16;过去 12 个月发布了 3 个版本。

版本发布日期距今
1.30.0latest2026/06/184 个月前
1.29.02025/12/1310 个月前
1.29.0-beta0beta2025/12/0111 个月前
1.28.12025/06/051.4 年前
1.28.02025/05/181.5 年前
1.27.02025/03/281.6 年前

关于 @brakebein/prisma-generator-nestjs-dto 的常见问题

@brakebein/prisma-generator-nestjs-dto 是什么?

Generates DTO and Entity classes from Prisma Schema for NestJS

如何安装 @brakebein/prisma-generator-nestjs-dto?

在项目目录运行 npm install @brakebein/prisma-generator-nestjs-dto,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:@prisma/client(^5.16.0 || ^6.0.0 || ^7.0.0)、prisma(^5.16.0 || ^6.0.0 || ^7.0.0)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。它还提供命令行工具 prisma-generator-nestjs-dto,可以用 npx prisma-generator-nestjs-dto 直接运行。

@brakebein/prisma-generator-nestjs-dto 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@brakebein/prisma-generator-nestjs-dto 支持 ESM 还是 CommonJS?

以 CommonJS 形式发布,可以直接 require();在 ES Module 项目中也可以用 import 引入(Node.js 会把 module.exports 作为默认导出)。

@brakebein/prisma-generator-nestjs-dto 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >=14。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

@brakebein/prisma-generator-nestjs-dto 可以商用吗?

@brakebein/prisma-generator-nestjs-dto 使用 Apache-2.0 许可。宽松许可:允许商业使用、修改和再分发,并附带明确的专利授权;分发时需保留许可与 NOTICE 文件,修改过的文件要注明改动。以上为概括,不构成法律意见,请以许可原文为准。

@brakebein/prisma-generator-nestjs-dto 的下载量有多少?

根据 npm 官方下载统计,@brakebein/prisma-generator-nestjs-dto 在 2026/10/09 – 2026/11/07 这 30 天内被下载 37,737 次,日均约 1,258 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@brakebein/prisma-generator-nestjs-dto 还在维护吗?

最新版本 1.30.0 发布于 2026/06/18(4 个月前);至今共发布 71 个版本,其中过去 12 个月发布了 3 个;首次发布于 2022/03/16。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。