I recently switched from Sequelize to TypeORM because I wanted proper typing and decorator support, but I'm struggling with certain queries that were easy to do in Sequelize.
// Sequelize
const count = await logEntry.aggregate('sender_id', 'count', {distinct: true})
console.log(count); // 4 (As a number)
// TypeORM
const count = await logEntry.getRepository().createQueryBuilder('log_entry').select('COUNT(DISTINCT(`sender_id`))', 'count').getRawOne()
console.log(count); // {count: 4} (As an object)
I'm basically writing the SELECT part of the query by hand, is there a better way to do this in TypeORM?
Not yet. We'll need to make a detailed proposal for this and all similar features and discuss its design first before implementing
@Ionaru this can be done now this way:
const count = await logEntry.getRepository().createQueryBuilder('log_entry').select('DISTINCT(`sender_id`)').getCount();
console.log(count); // 4 (As a number)
@afurculita
your solution did not work for me. .getCount()
always returns the number of rows in the table.
Most helpful comment
@afurculita
your solution did not work for me.
.getCount()
always returns the number of rows in the table.