Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

224
Visualizações
how to exclude required entities from a response in TypeORM with NestJS

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?

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

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

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda