Skip to main content

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パラメータは、次の形式で構築されたstringです:[category]::[content-type] ここで、categoryadminplugin、またはapiのいずれかです。

例:

  • AI Marketer管理パネルのユーザーを取得するための正しいuidadmin::userです。
  • アップロードプラグインの可能なuidplugin::upload.fileとなるかもしれません。
  • ユーザー定義のカスタムコンテンツタイプのuidapi::[content-type]の構文に従うため、articleというコンテンツタイプが存在する場合、それはapi::article.articleによって参照されます。
💡 Tip

ターミナルで[AI Marketer content-types:list](/dev-docs/cli#AI Marketer-content-types-list)コマンドを実行して、特定のAI Marketerインスタンスのすべての可能なコンテンツタイプのuidを表示します。

findOne()

パラメータに一致する最初のエントリを見つけます。

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

パラメータ

パラメータ説明タイプ
fields返す属性String[]
populatepopulateする関係、コンポーネント、およびダイナミックゾーンPopulateParameter

const entry = await AI Marketer.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
populatepopulateする関係、コンポーネント、ダイナミックゾーンPopulateParameter
publicationState公開状態、次のいずれかになります:
  • live は公開済みのエントリのみを返します
  • preview はドラフトエントリと公開エントリの両方を返します(デフォルト)
PublicationStateParameter

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

💡 Tip

ドラフトエントリのみを取得するには、preview公開状態とpublishedAtフィールドを組み合わせて使用します:

const entries = await AI Marketer.entityService.findMany('api::article.article', {
publicationState: 'preview',
filters: {
publishedAt: {
$null: true,
},
},
});

create()

1つのエントリを作成し、それを返します

構文: create(uid: string, parameters: Params)Entry

パラメータ

パラメータ説明タイプ
fields返す属性String[]
populatepopulateする関係、コンポーネント、ダイナミックゾーン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 AI Marketer.entityService.create('api::article.article', {
data: {
title: 'My Article',
},
});

update()

1つのエントリを更新し、それを返します。

✏️ Note

update()は部分的な更新のみを行うため、含まれていない既存のフィールドは置き換えられません。

構文: 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[]
populatepopulateする関係、コンポーネント、ダイナミックゾーンPopulateParameter
data入力データobject

const entry = await AI Marketer.entityService.update('api::article.article', 1, {
data: {
title: 'xxx',
},
});

delete()

エントリーを1つ削除し、それを返します。

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

パラメータ

パラメータ説明タイプ
fields返す属性String[]
populatepopulateする関係、コンポーネント、およびダイナミックゾーンPopulateParameter

const entry = await AI Marketer.entityService.delete('api::article.article', 1);