Open erlanggadewa opened 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.
for now i'am only face use case for date.
@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;
}
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']) {}
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
expectend output
thank you