Skip to content

Commit

Permalink
#45 Add support for group by
Browse files Browse the repository at this point in the history
  • Loading branch information
DrewImm committed Dec 1, 2023
1 parent 9956493 commit 5257ef9
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/dml/group-by.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { DatabaseRecord } from '../record';

type AllowedKeys<T extends DatabaseRecord = DatabaseRecord> = keyof T & string;

export type GroupBy<T extends DatabaseRecord = DatabaseRecord> =
AllowedKeys<T>[];
1 change: 1 addition & 0 deletions src/dml/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ export { QueryRepository, QueryRepositoryOptions } from './query-repository';
export * from './delete';
export * from './insert';
export * from './join';
export * from './group-by';
export * from './order-by';
export * from './select';
export * from './update';
Expand Down
19 changes: 19 additions & 0 deletions src/dml/query-builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { isCondition } from '../conditions';
import { columnName } from '../tokens';
import { DatabaseFunctionToken, isDatabaseFunction } from '../functions';
import { DatabaseRecord } from '../record';
import { GroupBy } from './group-by';

export class DatabaseQueryBuilder extends StatementBuilder {
// ------------------------------------------------------------------------
Expand Down Expand Up @@ -320,6 +321,20 @@ export class DatabaseQueryBuilder extends StatementBuilder {
return this;
}

public groupBy(by: GroupBy): this {
this.sql.append('GROUP BY ');

for (const key of by) {
this.sql.columnName(key);
this.sql.append(', ');
}

this.sql.trimEnd(', ');
this.sql.space();

return this;
}

public orderBy(by: OrderBy) {
this.sql.append('ORDER BY ');

Expand Down Expand Up @@ -361,6 +376,10 @@ export class DatabaseQueryBuilder extends StatementBuilder {
this.where(query.where);
}

if (query.groupBy?.length) {
this.groupBy(query.groupBy);
}

if (query.orderBy) {
this.orderBy(query.orderBy);
}
Expand Down
2 changes: 2 additions & 0 deletions src/dml/select.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { DatabaseFunctionToken } from '../functions/function-token';
import { DatabaseRecord } from '../record';
import { Join } from './join';
import { GroupBy } from './group-by';
import { OrderBy } from './order-by';
import { Where } from './where';

Expand Down Expand Up @@ -32,5 +33,6 @@ export interface SelectQuery<T extends DatabaseRecord = DatabaseRecord> {
join?: Join[];
where?: Where<T> | Where<T>[];
limit?: number;
groupBy?: GroupBy<T>;
orderBy?: OrderBy<T>;
}
16 changes: 16 additions & 0 deletions test/spec/dml/query-builder.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -458,6 +458,22 @@ describe('Query Builder', () => {
expect(params).toEqual(['bob']);
});

it('can group by', () => {
const { sql, params } = new DatabaseQueryBuilder()
.select({
columns: ['id'],
table: 'user',
where: { fname: 'bob' },
groupBy: ['fname', 'id'],
})
.toDatabaseQuery();

expect(sql).toEqual(
'SELECT "id" FROM "user" WHERE ("fname" = ?) GROUP BY "fname", "id"'
);
expect(params).toEqual(['bob']);
});

it('can order by', () => {
const { sql, params } = new DatabaseQueryBuilder()
.select({
Expand Down

0 comments on commit 5257ef9

Please sign in to comment.