Skip to content

Commit

Permalink
feat: support groupby
Browse files Browse the repository at this point in the history
Signed-off-by: Muhammad Aaqil <[email protected]>
  • Loading branch information
aaqilniz committed Sep 28, 2024
1 parent d76f00c commit 834539b
Show file tree
Hide file tree
Showing 6 changed files with 259 additions and 7 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 @@ -34,6 +34,14 @@ describe('sugar decorators for filter and where', () => {
title: 'MyModel.Filter',
'x-typescript-type': '@loopback/repository#Filter<MyModel>',
properties: {
avg: {
example: 'column1',
type: 'string',
},
count: {
example: 'column1',
type: 'string',
},
fields: {
oneOf: [
{
Expand All @@ -55,11 +63,29 @@ describe('sugar decorators for filter and where', () => {
},
},
],
title: 'MyModel.Fields',
title: 'MyModel.GroupBy',
},
offset: {type: 'integer', minimum: 0},
groupBy: {
items: {
type: 'string',
},
type: 'array',
},
limit: {type: 'integer', minimum: 1, example: 100},
max: {
example: 'column1',
type: 'string',
},
min: {
example: 'column1',
type: 'string',
},
skip: {type: 'integer', minimum: 0},
sum: {
example: 'column1',
type: 'string',
},
order: {
oneOf: [
{type: 'string'},
Expand Down Expand Up @@ -92,6 +118,14 @@ describe('sugar decorators for filter and where', () => {
title: 'MyModel.Filter',
'x-typescript-type': '@loopback/repository#Filter<MyModel>',
properties: {
avg: {
example: 'column1',
type: 'string',
},
count: {
example: 'column1',
type: 'string',
},
fields: {
oneOf: [
{
Expand All @@ -113,11 +147,29 @@ describe('sugar decorators for filter and where', () => {
},
},
],
title: 'MyModel.Fields',
title: 'MyModel.GroupBy',
},
offset: {type: 'integer', minimum: 0},
limit: {type: 'integer', minimum: 1, example: 100},
groupBy: {
items: {
type: 'string',
},
type: 'array',
},
skip: {type: 'integer', minimum: 0},
max: {
example: 'column1',
type: 'string',
},
min: {
example: 'column1',
type: 'string',
},
sum: {
example: 'column1',
type: 'string',
},
order: {
oneOf: [
{type: 'string'},
Expand Down Expand Up @@ -157,6 +209,14 @@ describe('sugar decorators for filter and where', () => {
title: 'MyModel.Filter',
'x-typescript-type': '@loopback/repository#Filter<MyModel>',
properties: {
avg: {
example: 'column1',
type: 'string',
},
count: {
example: 'column1',
type: 'string',
},
fields: {
oneOf: [
{
Expand All @@ -178,11 +238,29 @@ describe('sugar decorators for filter and where', () => {
},
},
],
title: 'MyModel.Fields',
title: 'MyModel.GroupBy',
},
offset: {type: 'integer', minimum: 0},
limit: {type: 'integer', minimum: 1, example: 100},
groupBy: {
items: {
type: 'string',
},
type: 'array',
},
skip: {type: 'integer', minimum: 0},
max: {
example: 'column1',
type: 'string',
},
min: {
example: 'column1',
type: 'string',
},
sum: {
example: 'column1',
type: 'string',
},
order: {
oneOf: [
{type: 'string'},
Expand Down
84 changes: 81 additions & 3 deletions packages/openapi-v3/src/__tests__/unit/filter-schema.unit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,14 @@ describe('filterSchema', () => {
type: 'object',
'x-typescript-type': '@loopback/repository#Filter<MyUserModel>',
properties: {
avg: {
example: 'column1',
type: 'string',
},
count: {
example: 'column1',
type: 'string',
},
where: {
type: 'object',
title: 'my-user-model.WhereFilter',
Expand Down Expand Up @@ -54,11 +62,29 @@ describe('filterSchema', () => {
},
},
],
title: 'my-user-model.Fields',
title: 'my-user-model.GroupBy',
},
offset: {type: 'integer', minimum: 0},
limit: {type: 'integer', minimum: 1, example: 100},
max: {
example: 'column1',
type: 'string',
},
min: {
example: 'column1',
type: 'string',
},
groupBy: {
items: {
type: 'string',
},
type: 'array',
},
skip: {type: 'integer', minimum: 0},
sum: {
example: 'column1',
type: 'string',
},
order: {
oneOf: [{type: 'string'}, {type: 'array', items: {type: 'string'}}],
},
Expand All @@ -75,6 +101,14 @@ describe('filterSchema', () => {
type: 'object',
'x-typescript-type': '@loopback/repository#Filter<MyUserModel>',
properties: {
avg: {
example: 'column1',
type: 'string',
},
count: {
example: 'column1',
type: 'string',
},
fields: {
oneOf: [
{
Expand All @@ -99,11 +133,29 @@ describe('filterSchema', () => {
},
},
],
title: 'my-user-model.Fields',
title: 'my-user-model.GroupBy',
},
offset: {type: 'integer', minimum: 0},
groupBy: {
items: {
type: 'string',
},
type: 'array',
},
limit: {type: 'integer', minimum: 1, example: 100},
max: {
example: 'column1',
type: 'string',
},
min: {
example: 'column1',
type: 'string',
},
skip: {type: 'integer', minimum: 0},
sum: {
example: 'column1',
type: 'string',
},
order: {
oneOf: [{type: 'string'}, {type: 'array', items: {type: 'string'}}],
},
Expand All @@ -129,6 +181,14 @@ describe('filterSchema', () => {
type: 'object',
'x-typescript-type': '@loopback/repository#Filter<CustomUserModel>',
properties: {
avg: {
example: 'column1',
type: 'string',
},
count: {
example: 'column1',
type: 'string',
},
where: {
type: 'object',
title: 'CustomUserModel.WhereFilter',
Expand Down Expand Up @@ -158,11 +218,29 @@ describe('filterSchema', () => {
},
},
],
title: 'CustomUserModel.Fields',
title: 'CustomUserModel.GroupBy',
},
offset: {type: 'integer', minimum: 0},
groupBy: {
items: {
type: 'string',
},
type: 'array',
},
limit: {type: 'integer', minimum: 1, example: 100},
max: {
example: 'column1',
type: 'string',
},
min: {
example: 'column1',
type: 'string',
},
skip: {type: 'integer', minimum: 0},
sum: {
example: 'column1',
type: 'string',
},
order: {
oneOf: [{type: 'string'}, {type: 'array', items: {type: '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
Loading

0 comments on commit 834539b

Please sign in to comment.