Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions packages/filter/src/query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,14 @@ export type FilterExcludingWhere<MT extends object = AnyObject> = Omit<
'where'
>;

/**
* Filter without additional properties for count
*/
export type FilterExcludingsForCount<MT extends object = AnyObject> = Omit<
Filter<MT>,
'fields' | 'order' | 'limit' | 'skip' | 'offset' | 'include'
>;

/**
* TypeGuard for Filter
* @param candidate
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,14 @@ describe('CrudRestController for a simple Product model', () => {
.expect(200);
expect(body).to.deepEqual({count: 1 /* pencil was omitted */});
});

it('supports `filter` query param', async () => {
const {body} = await client
.get('/products/count')
.query({'filter[where][name]': pen.name})
.expect(200);
expect(body).to.deepEqual({count: 1 /* pencil was omitted */});
});
});

describe('updateAll', () => {
Expand Down
10 changes: 9 additions & 1 deletion packages/rest-crud/src/crud-rest.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
Entity,
EntityCrudRepository,
Filter,
FilterExcludingsForCount,
FilterExcludingWhere,
Where,
} from '@loopback/repository';
Expand Down Expand Up @@ -189,8 +190,15 @@ export function defineCrudRestController<
async count(
@param.where(modelCtor)
where?: Where<T>,
@param.query.object(
'filter',
getFilterSchemaFor(modelCtor, {
exclude: ['fields', 'order', 'limit', 'skip', 'offset', 'include'],
}),
)
filter?: FilterExcludingsForCount<T>,
): Promise<Count> {
return this.repository.count(where);
return this.repository.count(where || filter?.where);
}
}

Expand Down
Loading