Lucas Pelegrino
10/17/2022, 6:07 PMimport { ApiProperty } from '@nestjs/swagger';
import { User } from '@prisma/client';
import { Exclude } from 'class-transformer';
export class UserEntity implements User {
...
@Exclude()
password: string;
...
// If I add this constructor and call `return new UserEntity(user) at the end of my services it works, when using TypeORM that wasn't necessary.
constructor(partial: Partial<UserEntity>) {
Object.assign(this, partial);
}
}
A simple service I wrote just to validade password exclusion
@Injectable()
export class GetUserService {
constructor(private prisma: PrismaService) {}
async get(id: string): Promise<UserEntity> {
const user = await this.prisma.user.findUnique({ where: { id } });
// Again, in order for the @Exclude option to work, I have to do this.
return new UserEntity(user);
}
}
NOTE: I have Nest global serialization enabled on the app.
My goal is to have password removed from the response without having to add the constructor in the entity, I did check Prisma Docs and read about excluding fields, but the options there are to open to errors just as this one I’m using.
Manually removing password using the select option also gives room to error, as I have to add all other fields every time, excluding only password.
So, anyone have other suggestions on how I can solve this, am I missing something?
Appreciate feedback.Simun Romic
10/17/2022, 6:28 PMClassSerializerInterceptor
set as a global interceptor in Nest app, right?
I see you are returning an instance of a class, so class serializer interceptor which uses class-transformer
should be able to process @Exclude
annotated properties on your class instance.Simun Romic
10/17/2022, 6:29 PMSimun Romic
10/18/2022, 5:52 PM