【问题标题】:WHERE condition based on arrays in typeorm基于 typeorm 中数组的 WHERE 条件
【发布时间】:2021-06-22 17:55:31
【问题描述】:

有一个没有最大项的给定数组,例如:

["DEFAULT", "NEW"], ["REVIEW", "OPEN"], ["DEFAULT", "IN PROGRESS"]

如何使用 typeorm 生成查询,例如

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]);
        }
    }
}

【问题讨论】:

    标签: sql typescript typeorm node.js-typeorm


    【解决方案1】:

    这是一个应该可行的解决方案(CategoryEntityclass 实体):

    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 子句的查询生成器 的更多信息,请参阅this

    【讨论】:

      猜你喜欢
      • 2021-11-05
      • 1970-01-01
      • 2014-07-21
      • 2021-01-16
      • 2017-03-17
      • 2019-12-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多