Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions examples/todo-list/src/__tests__/acceptance/todo.acceptance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,35 @@ describe('TodoListApplication', () => {
.expect(200, [toJSON(todoInProgress)]);
});

it('queries todos with a filter and group by', async () => {
const todoInProgress = await givenTodoInstance(todoRepo, {
title: 'go to sleep',
isComplete: false,
});
await client
.get('/todos')
.query({
filter: {
count: 'title',
sum: 'id',
avg: 'id',
min: 'id',
max: 'id',
groupBy: {title: true},
},
})
.expect(200, [
toJSON({
...todoInProgress,
count: 1,
sum: 6,
avg: 6,
min: 6,
max: 6,
}),
]);
});

it('updates todos using a filter', async () => {
await givenTodoInstance(todoRepo, {
title: 'hello',
Expand Down
2 changes: 1 addition & 1 deletion examples/todo-list/src/models/todo.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import {belongsTo, Entity, model, property} from '@loopback/repository';
import {TodoList, TodoListWithRelations} from './todo-list.model';

@model()
@model({settings: {strict: false}})
export class Todo extends Entity {
@property({
type: 'number',
Expand Down
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