postgresql 如何在TypeOrm中进行聚合

2cmtqfgy  于 2022-12-23  发布在  PostgreSQL
关注(0)|答案(1)|浏览(129)

我需要获取兴趣列表及其相关数据,并计算订阅兴趣的客户数量
我的利益和客户是多对一的关系,这是唯一可能做的

const list = await this.interestRepository
        .createQueryBuilder('interest')
        .leftJoinAndSelect("interest.translations", "translations")
        .leftJoinAndSelect("interest.customers", "customers")
        .getMany()

这就是结果

list [
  Interest {
    id: 'YJnu_8bpzMSrFiztDl1zH',
    creationDate: 2022-12-19T16:49:55.090Z,
    countryCode: 'LTU',
    rank: 2,
    status: 'active',
    translations: [ [InterestTranslation], [InterestTranslation] ],
    customers: [ [Customer] ]
  },
  Interest {
    id: '-h1UPpOSkw4TqcTc7F5Ej',
    creationDate: 2022-12-19T16:57:47.718Z,
    countryCode: 'USA',
    rank: 1,
    status: 'active',
    translations: [ [InterestTranslation], [InterestTranslation] ],
    customers: []
  }
]

但我想计算的客户,而不是得到所有的人,因为它可能是成千上万的人,这将影响请求的持续时间。

3mpgtkmj

3mpgtkmj1#

如果我没理解错的话你需要把这个

.loadRelationCountAndMap('interest.customers', 'interest.customers')

代替

.leftJoinAndSelect("interest.customers", "customers")

相关问题