Skip to content

Commit 24cce3f

Browse files
committed
feat: provide support for groupby
Signed-off-by: Muhammad Aaqil <[email protected]>
1 parent d76f00c commit 24cce3f

File tree

5 files changed

+109
-2
lines changed

5 files changed

+109
-2
lines changed

packages/filter/src/query.ts

+24
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,30 @@ export interface Filter<MT extends object = AnyObject> {
230230
* To include related objects
231231
*/
232232
include?: InclusionFilter[];
233+
/**
234+
* return groupBy of
235+
*/
236+
groupBy?: string[];
237+
/**
238+
* return sum of
239+
*/
240+
sum?: string;
241+
/**
242+
* return min of
243+
*/
244+
min?: string;
245+
/**
246+
* return max of
247+
*/
248+
max?: string;
249+
/**
250+
* return avg of
251+
*/
252+
avg?: string;
253+
/**
254+
* return count of
255+
*/
256+
count?: string;
233257
}
234258

235259
/**

packages/repository-json-schema/src/__tests__/unit/filter-json-schema.unit.ts

+9
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,12 @@ describe('getFilterJsonSchemaFor', () => {
7272
limit: 10,
7373
order: ['id DESC'],
7474
skip: 0,
75+
sum: 'salary',
76+
min: 'salary',
77+
max: 'salary',
78+
avg: 'salary',
79+
count: 'salary',
80+
groupBy: ['salary'],
7581
};
7682

7783
expectSchemaToAllowFilter(customerFilterSchema, filter);
@@ -560,6 +566,9 @@ class Customer extends Entity {
560566
@property()
561567
name: string;
562568

569+
@property()
570+
salary: number;
571+
563572
@hasMany(() => Order)
564573
orders?: Order[];
565574
}

packages/repository-json-schema/src/filter-json-schema.ts

+63
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,32 @@ export function getFilterJsonSchemaFor(
9494
examples: [100],
9595
},
9696

97+
sum: {
98+
type: 'string',
99+
examples: ['column1'],
100+
},
101+
min: {
102+
type: 'string',
103+
examples: ['column1'],
104+
},
105+
max: {
106+
type: 'string',
107+
examples: ['column1'],
108+
},
109+
avg: {
110+
type: 'string',
111+
examples: ['column1'],
112+
},
113+
count: {
114+
type: 'string',
115+
examples: ['column1'],
116+
},
117+
groupBy: {
118+
type: 'array',
119+
items: {
120+
type: 'string',
121+
},
122+
},
97123
skip: {
98124
type: 'integer',
99125
minimum: 0,
@@ -120,6 +146,9 @@ export function getFilterJsonSchemaFor(
120146
if (!excluded.includes('fields')) {
121147
properties.fields = getFieldsJsonSchemaFor(modelCtor, options);
122148
}
149+
if (!excluded.includes('groupBy')) {
150+
properties.fields = getGroupByJsonSchemaFor(modelCtor, options);
151+
}
123152

124153
// Remove excluded properties
125154
for (const p of excluded) {
@@ -235,3 +264,37 @@ export function getFieldsJsonSchemaFor(
235264

236265
return schema;
237266
}
267+
268+
export function getGroupByJsonSchemaFor(
269+
modelCtor: typeof Model,
270+
options: FilterSchemaOptions = {},
271+
): JsonSchema {
272+
const schema: JsonSchema = {oneOf: []};
273+
if (options.setTitle !== false) {
274+
schema.title = `${modelCtor.modelName}.GroupBy`;
275+
}
276+
277+
const properties = Object.keys(modelCtor.definition.properties);
278+
const additionalProperties = modelCtor.definition.settings.strict === false;
279+
280+
schema.oneOf?.push({
281+
type: 'object',
282+
properties: properties.reduce(
283+
(prev, crr) => ({...prev, [crr]: {type: 'boolean'}}),
284+
{},
285+
),
286+
additionalProperties,
287+
});
288+
289+
schema.oneOf?.push({
290+
type: 'array',
291+
items: {
292+
type: 'string',
293+
enum: properties.length && !additionalProperties ? properties : undefined,
294+
examples: properties,
295+
},
296+
uniqueItems: true,
297+
});
298+
299+
return schema;
300+
}

packages/repository/src/repositories/legacy-juggler-bridge.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -739,7 +739,7 @@ export class DefaultCrudRepository<
739739
}
740740

741741
protected toEntity<R extends T>(model: juggler.PersistedModel): R {
742-
return new this.entityClass(model.toObject()) as R;
742+
return new this.entityClass(model.toObject({onlySchema: false})) as R;
743743
}
744744

745745
protected toEntities<R extends T>(models: juggler.PersistedModel[]): R[] {

packages/rest/src/writer.ts

+12-1
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,19 @@ export function writeResultToResponse(
5050
// TODO(ritch) remove this, should be configurable
5151
// See https://github.com/loopbackio/loopback-next/issues/436
5252
response.setHeader('Content-Type', 'application/json');
53+
let customResult = result;
54+
if (result.length) {
55+
customResult = [];
56+
result.forEach((item: {[key: string]: Object[]}) => {
57+
const org: {[key: string]: Object[]} = {};
58+
Object.keys(item).forEach(key => {
59+
org[key] = item[key];
60+
});
61+
customResult.push(org);
62+
});
63+
}
5364
// TODO(bajtos) handle errors - JSON.stringify can throw
54-
result = JSON.stringify(result);
65+
result = JSON.stringify(customResult);
5566
}
5667
break;
5768
default:

0 commit comments

Comments
 (0)