Skip to content

Commit

Permalink
fix: improve implmenetation for group by
Browse files Browse the repository at this point in the history
Signed-off-by: Muhammad Aaqil <[email protected]>
  • Loading branch information
aaqilniz committed Mar 2, 2024
1 parent e145a31 commit d067241
Showing 1 changed file with 35 additions and 26 deletions.
61 changes: 35 additions & 26 deletions packages/rest/src/writer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,33 +50,42 @@ export function writeResultToResponse(
// TODO(ritch) remove this, should be configurable
// See https://github.com/loopbackio/loopback-next/issues/436
response.setHeader('Content-Type', 'application/json');
let customResult = result;
let org: {[key: string]: Object[]} = {};

if (result && typeof result === 'object') {
if (Array.isArray(result)) {
customResult = [];
result.forEach((item: {[key: string]: Object[]}) => {
org = {};
if (typeof item === 'object') {
Object.keys(item).forEach(key => {
org[key] = item[key];
});
customResult.push(org);
} else {
customResult.push(item);
}
});
} else {
org = {};
Object.keys(result).forEach(key => {
org[key] = result[key];
});
customResult = org;
}
}
// let customResult = result;
// let org: {[key: string]: Object[]} = {};
// if (result && typeof result === 'object') {
// if (Array.isArray(result)) {
// customResult = [];
// result.forEach((item: {[key: string]: Object[]}) => {
// org = {};
// if (typeof item === 'object') {
// Object.keys(item).forEach(key => {
// org[key] = item[key];
// });
// customResult.push(org);
// } else {
// customResult.push(item);
// }
// });
// } else {
// org = {};
// Object.keys(result).forEach(key => {
// console.log('-----------------');
// console.log(key);
// console.log('-----------------');
// org[key] = result[key];
// });
// customResult = org;
// }
// }
// TODO(bajtos) handle errors - JSON.stringify can throw
result = JSON.stringify(customResult);
// result = JSON.stringify(customResult);
console.log('------------before------------');
console.log(result);
console.log('------------before------------');
result = JSON.stringify(result);
console.log('------------after------------');
console.log(result);
console.log('------------after------------');
}
break;
default:
Expand Down

0 comments on commit d067241

Please sign in to comment.