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.
findOne()
Document Service の findOne() で足りない場合に限り、Query Engine の findOne() を使ってください。
パラメータに一致する最初のエントリを取得します。
構文: findOne(parameters) ⇒ Entry
パラメータ
| パラメータ | 型 | 説明 |
|---|---|---|
select | 文字列、または文字列の配列 | 返す 属性 |
where | WhereParameter | 使う フィルター |
offset | 整数 | スキップする件数 |
orderBy | OrderByParameter | 並べ替え の定義 |
populate | PopulateParameter | ポピュレート するリレーション |
例
const entry = await strapi.db.query('api::blog.article').findOne({
select: ['title', 'description'],
where: { title: 'Hello World' },
populate: { category: true },
});
findMany()
Document Service の findMany() で足りない場合に限り、Query Engine の findMany() を使ってください。
パラメータに一致するエントリを取得します。
構文: findMany(parameters) ⇒ Entry[]
パラメータ
| パラメータ | 型 | 説明 |
|---|---|---|
select | 文字列、または文字列の配列 | 返す 属性 |
where | WhereParameter | 使う フィルター |
limit | 整数 | 取得件数 |
offset | 整数 | スキップする件数 |
orderBy | OrderByParameter | 並べ替え の定義 |
populate | PopulateParameter | ポピュレート するリレーション |
例
const entries = await strapi.db.query('api::blog.article').findMany({
select: ['title', 'description'],
where: { title: 'Hello World' },
orderBy: { publishedAt: 'DESC' },
populate: { category: true },
});
findWithCount()
条件に一致するエントリを取得し、件数も返します。
構文: findWithCount(parameters) => [Entry[], number]
パラメータ
| パラメータ | 型 | 説明 |
|---|---|---|
select | 文字列、または文字列の配列 | 返す 属性 |
where | WhereParameter | 使う フィルター |
limit | 整数 | 取得件数 |
offset | 整数 | スキップする件数 |
orderBy | OrderByParameter | 並べ替え の定義 |
populate | PopulateParameter | ポピュレート するリレーション |
例
const [entries, count] = await strapi.db.query('api::blog.article').findWithCount({
select: ['title', 'description'],
where: { title: 'Hello World' },
orderBy: { title: 'DESC' },
populate: { category: true },
});
create()
Document Service の create() で足りない場合に限り、Query Engine の create() を使ってください。
1 件作成して返します。
構文: create(parameters) => Entry
パラメータ
| パラメータ | 型 | 説明 |
|---|---|---|
select | 文字列、または文字列の配列 | 返す 属性 |
populate | PopulateParameter | ポピュレート するリレーション |
data | オブジェクト | 入力データ |
例
const entry = await strapi.db.query('api::blog.article').create({
data: {
title: 'My Article',
},
});
update()
Document Service の update() で足りない場合に限り、Query Engine の update() を使ってください。
1 件更新して返します。
構文: update(parameters) => Entry