REST API: ソートとページネーション
REST API のクエリ結果は、ソートとページネーションができます。
Strapi takes advantage of the ability of the `qs` library to parse nested objects to create more complex queries.
Use qs directly to generate complex queries instead of creating them manually. Examples in this documentation showcase how you can use qs.
You can also use the interactive query builder if you prefer playing with our online tool instead of generating queries with qs on your machine.
ソート
クエリでは sort パラメータで、1 つまたは複数フィールドのソートができます。構文は次のとおりです。
- 1 フィールド:
GET /api/:pluralApiId?sort=value - 複数フィールド:
GET /api/:pluralApiId?sort[0]=value1&sort[1]=value2(例: 2 フィールド)
並び順は次で指定します。
:ascは昇順(既定。省略可):descは降順
例: 2 フィールドでソート
sort に複数フィールドを渡して並べ替えます。
GET /api/restaurants?sort[0]=Description&sort[1]=Name
JavaScript のクエリ(qs ライブラリで組み立て):
The query URL above was built using the `qs` library.
qs can be run locally on your machine, as shown in the following code example, or you can use our interactive query builder online tool.
const qs = require('qs');
const query = qs.stringify({
sort: ['Description', 'Name'],
}, {
encodeValuesOnly: true, // prettify URL
});
await request(`/api/restaurants?${query}`);
{
"data": [
{
"id": 9,
"documentId": "hgv1vny5cebq2l3czil1rpb3",
"Name": "BMK Paris Bamako",
"Description": [
{
"type": "paragraph",
"children": [
{
"type": "text",
"text": "A very short description goes here."
}
]
}
],
// …
},
{
"id": 8,
"documentId": "flzc8qrarj19ee0luix8knxn",
"Name": "Restaurant D",
"Description": [
{
"type": "paragraph",
"children": [
{
"type": "text",
"text": "A very short description goes here."
}
]
}
],
// …
},
// …
],
"meta": {
// …
}
}
例: 2 フィールドでソートし、昇順・降順を指定
各フィールドに :asc または :desc を付けて、希望の順序にします。
GET /api/restaurants?sort[0]=Description:asc&sort[1]=Name:desc
JavaScript のクエリ(qs ライブラリで組み立て):
The query URL above was built using the `qs` library.
qs can be run locally on your machine, as shown in the following code example, or you can use our interactive query builder online tool.
const qs = require('qs');
const query = qs.stringify({
sort: ['Description:asc', 'Name:desc'],
}, {
encodeValuesOnly: true, // prettify URL
});
await request(`/api/restaurants?${query}`);
{
"data": [
{
"id": 8,
"documentId": "flzc8qrarj19ee0luix8knxn",
"Name": "Restaurant D",
"Description": [
{
"type": "paragraph",
"children": [
{
"type": "text",
"text": "A very short description goes here."
}
]
}
],
// …
},
{
"id": 9,
"documentId": "hgv1vny5cebq2l3czil1rpb3",
"Name": "BMK Paris Bamako",
"Description": [
{
"type": "paragraph",
"children": [
{
"type": "text",
"text": "A very short description goes here."
}
]
}
],
// …
},
// …
],
"meta": {
// …
}
}
ページネーション
クエリでは pagination パラメータを使えます。次のいずれかの方法です。
ページネーションの方式は混在させられません。page と pageSize、または start と limit のどちらか一方だけを使ってください。
ページ番号によるページネーション
ページ番号で区切るには次のパラメータを使います。
| パラメータ | 型 | 説明 | 既定値 |
|---|---|---|---|
pagination[page] | Integer | ページ番号 | 1 |
pagination[pageSize] | Integer | 1 ページあたりの件数 | 25 |
pagination[withCount] | Boolean | レスポンスにエントリ総数とページ数を含めるか | True |
GET /api/articles?pagination[page]=1&pagination[pageSize]=10
JavaScript のクエリ(qs ライブラリで組み立て):
The query URL above was built using the `qs` library.
qs can be run locally on your machine, as shown in the following code example, or you can use our interactive query builder online tool.
const qs = require('qs');
const query = qs.stringify({
pagination: {
page: 1,
pageSize: 10,
},
}, {
encodeValuesOnly: true, // prettify URL
});
await request(`/api/articles?${query}`);
{
"data": [
// ...
],
"meta": {
"pagination": {
"page": 1,
"pageSize": 10,
"pageCount": 5,
"total": 48
}
}
}
オフセットによるページネーション
オフセットで区切るには次のパラメータを使います。
| パラメータ | 型 | 説明 | 既定値 |
|---|---|---|---|
pagination[start] | Integer | 開始位置(返す最初のエントリのオフセット) | 0 |
pagination[limit] | Integer | 返すエントリ数 | 25 |
pagination[withCount] | Boolean | レスポンスにエントリ総数を含めるか | true |
pagination[limit] の既定値と最大値は、./config/api.js の api.rest.defaultLimit と api.rest.maxLimit で設定できます。
GET /api/articles?pagination[start]=0&pagination[limit]=10
JavaScript のクエリ(qs ライブラリで組み立て):
The query URL above was built using the `qs` library.
qs can be run locally on your machine, as shown in the following code example, or you can use our interactive query builder online tool.
const qs = require('qs');
const query = qs.stringify({
pagination: {
start: 0,
limit: 10,
},
}, {
encodeValuesOnly: true, // prettify URL
});
await request(`/api/articles?${query}`);
{
"data": [
// ...
],
"meta": {
"pagination": {
"start": 0,
"limit": 10,
"total": 42
}
}
}