Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

97
Vistas
Best pratice of dynamically filtering REST API

Our team are developing a backend api using Node.JS, TypeScript and TypeORM (Oracle), we are relative new using this stack and have some doubts about best pratices about dynamically filtering large amount of data.

Considering a table with 20 records, we can just bring all the information to the frontend and filtering directly on browser, but considering a table with 1M records, I suppose you'll need a pagination feature and the user will not be able to filter directly on frontend.

The way that I'm tring to solve this problem, is creating an object with all filters generated by the user and send it on request body to the api, processing the information and returning the filtered data.

Considering that I'm using TypeORM queryBuilder, I created the function below to implements this logic:

interface IFilters {
    entity: string;
    key: string;
    value: string;
    type: string;
}
function filterQueryBuilder(
    query: SelectQueryBuilder<any>,
    filters: IFilters[]
): SelectQueryBuilder<any> {
    if (filters.length === 0) {
        return query;
    }

    filters.forEach((filter) => {
        if (filter.type === "in") {
            query.andWhere(`${filter.entity}.${filter.key} IN (:...values)`, {
                values: filter.value,
            });
        }

        if (filter.type === "like") {
            query.andWhere(`${filter.entity}.${filter.key} LIKE :value`, {
                value: `%${filter.value}%`,
            });
        }

        if (filter.type === "equal") {
            query.andWhere(`${filter.entity}.${filter.key} = :value`, {
                value: filter.value,
            });
        }
    });

    return query;
}

It works, but looks kinda dirt and the frontend will need to indicate the entity, so I think thats not the correct way.

Can someone indicates me a better way to do that? Or an article with a better solution, don't necessary needs to be on TypeORM.

Thanks in advance

about 4 years ago · Juan Pablo Isaza
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda