nestjsprismanestjs-swagger

How to expose prisma schema to swagger UI in NestJS?


NestJS exposes entity/DTO classes decorated with @ApiProperty to Swagger UI schemas. CLI Plugin automates it for all class files by default with suffixes ['.dto.ts', '.entity.ts']. Prisma schema, unfortunately, is not defined in terms of class thus cannot be decorated.

Without violating DRY principle by duplicating Prisma schema in another format, be it class or raw OpenAPI specs, how to expose it to swagger UI?


Solution

  • 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