Query builder with getRawMany returns count column as string

See original GitHub issue

Issue type:

[ ] question [ ] bug report [* ] feature request [ ] documentation issue

Database system/driver:

[ ] cordova [ ] mongodb [ ] mssql [ *] mysql / mariadb [ ] oracle [ ] postgres [ ] cockroachdb [ ] sqlite [ ] sqljs [ ] react-native [ ] expo

TypeORM version:

[* ] latest [ ] @next [ ] 0.x.x (or put your version here)

Steps to reproduce or a small repository showing the problem:

Ussing query builder with getRawMany returns aggregated count column as a string.

      const countByWorker = await Ticket.createQueryBuilder()
        .select(['worker_id', 'status', 'count(*) as count'])
        .groupBy('worker_id, status')
        .getRawMany();

Is retuning this:

[
  RowDataPacket { worker_id: 0, status: 'OK', count: '27217' },
  RowDataPacket { worker_id: 0, status: 'TOEXTRACT', count: '743' },
  RowDataPacket { worker_id: 1, status: 'KO', count: '1' },
  RowDataPacket { worker_id: 1, status: 'OK', count: '2' }
]

It would be expected ‘count’ column to be typeof number.

Thanks Javier

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:41
  • Comments:15 (2 by maintainers)

github_iconTop GitHub Comments

19reactions
lukzardcommented, Feb 25, 2021

Had the same “issue”. Which isn’t an issue of typeorm. I assume that count(*) returns a data type specific to the database that is not directly convertible to javascript’s numeric data type. Example if the db return bigint, then javascript will mark it as string.

Simple solution is to cast to integer or int in SQL context. I.e., in your example:

const countByWorker = await Ticket.createQueryBuilder() .select(['worker_id', 'status', 'count(*)::int as count']) .groupBy('worker_id, status') .getRawMany();

::int is a short form of cast in postgres; adapt whatever is the way to cast in your db

2reactions
hengkxcommented, Dec 2, 2020

any progress?

Read more comments on GitHub >

github_iconTop Results From Across the Web

TypeORM: How to add COUNT field when using getMany()
Here is an example to count articles for each users using the TypeOrm QueryBuilder: async findUsers(): Promise<UsersEntity[]> { return this.
Read more >
typeorm/typeorm - Gitter
Hi! I have this piece of code: return this.tagAuditLogRepository .createQueryBuilder("logs") .select(["logs.id", `logs."tagId"`]) .
Read more >
Select using Query Builder - typeorm - GitBook
QueryBuilder is one of the most powerful features of TypeORM - it allows you to build SQL queries using elegant and convenient syntax,...
Read more >
SelectQueryBuilder | typeorm
Executes sql generated by query builder and returns object with raw results and entities created from them. Returns Promise<object>. getRawMany. getRawMany() ...
Read more >
TypeORM - Query Builder with Subquery - DEV Community ‍ ‍
import {Entity, PrimaryGeneratedColumn, Column} from "typeorm"; ... need to set them on your main query before calling getRawMany() .
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found