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

Entity Service API での CRUD 操作

Caution

The Entity Service API is deprecated in Strapi v5. Please consider using the Document Service API instead.

Entity Service APIQuery Engine API の上にあり、エンティティに対する CRUD を実行するために使います。

この API の関数呼び出しで使う uid は、[category]::[content-type] 形式の文字列です。categoryadminpluginapi のいずれかです。

例:

  • 管理パネルのユーザー取得に使う正しい uidadmin::user
  • Upload プラグインなら plugin::upload.file など。
  • ユーザー定義のコンテンツタイプは api::[content-type] 形式。例: コンテンツタイプ articleapi::article.article
Tip

ターミナルで 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公開状態。次のいずれか。
  • live — 公開済みのみ
  • preview — 下書きと公開の両方(既定)
PublicationStateParameter

const entries = await strapi.entityService.findMany('api::article.article', {
fields: ['title', 'description'],
filters: { title: 'Hello World' },
sort: { createdAt: 'DESC' },
populate: { category: true },
});

Tip

下書きだけを取得するには、previewpublishedAt を組み合わせます。

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
Tip

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 件更新して返します。

Note

update() は部分更新のみです。data に含めない既存フィールドは置き換えられません。

構文: update(uid: string, id: ID, parameters: Params)Entry

Tip

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