I have an entity
@Column()
name: string;
@IsEmail()
email: string;
@Column({ select: false })
autogeneratedCode: string;
I'm getting name and string only in my GET request response which is expected.
But when I'm hit my POST Api with body, it is returning name, email, autogeneratedCode as well.
I need to hide autogeneratedCode in all CRUD responses.
Is there anyway to do that? or Am I missing something here?
You can use @Exclude() from 'class-transformer'
Example
import { Exclude } from 'class-transformer';
@Entity()
export class User {
@Column()
name: string;
@IsEmail()
email: string;
@Column({ select: false })
@Exclude()
autogeneratedCode: string;
constructor(entity: Partial<User>) {
Object.assign(this, entity);
}
}
Then you can use the constructor to create a new object excluding the @Exclude() properties.
export class UserService {
constructor(
@InjectRepository(User)
private userRepository: Repository<User>
) {}
public async createUser(user: User): Promise<User> {
return new User(
await this.userRepository.save(user)
);
}
}
NestJS Doc on Serialization
https://docs.nestjs.com/techniques/serialization#exclude-properties