Skip to content

Commit

Permalink
feat: provide support for groupby
Browse files Browse the repository at this point in the history
Signed-off-by: Muhammad Aaqil <[email protected]>
  • Loading branch information
aaqilniz committed Feb 8, 2024
1 parent d76f00c commit 24cce3f
Show file tree
Hide file tree
Showing 5 changed files with 109 additions and 2 deletions.
24 changes: 24 additions & 0 deletions packages/filter/src/query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,30 @@ export interface Filter<MT extends object = AnyObject> {
* To include related objects
*/
include?: InclusionFilter[];
/**
* return groupBy of
*/
groupBy?: string[];
/**
* return sum of
*/
sum?: string;
/**
* return min of
*/
min?: string;
/**
* return max of
*/
max?: string;
/**
* return avg of
*/
avg?: string;
/**
* return count of
*/
count?: string;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,12 @@ describe('getFilterJsonSchemaFor', () => {
limit: 10,
order: ['id DESC'],
skip: 0,
sum: 'salary',
min: 'salary',
max: 'salary',
avg: 'salary',
count: 'salary',
groupBy: ['salary'],
};

expectSchemaToAllowFilter(customerFilterSchema, filter);
Expand Down Expand Up @@ -560,6 +566,9 @@ class Customer extends Entity {
@property()
name: string;

@property()
salary: number;

@hasMany(() => Order)
orders?: Order[];
}
Expand Down
63 changes: 63 additions & 0 deletions packages/repository-json-schema/src/filter-json-schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,32 @@ export function getFilterJsonSchemaFor(
examples: [100],
},

sum: {
type: 'string',
examples: ['column1'],
},
min: {
type: 'string',
examples: ['column1'],
},
max: {
type: 'string',
examples: ['column1'],
},
avg: {
type: 'string',
examples: ['column1'],
},
count: {
type: 'string',
examples: ['column1'],
},
groupBy: {
type: 'array',
items: {
type: 'string',
},
},
skip: {
type: 'integer',
minimum: 0,
Expand All @@ -120,6 +146,9 @@ export function getFilterJsonSchemaFor(
if (!excluded.includes('fields')) {
properties.fields = getFieldsJsonSchemaFor(modelCtor, options);
}
if (!excluded.includes('groupBy')) {
properties.fields = getGroupByJsonSchemaFor(modelCtor, options);
}

// Remove excluded properties
for (const p of excluded) {
Expand Down Expand Up @@ -235,3 +264,37 @@ export function getFieldsJsonSchemaFor(

return schema;
}

export function getGroupByJsonSchemaFor(
modelCtor: typeof Model,
options: FilterSchemaOptions = {},
): JsonSchema {
const schema: JsonSchema = {oneOf: []};
if (options.setTitle !== false) {
schema.title = `${modelCtor.modelName}.GroupBy`;
}

const properties = Object.keys(modelCtor.definition.properties);
const additionalProperties = modelCtor.definition.settings.strict === false;

schema.oneOf?.push({
type: 'object',
properties: properties.reduce(
(prev, crr) => ({...prev, [crr]: {type: 'boolean'}}),
{},
),
additionalProperties,
});

schema.oneOf?.push({
type: 'array',
items: {
type: 'string',
enum: properties.length && !additionalProperties ? properties : undefined,
examples: properties,
},
uniqueItems: true,
});

return schema;
}
Original file line number Diff line number Diff line change
Expand Up @@ -739,7 +739,7 @@ export class DefaultCrudRepository<
}

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

protected toEntities<R extends T>(models: juggler.PersistedModel[]): R[] {
Expand Down
13 changes: 12 additions & 1 deletion packages/rest/src/writer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,19 @@ 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;
if (result.length) {
customResult = [];
result.forEach((item: {[key: string]: Object[]}) => {
const org: {[key: string]: Object[]} = {};
Object.keys(item).forEach(key => {
org[key] = item[key];
});
customResult.push(org);
});
}
// TODO(bajtos) handle errors - JSON.stringify can throw
result = JSON.stringify(result);
result = JSON.stringify(customResult);
}
break;
default:
Expand Down

0 comments on commit 24cce3f

Please sign in to comment.