Having a given array with no maximum items such as:
["DEFAULT", "NEW"], ["REVIEW", "OPEN"], ["DEFAULT", "IN PROGRESS"]
How can I generate a query using typeorm such as
WHERE (tsk.category = DEFAULT AND tsk.status = NEW) OR (tsk.category = REVIEW AND tsk.status = OPEN) OR (tsk.category = DEFAULT and tsk.status = "IN PROGRESS")
In an elegant way ? Could I do something like query.andWhere(query.orWhere())
My code so far:
const categories = await categoryService.getAllCategories();
const statusMap = [];
for (const category of categories) {
for (const state of category.states) {
const key = Object.keys(state)[0];
if (state[key].metaState === filter.meta_status) {
statusMap.push([category.name, key]);
}
}
}
Here is a solution that should work (CategoryEntity
is the class entity):
const categories: CategoryEntity[] = await getManager()
.createQueryBuilder(CategoryEntity, 'tsk')
.where(new Brackets((qb) => qb.where('tsk.category = DEFAULT').andWhere('tsk.status = NEW')))
.orWhere(new Brackets((qb) => qb.where('tsk.category = REVIEW').andWhere('tsk.status = OPEN')))
.orWhere(new Brackets((qb) => qb.where('tsk.category = DEFAULT').andWhere('tsk.status = "IN PROGRESS"')))
.getMany();
For more information about Query Builder with WHERE
clause see this.