Actually I was looking for the use case of the DTO in nestjs when you have the prisma generated types
As we can't use the prisma generated type in the swagger decorator, you will have to use DTOs to make it work, as prisma doesn't provide auto generated DTOs
It's in discussion and it's a big subject, I don't know if they planned to add this feature in the future version, but actually to make your swagger work you will have to pass by DTOs
You can find some libs to generate this DTOs for you using your prisma schema as https://www.npmjs.com/package/tsoa
I didn't reviewed it myself as I m still looking for the one that could work properly, so you will probably have to make some search too
But to answer your main question, no you can't use prisma generated type for now :')
Answer from Valentin on Stack OverflowActually I was looking for the use case of the DTO in nestjs when you have the prisma generated types
As we can't use the prisma generated type in the swagger decorator, you will have to use DTOs to make it work, as prisma doesn't provide auto generated DTOs
It's in discussion and it's a big subject, I don't know if they planned to add this feature in the future version, but actually to make your swagger work you will have to pass by DTOs
You can find some libs to generate this DTOs for you using your prisma schema as https://www.npmjs.com/package/tsoa
I didn't reviewed it myself as I m still looking for the one that could work properly, so you will probably have to make some search too
But to answer your main question, no you can't use prisma generated type for now :')
In present time I'd recommend prisma-class-generator lib which is also listed on Prisma's docs site for generators here. It does the same job, as it's description promises > Generates classes from your Prisma Schema that can be used as DTO, Swagger Response, TypeGraphQL and so on.
Note, my old go to choice wasprisma-generator-nestjs-dto which is also listed on Prisma's docs site.
However it was archived on Jan 5th, 2025 as it was looking for maintainers.
Actually, Lot of people asked Prisma about this, their team is aware of this "problem", but for what I could see they didn't plan to work on it at the moment
Until this feature come out, you will have to use a tool to generate your DTOs directly from your schema : https://github.com/robblovell/prisma-generator-nestjs
Then you can use the integrated swagger cli provided with nestjs : https://docs.nestjs.com/openapi/cli-plugin
It will add the decorator to your DTOs
I think this can be automatised with a script, so you can run it every time you update your prisma schema to sync it with your DTOs
Hope it's help
Create a DTO class that mirrors the Prisma.XXXXX structure
For example the code below mirror the create role structure in the prism/client file
export class CreateRoleDto implements Prisma.RoleCreateInput {
@ApiProperty({
example: 'ADMIN',
})
@IsString()
@IsNotEmpty()
name: string;
}
I can then add the dto to my controller as follows
async create(@Body() createRoleDto: CreateRoleDto) {
return this.rolesService.create(createRoleDto);
}
Hope this helps
» npm install @brakebein/prisma-generator-nestjs-dto
» npm install prisma-nestjs-graphql-swagger
» npm install @atlas/nestjs-prisma-gen