Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use prisma generated types with swagger nestjs

I am using nestjs with prisma and swagger. When I use

import {PrismaGeneratedType} from '@prisma/client';

...

@ApiOkResponse({type: PrismaGeneratedType})

in a controller if gives error:

'PrismaGeneratedType' only refers to a type, but is being used as a value here

since prisma generate types and not class definitions. How can I use Prisma generated client (types) with swagger?

like image 811
Ahmed Nawaz Khan Avatar asked Feb 15 '26 09:02

Ahmed Nawaz Khan


2 Answers

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 :')

like image 132
Valentin Avatar answered Feb 20 '26 06:02

Valentin


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.

like image 45
ZR87 Avatar answered Feb 20 '26 07:02

ZR87



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!