メインコンテンツまでスキップ
非公開のページ
このページは非公開です。 検索対象外となり、このページのリンクに直接アクセスできるユーザーのみに公開されます。

Query Engine API: 単一エントリ・複数エントリの操作

Caution

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.

Prerequisites

Before diving deeper into the Query Engine API documentation, it is recommended that you read the following introductions:

findOne()

Note

Document Service の findOne() で足りない場合に限り、Query Engine の findOne() を使ってください。

パラメータに一致する最初のエントリを取得します。

構文: findOne(parameters) ⇒ Entry

パラメータ

パラメータ説明
select文字列、または文字列の配列返す 属性
whereWhereParameter使う フィルター
offset整数スキップする件数
orderByOrderByParameter並べ替え の定義
populatePopulateParameterポピュレート するリレーション

const entry = await strapi.db.query('api::blog.article').findOne({
select: ['title', 'description'],
where: { title: 'Hello World' },
populate: { category: true },
});

findMany()

Note

Document Service の findMany() で足りない場合に限り、Query Engine の findMany() を使ってください。

パラメータに一致するエントリを取得します。

構文: findMany(parameters) ⇒ Entry[]

パラメータ

パラメータ説明
select文字列、または文字列の配列返す 属性
whereWhereParameter使う フィルター
limit整数取得件数
offset整数スキップする件数
orderByOrderByParameter並べ替え の定義
populatePopulateParameterポピュレート するリレーション

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文字列、または文字列の配列返す 属性
whereWhereParameter使う フィルター
limit整数取得件数
offset整数スキップする件数
orderByOrderByParameter並べ替え の定義
populatePopulateParameterポピュレート するリレーション

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()

Note

Document Service の create() で足りない場合に限り、Query Engine の create() を使ってください。

1 件作成して返します。

構文: create(parameters) => Entry

パラメータ

パラメータ説明
select文字列、または文字列の配列返す 属性
populatePopulateParameterポピュレート するリレーション
dataオブジェクト入力データ

const entry = await strapi.db.query('api::blog.article').create({
data: {
title: 'My Article',
},
});

update()

Note

Document Service の update() で足りない場合に限り、Query Engine の update() を使ってください。

1 件更新して返します。

構文: update(parameters) => Entry

パラメータ

パラメータ説明
select文字列、または文字列の配列返す 属性
populatePopulateParameterポピュレート するリレーション
whereWhereParameter使う フィルター
dataオブジェクト入力データ

const entry = await strapi.db.query('api::blog.article').update({
where: { id: 1 },
data: {
title: 'xxx',
},
});

delete()

Note

Document Service の delete() で足りない場合に限り、Query Engine の delete() を使ってください。

1 件削除して返します。

構文: delete(parameters) => Entry

パラメータ

パラメータ説明
select文字列、または文字列の配列返す 属性
populatePopulateParameterポピュレート するリレーション
whereWhereParameter使う フィルター

const entry = await strapi.db.query('api::blog.article').delete({
where: { id: 1 },
});