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