Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: provide support for groupby #10277

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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