Brakebein / prisma-generator-nestjs-dto

Generates NestJS DTO classes from Prisma Schema
Apache License 2.0
51 stars 28 forks source link

Support class transformer #26

Open erlanggadewa opened 1 year ago

erlanggadewa commented 1 year ago

Can you add class transformer custom annotation, because sometimes i need class transformer to transform input to other class type that i want

example :

input in prisma schema

/// @Type(() => Date)
birthDate                     DateTime                        @map("birth_date")

expectend output

import { Type } from 'class-transformer';

....... other code

  @ApiProperty({
    type: 'string',
    format: 'date-time',
  })
  @IsNotEmpty()
  @IsDateString()
  @Type(() => Date)
  birthDate: Date;

thank you

Brakebein commented 1 year ago

Are there also some other use cases (except for Date)?

Actually, I was thinking about to change the behavior of DateTime from Date to string, because it's a string that gets transferred and I got some troubles with TypeScript in my own projects resulting in casting types to string.

erlanggadewa commented 1 year ago

for now i'am only face use case for date.

warmansuganda commented 3 months ago

@Brakebein in my case I want to exclude the password from the response, e.g:

import { Exclude } from 'class-transformer';

export class UserEntity {
  id: number;
  firstName: string;
  lastName: string;

  @Exclude()
  password: string;

}

I found it from NestJs documentation here

Why is this necessary? because, I want to automatically exclude a password property from a user entity, if I do it manually I might miss it on certain endpoint/logic related to the user entity

I tried to change this line: https://github.com/Brakebein/prisma-generator-nestjs-dto/blob/ed67687ab04c2dd27bc6c4b8d14d00ea41329f49/src/generator/api-decorator.ts#L163

to be like this:

        return '@ApiHideProperty()\n@Exclude()\n';

assume when I use /// @ApiHideProperty to hide the field from the swagger I also append @Exclude to the entity

and I also change this line: https://github.com/Brakebein/prisma-generator-nestjs-dto/blob/ed67687ab04c2dd27bc6c4b8d14d00ea41329f49/src/generator/api-decorator.ts#L205

to be like this:

        const imp = [{ from: '@nestjs/swagger', destruct }];
        if (hasApiHideProperty) imp.push({ from: 'class-transformer', destruct: ['Exclude'] })
        return imp;

it's works for me.

import { ApiHideProperty, ApiProperty } from '@nestjs/swagger';
import { Exclude } from 'class-transformer';

export class User {
  ...
  @ApiHideProperty()
  @Exclude()
  password: string;
}
Brakebein commented 3 months ago

Interesting. I wasn't yet aware of this serialization and @Exclude decorator. The docs, however, say that you need to always return an instance of the class in order to work, and suggest to use this constructor:

constructor(partial: Partial<UserEntity>) {
  Object.assign(this, partial);
}

So, this contructor should also be generated? Or how do you create and return the instance?


In my own projects, I created a custom dto so far to exclude, for example, the password. But of course, it is not automatic.

export class UserWithoutPasswordDto extends OmitType(UserDto, ['password']) {}