Skip to content

Commit 807e70b

Browse files
committed
feat: provide support for groupby
Signed-off-by: Muhammad Aaqil <[email protected]>
1 parent 012c2de commit 807e70b

File tree

1 file changed

+39
-1
lines changed

1 file changed

+39
-1
lines changed

lib/sql.js

+39-1
Original file line numberDiff line numberDiff line change
@@ -1439,7 +1439,25 @@ SQLConnector.prototype.buildSelect = function(model, filter, options) {
14391439
}
14401440
}
14411441

1442+
let extraSelect = '';
1443+
if (filter.sum) {
1444+
extraSelect = `SUM(${filter.sum}) as sumOf${filter.sum}, `;
1445+
}
1446+
if (filter.count) {
1447+
extraSelect += `COUNT(${filter.count}) as countOf${filter.count}, `;
1448+
}
1449+
if (filter.avg) {
1450+
extraSelect += `AVG(${filter.avg}) as avgOf${filter.avg}, `;
1451+
}
1452+
if (filter.min) {
1453+
extraSelect += `MIN(${filter.min}) as minOf${filter.min}, `;
1454+
}
1455+
if (filter.max) {
1456+
extraSelect += `MAX(${filter.max}) as maxOf${filter.max}, `;
1457+
}
1458+
14421459
let selectStmt = new ParameterizedSQL('SELECT ' +
1460+
extraSelect +
14431461
this.buildColumnNames(model, filter) +
14441462
' FROM ' + this.tableEscaped(model));
14451463

@@ -1449,6 +1467,10 @@ SQLConnector.prototype.buildSelect = function(model, filter, options) {
14491467
selectStmt.merge(whereStmt);
14501468
}
14511469

1470+
if (filter.groupBy) {
1471+
selectStmt.merge(this.buildGroupBy(filter.groupBy));
1472+
}
1473+
14521474
if (filter.order) {
14531475
selectStmt.merge(this.buildOrderBy(model, filter.order));
14541476
}
@@ -1510,7 +1532,23 @@ SQLConnector.prototype.all = function find(model, filter, options, cb) {
15101532
}
15111533

15121534
const objs = data.map(function(obj) {
1513-
return self.fromRow(model, obj);
1535+
const object = self.fromRow(model, obj);
1536+
if (obj[`sumOf${filter.sum}`]) {
1537+
object[`sumOf${filter.sum}`] = obj[`sumOf${filter.sum}`];
1538+
}
1539+
if (obj[`countOf${filter.count}`]) {
1540+
object[`countOf${filter.count}`] = obj[`countOf${filter.count}`];
1541+
}
1542+
if (obj[`avgOf${filter.avg}`]) {
1543+
object[`avgOf${filter.avg}`] = obj[`avgOf${filter.avg}`];
1544+
}
1545+
if (obj[`minOf${filter.min}`]) {
1546+
object[`minOf${filter.min}`] = obj[`minOf${filter.min}`];
1547+
}
1548+
if (obj[`maxOf${filter.max}`]) {
1549+
object[`maxOf${filter.max}`] = obj[`maxOf${filter.max}`];
1550+
}
1551+
return object;
15141552
});
15151553
if (filter && filter.include) {
15161554
self.getModelDefinition(model).model.include(

0 commit comments

Comments
 (0)