diff --git a/models/table.js b/models/table.js index fce0fbaec..2fc1a58ca 100644 --- a/models/table.js +++ b/models/table.js @@ -421,6 +421,7 @@ class Table { query.distinct(options.distinct); options.clearSelect = true; } + if (options.distinctOn) query.distinctOn(options.distinctOn); if (options.clearSelect || (!Array.isArray(columns) && columns !== 'all')) return query; if (!Array.isArray(columns)) columns = '*'; return query.select(columns); diff --git a/test/models/table/find/find2.js b/test/models/table/find/find2.js index 61e5eafa7..972c98581 100644 --- a/test/models/table/find/find2.js +++ b/test/models/table/find/find2.js @@ -47,6 +47,18 @@ describe('TABLE GATEWAY: FIND', () => { // eslint-disable-line assert.equal(keys.length, 5); } }); + + + it('With distinctOn', async () => { // eslint-disable-line no-undef + await Coffee.save({ name: 'other', price: 99 }); + const results = await Coffee.find({}, ['name', 'price'], { distinctOn: 'name', orderBy: ['name', ['price', 'asc']] }); + const expected = [ + { name: 'expensive', price: 110 }, + { name: 'other', price: 99 }, + { name: 'this is the name', price: 100 }, + ]; + assert.deepEqual(results, expected); + }); }); describe('Malicious happy path', () => { // eslint-disable-line