【问题标题】:Typeorm find options with order and whereTypeorm 查找带有顺序和位置的选项
【发布时间】:2021-05-05 22:27:29
【问题描述】:

我想通过表关系订购这个查找功能。

const [people, total] = await typePersonServiceInstance.find(
  {
    take,
    skip,
    where: (qb: any) => {
      qb.where('person.type IN (:...type)', { type });
      qb.andWhere('person.status IN (:...status)', { status });
      if (query.search) {
        qb.andWhere(new Brackets((subQb) => {
          subQb.where('name like :name', { name: `%${query.search}%` });
          subQb.orWhere('fantasyName like :fantasyName', { fantasyName: `%${query.search}%` });
          subQb.orWhere('person.city like :city', { city: `%${query.search}%` });
          subQb.orWhere('person.state like :state', { state: `%${query.search}%` });
          subQb.orWhere('person.id = :id', { id: query.search });
        }));
      }
    },
    order: {
      person: {
        status: 'ASC'
      }
    }
  },
);

我面临的问题是尝试通过person 表中的某些属性进行排序时,如果我这样做的话

order: {
  anyColumnFromTypePersonHere: 'ASC' | 'DESC'
}

它工作得很好,但如果我想按状态排序(这是一个人的属性)它不会工作

【问题讨论】:

    标签: node.js sorting entity relationship typeorm


    【解决方案1】:

    只需添加这一行:

    qb.addOrderBy('person.status', "ASC") ;
    

    【讨论】:

    • 您的答案可以通过额外的支持信息得到改进。请edit 添加更多详细信息,例如引用或文档,以便其他人可以确认您的答案是正确的。你可以找到更多关于如何写好答案的信息in the help center
    猜你喜欢
    • 1970-01-01
    • 2016-10-26
    • 1970-01-01
    • 1970-01-01
    • 2021-09-03
    • 2022-11-07
    • 1970-01-01
    • 2020-12-09
    • 1970-01-01
    相关资源
    最近更新 更多