Company logo
  • Empleos
  • Bootcamp
  • Acerca de nosotros
  • Para profesionales
    • Inicio
    • Empleos
    • Cursos y retos
    • Preguntas
    • Profesores
    • Bootcamp
  • Para empresas
    • Inicio
    • Nuestro proceso
    • Planes
    • Pruebas
    • Nómina
    • Blog
    • Calculadora

0

142
Vistas
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.

9 months ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

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();
}
9 months ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar empleo Planes Nuestro proceso Comercial
Legal
Términos y condiciones Política de privacidad
© 2023 PeakU Inc. All Rights Reserved.