Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

101
Views
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
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!