Query Engine API での並べ替えとページネーション
In most cases you should not use the Query Engine API and rather use the Document Service API.
Only use the Query Engine API if you exactly know what you are doing, for instance if you want to use a lower-level API that directly interacts with unique rows of the database.
Please keep in mind that the Query Engine API is not aware of the most advanced Strapi 5 features like Draft & Publish, Internationalization, Content History, and possibly more.
This also means that the Query Engine API will not be able to use documentId and will use id, which means it could lead to unattended consequences at the database level or partial or incomplete compatibility with Strapi 5 features.
Before diving deeper into the Query Engine API documentation, it is recommended that you read the following introductions:
- the backend customization introduction,
- and the Content APIs introduction.
Query Engine API では、結果を 並べ替え したり ページネーション したりできます。
並べ替え
orderBy で並べ替えます。単一、複数、リレーション経由 を指定できます。
単一
strapi.db.query('api::article.article').findMany({
orderBy: 'id',
});
// 向きを指定
strapi.db.query('api::article.article').findMany({
orderBy: { id: 'asc' },
});
複数
strapi.db.query('api::article.article').findMany({
orderBy: ['id', 'name'],
});
// 向きを指定した複数キー
strapi.db.query('api::article.article').findMany({
orderBy: [{ title: 'asc' }, { publishedAt: 'desc' }],
});
リレーシ ョン経由の並べ替え
strapi.db.query('api::article.article').findMany({
orderBy: {
author: {
name: 'asc',
},
},
});
ページネーション
offset と limit でページ分割します。
strapi.db.query('api::article.article').findMany({
offset: 15,
limit: 10,
});