如何使用"postgresql"在"typeorm"中选择"array



我在typescript项目中使用typeorm连接到postresql 11。我在sql中有以下查询,它给了我预期的结果:

select "customerUuid", array_agg("siteUuid") from "SiteCustomer" where "customerUuid" in ('id1', 'id2') group by "customerUuid";

但当我使用typeorm时,它总是返回空数组:

queryRunner.manager.getRepository(SiteCustomer).
.createQueryBuilder()
.select('"customerUuid", array_agg("siteUuid")')
.where('"customerUuid" IN (:customerUuids)', { customerUuids: customerUuids})
.groupBy('"customerUuid"')
.getMany();

如何在postgresql中使typeormarray_agg协同工作?

你可以做这样的事情-

let baseQuery = queryRunner.manager.getRepository(SiteCustomer)
.createQueryBuilder()
.select([
'"customerUuid" as "customerUuid"',
// It's better to specify an alias explicitly, because the default one 
// is implicit and may be unexpected
'array_agg("siteUuid") as "aggregatedSiteUuids"', 
])
.where('"customerUuid" IN (:customerUuids)', { customerUuids: customerUuids})
.groupBy('"customerUuid"')

但是使用-

baseQuery.getRawMany()

以防您没有选择实体。

最新更新