Ordering & Pagination
The Entity Service API offers the ability to order and paginate results found with its findMany() method.
Ordering​
To order results returned by the Entity Service API, use the sort
parameter. Results can be ordered based on a single or on multiple attribute(s) and can also use relational ordering.
Single​
To order results by a single field, pass it to the sort
parameter either:
- as a
string
to sort with the default ascending order, or - as an
object
to define both the field name and the order (i.e.'asc'
for ascending order or'desc'
for descending order)
strapi.entityService.findMany('api::article.article', {
sort: 'id',
});
// single with direction
strapi.entityService.findMany('api::article.article', {
sort: { id: 'desc' },
});
Multiple​
To order results by multiple fields, pass the fields as an array to the sort
parameter either:
- as an array of strings to sort multiple fields using the default ascending order, or
- as an array of objects to define both the field name and the order (i.e.
'asc'
for ascending order or'desc'
for descending order)
strapi.entityService.findMany('api::article.article', {
sort: ['publishDate', 'name'],
});
// multiple with direction
strapi.entityService.findMany('api::article.article', {
sort: [{ title: 'asc' }, { publishedAt: 'desc' }],
});
Relational ordering​
Fields can also be sorted based on fields from relations:
strapi.entityService.findMany('api::article.article', {
sort: {
author: {
name: 'asc',
},
},
});
Pagination​
Results can be paginated using 2 different strategies (see REST API documentation for more details):
- pagination by page, when defining the
page
andpageSize
parameters, - and pagination by offset, when defining the
start
andlimit
parameters.
2 different functions can be used to paginate results with the Entity Service API and accept different pagination strategies:
Function name | Possible pagination method(s) |
---|---|
findMany() | Offset pagination only |
findPage() |
|
findMany()
should only be used with offset pagination:
strapi.entityService.findMany('api::article.article', {
start: 10,
limit: 15,
});
findPage()
accepts both offset and page pagination, provided you use only one pagination strategy per query:
strapi.entityService.findPage('api::article.article', {
start: 10,
limit: 15,
});
strapi.entityService.findPage('api::article.article', {
page: 1,
pageSize: 15,
});