Entity Service API での CRUD 操作
The Entity Service API is deprecated in Strapi v5. Please consider using the Document Service API instead.
Entity Service API は Query Engine API の上にあり、エンティティに対する CRUD を実行するために使います。
この API の関数呼び出しで使う uid は、[category]::[content-type] 形式の文字列です。category は admin、plugin、api のいずれかです。
例:
- 管理パネルのユーザー取得に使う正しい
uidはadmin::user。 - Upload プラグインなら
plugin::upload.fileなど。 - ユーザー定義のコンテンツタイプは
api::[content-type]形式。例: コンテンツタイプarticleはapi::article.article。
ターミナルで strapi content-types:list を実行すると、その Strapi インスタンスで使えるコンテンツタイプの uid 一覧を表示できます。
findOne()
パラメータに一致する最初のエントリを取得します。
構文: findOne(uid: string, id: ID, parameters: Params) ⇒ Entry
パラメータ
| パラメータ | 説明 | 型 |
|---|---|---|
fields | 返す属性 | String[] |
populate | ポピュレート するリレーション・コンポーネント・ダイナミックゾーン | PopulateParameter |
例
const entry = await strapi.entityService.findOne('api::article.article', 1, {
fields: ['title', 'description'],
populate: { category: true },
});
findMany()
パラメータに一致するエントリを取得します。
構文: findMany(uid: string, parameters: Params) ⇒ Entry[]
パラメータ
| パラメ ータ | 説明 | 型 |
|---|---|---|
fields | 返す属性 | String[] |
filters | 使う フィルター | FiltersParameters |
start | スキップする件数(ページネーション) | Number |
limit | 取得件数(ページネーション) | Number |
sort | 並べ替え の定義 | OrderByParameter |
populate | ポピュレート するリレーションなど | PopulateParameter |
publicationState | 公開状態。次のいずれか。
| PublicationStateParameter |
例
const entries = await strapi.entityService.findMany('api::article.article', {
fields: ['title', 'description'],
filters: { title: 'Hello World' },
sort: { createdAt: 'DESC' },
populate: { category: true },
});
下書きだけを取得するには、preview と publishedAt を組み合わせます。
const entries = await strapi.entityService.findMany('api::article.article', {
publicationState: 'preview',
filters: {
publishedAt: {
$null: true,
},
},
});
create()
1 件作成して返します。
構文: create(uid: string, parameters: Params) ⇒ Entry
パラメータ
| パラメータ | 説明 | 型 |
|---|---|---|
fields | 返す属性 | String[] |
populate | ポピュレート するリレーションなど | PopulateParameter |
data | 入力データ | Object |
In the data object, relations can be managed with the connect, disconnect, and set parameters using the syntax described for the REST API (see managing relations).
例
const entry = await strapi.entityService.create('api::article.article', {
data: {
title: 'My Article',
},
});
update()
1 件更新して返します。
update() は部分更新のみです。data に含めない既存フィールドは置き換えられません。
構文: update(uid: string, id: ID, parameters: Params) ⇒ Entry
In the data object, relations can be managed with the connect, disconnect, and set parameters using the syntax described for the REST API (see managing relations).
パラメータ
| パラメータ | 説明 | 型 |
|---|---|---|
fields | 返す属性 | String[] |
populate | ポピュレート するリレーションなど | PopulateParameter |
data | 入力データ | object |
例
const entry = await strapi.entityService.update('api::article.article', 1, {
data: {
title: 'xxx',
},
});
delete()
1 件削除して返します。
構文: delete(uid: string, id: ID, parameters: Params) ⇒ Entry
パラメータ
| パラメータ | 説明 | 型 |
|---|---|---|
fields | 返す属性 | String[] |
populate | ポピュレート するリレーションなど | PopulateParameter |
例
const entry = await strapi.entityService.delete('api::article.article', 1);