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

98
Visualizações
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 à 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