'How to use SUM() with getMany() in TypeORM?

I'm trying to write a group by query with TypeOrm/PostgreSQL ("pg": "^8.3.3", "typeorm": "^0.2.25").

The SQL query I need to be run is:

SELECT
    ad_id,
    COUNT (impression_id) AS impressions
FROM
    Ad_impressions
GROUP BY
    ad_id
ORDER BY
    impressions

I've read all the documentation and searched a lot online, but couldn't get it to work. This was my best try:

await getConnection()
  .getRepository(Ad_impressions)
  .createQueryBuilder('Ad_impressions')
  .select([
    'Ad_impressions.ad_id',
    'COUNT(Ad_impressions.impression_id) as count',
  ])
  .groupBy("Ad_impressions.ad_id")
  .orderBy('impressions', 'DESC')
  .getMany();


Solution 1:[1]

use getRawMany instead of getMany.

await getConnection().getRepository(Ad_impressions).createQueryBuilder('Ad_impressions').select([
'Ad_impressions.ad_id',
'COUNT(Ad_impressions.impression_id) as count',]).groupBy("Ad_impressions.ad_id").orderBy('impressions', 'DESC').getRawMany();

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Sohail Khn