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

304
Visualizações
How to add a where clause dynamically to query which is generated using nestjs Query Builder?

I am working on an API for which the requirement from UI is based on the value of the search field I shall receive the filtered results. There are many search fields on UI.

Example code -

  async getRoomsByMember(active: boolean, email: string): Promise<any[]> {
      return await getRepository(Room)
      .createQueryBuilder('room')
      .innerJoinAndSelect('room.member', 'member')
      .where("room.active = :active", {active: active})
      .andWhere("member.email = :email", { email: email })
      .getMany();
  }

I shall be able to filter room members dynamically if values entered by a user on filter fields like - member phone number, city, state, country, and zip.

over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

You're almost there :-)

You can try something like this:

async getRoomsByMember(active: boolean, email: string): Promise<any[]> {
  const query = getRepository(Room)
    .createQueryBuilder('room')
    .innerJoinAndSelect('room.member', 'member')
    .where("room.active = :active", {active: active});
  // Keep adding your other fields like member phone number, city, state, country, and zip, like below
  if(email) {
    query.andWhere("member.email = :email", { email: email })
  }
  return query.getMany();
}
over 4 years ago · Santiago Trujillo 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