具有一个没有最大项的给定数组,例如:
["DEFAULT", "NEW"], ["REVIEW", "OPEN"], ["DEFAULT", "IN PROGRESS"]
如何使用等类型表单生成查询
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")
以优雅的方式?我能做一些类似query.andWhere(query.orWhere())
的事情吗
到目前为止我的代码:
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]);
}
}
}
这里有一个应该有效的解决方案(CategoryEntity
是类实体(:
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();
有关带有WHERE
子句的查询生成器的更多信息,请参阅。