Add pagination

Signed-off-by: Alexandre Bodin <bodin.alex@gmail.com>
This commit is contained in:
Alexandre Bodin 2020-11-02 22:10:51 +01:00
parent 9438230648
commit cbafb3ef92
4 changed files with 50 additions and 3 deletions

View File

@ -22,13 +22,19 @@ module.exports = {
return ctx.forbidden();
}
const method = has('_q', query) ? 'search' : 'find';
const method = has('_q', query) ? 'searchPage' : 'findPage';
const permissionQuery = permissionChecker.buildPermissionQuery(query);
const results = await getService('entity-manager')[method](permissionQuery, model);
const { results, pagination } = await getService('entity-manager')[method](
permissionQuery,
model
);
ctx.body = results.map(entity => permissionChecker.sanitizeOutput(entity));
ctx.body = {
results: results.map(entity => permissionChecker.sanitizeOutput(entity)),
pagination,
};
},
async findOne(ctx) {

View File

@ -322,6 +322,19 @@ paths:
tags:
- Collection Types content management
description: Get a list of entries
responses:
200:
content:
application/json:
schema:
type: object
properties:
results:
type: array
items:
$ref: '#/components/schemas/entity'
pagination:
$ref: '#/components/schemas/pagination'
post:
tags:
- Collection Types content management
@ -473,6 +486,18 @@ components:
- type: string
- type: integer
pagination:
type: object
properties:
page:
type: integer
pageSize:
type: integer
pageCount:
type: integer
total:
type: integer
entity:
type: object
required:

View File

@ -48,10 +48,18 @@ module.exports = {
return strapi.entityService.find({ params }, { model });
},
findPage(params, model) {
return strapi.entityService.findPage({ params }, { model });
},
search(params, model) {
return strapi.entityService.search({ params }, { model });
},
searchPage(params, model) {
return strapi.entityService.searchPage({ params }, { model });
},
count(params, model) {
return strapi.entityService.count({ params }, { model });
},

View File

@ -33,6 +33,10 @@ module.exports = ({ db, eventHub, entityValidator }) => ({
return db.query(model).find(params, populate);
},
findPage({ params, populate }, { model }) {
return db.query(model).findPage(params, populate);
},
/**
* Promise to fetch record
*
@ -148,6 +152,10 @@ module.exports = ({ db, eventHub, entityValidator }) => ({
return db.query(model).search(params);
},
searchPage({ params }, { model }) {
return db.query(model).searchPage(params);
},
/**
* Promise to count searched records
*