Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implemented distinct/distinctOn #284

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
29 changes: 27 additions & 2 deletions lib/database.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,17 @@ const buildWhere = (conditions, firstArgIndex = 1) => {
return { clause: disjunction.join(' OR '), args };
};

const buildSelect = ({ distinct = false, distinctOn = [] }) => {
let select = 'SELECT';
if (distinctOn.length > 0) {
const fields = distinctOn.map((field) => `"${field}"`).join(', ');
select = `SELECT DISTINCT ON (${fields})`;
} else if (distinct) {
select = 'SELECT DISTINCT';
}
return select;
};

const updates = (delta, firstArgIndex = 1) => {
const clause = [];
const args = [];
Expand Down Expand Up @@ -103,6 +114,19 @@ class Query {
return this;
}

distinct(distinct = true) {
if (this.options.distinctOn)
Reflect.deleteProperty(this.options, 'distinctOn');
this.options.distinct = distinct;
return this;
}

distinctOn(fields = []) {
if (this.options.distinct) Reflect.deleteProperty(this.options, 'distinct');
this.options.distinctOn = fields;
return this;
}

limit(count) {
this.options.limit = count;
return this;
Expand All @@ -116,14 +140,15 @@ class Query {
prepare() {
const args = [];
const { table, fields, where, options } = this;
const { order, desc, limit, offset, distinct, distinctOn } = options;
const select = buildSelect({ distinct, distinctOn });
const names = buildFields(fields);
const sql = [`SELECT ${names} FROM "${table}"`];
const sql = [`${select} ${names} FROM "${table}"`];
if (where.length !== 0) {
const cond = buildWhere(where);
sql.push('WHERE ' + cond.clause);
args.push(...cond.args);
}
const { order, desc, limit, offset } = options;
if (order) sql.push('ORDER BY "' + order.join('", "') + '"');
if (desc) sql.push('ORDER BY "' + desc.join('", "') + '" DESC');
if (limit) sql.push('LIMIT ' + limit);
Expand Down
2 changes: 2 additions & 0 deletions metasql.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ export class Query {
);
order(field: string | Array<string>): Query;
desc(field: string | Array<string>): Query;
distinct(distinct: boolean): Query;
distinctOn(fields: Array<string>): Query;
limit(count: number): Query;
offset(count: number): Query;
then(
Expand Down
10 changes: 10 additions & 0 deletions test/sql.js
Original file line number Diff line number Diff line change
Expand Up @@ -349,4 +349,14 @@ const metadomain = require('metadomain');
await db.delete('Counter', { counterId });
test.end();
});

metatests.test('query.distinct/distinctOn', async (test) => {
const res1 = await db.select('City', ['countryId']).distinct();
test.strictSame(res1.length, 9);

const res2 = await db.select('City').distinctOn(['countryId']);
test.strictSame(res2.length, 9);

test.end();
});
})();