Skip to content

Commit ce78f27

Browse files
committed
fix: add extra fields to response object & add tests
Signed-off-by: Muhammad Aaqil <[email protected]>
1 parent 05ea0ec commit ce78f27

File tree

4 files changed

+51
-4
lines changed

4 files changed

+51
-4
lines changed

packages/filter/src/query.ts

Lines changed: 24 additions & 0 deletions
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

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,13 @@ 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
};
76-
7782
expectSchemaToAllowFilter(customerFilterSchema, filter);
7883
});
7984

@@ -560,6 +565,9 @@ class Customer extends Entity {
560565
@property()
561566
name: string;
562567

568+
@property()
569+
salary: number;
570+
563571
@hasMany(() => Order)
564572
orders?: Order[];
565573
}

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,6 @@ export function getFilterJsonSchemaFor(
8787
type: 'integer',
8888
minimum: 0,
8989
},
90-
9190
limit: {
9291
type: 'integer',
9392
minimum: 1,
@@ -113,11 +112,16 @@ export function getFilterJsonSchemaFor(
113112
type: 'string',
114113
examples: ['column1'],
115114
},
115+
groupBy: {
116+
type: 'array',
117+
items: {
118+
type: 'string',
119+
},
120+
},
116121
skip: {
117122
type: 'integer',
118123
minimum: 0,
119124
},
120-
121125
order: {
122126
oneOf: [
123127
{

packages/rest/src/writer.ts

Lines changed: 12 additions & 1 deletion
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)