Skip to content

Commit

Permalink
feat(countDistinct): with group by
Browse files Browse the repository at this point in the history
  • Loading branch information
afontainec committed Mar 9, 2021
1 parent cd13cec commit d4f934b
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
3 changes: 2 additions & 1 deletion models/table.js
Expand Up @@ -56,7 +56,8 @@ class Table {
}

countQuery(search, options) {
if (options && options.countDistinct) return this.countDistinctQuery(search, options);
const shouldCountAsDistinct = options && options.countDistinct && !options.groupBy;
if (shouldCountAsDistinct) return this.countDistinctQuery(search, options);
return this.buildQuery('count', search, 'all', options);
}

Expand Down
13 changes: 13 additions & 0 deletions test/models/table/count/count.js
Expand Up @@ -218,6 +218,19 @@ describe('with advance settings: countDistinct', () => { // eslint-disable-line
assert.equal(results, 3);
});

it('With countDistinct and group by', async () => { // eslint-disable-line
await Coffee.save({ price: 110, name: 'expensive' });
const results = await Coffee.count({}, {
countDistinct: 'name',
groupBy: 'price',
orderBy: 'price',
});
const expected = [{ price: 100, count: 2 },
{ price: 110, count: 1 },
{ price: null, count: 1 }];
assert.deepEqual(results, expected);
});

it('With invalid countDistinct', (done) => { // eslint-disable-line
Coffee.count({}, {
countDistinct: 'not a valid column',
Expand Down

0 comments on commit d4f934b

Please sign in to comment.