【Strapi】Componentを含めてデータを登録する【カスタムAPI】

こんにちは、フリーランスエンジニアの太田雅昭です。

カスタムAPIでデータを登録する

ファイル構成などは省かせていただきますが、controllers/xxxのファイルでの話です。

データベースを操作するにはEntity Service APIとQuery Engine APIの2つがあります。このうち公式サイトの説明ではEntity Service APIの方が簡単だということでこちらを使いたかったのですが、create関数の説明が見当たらない。

なるほど、createするにはQuery Engine APIしかないのねと思いそちらで頑張ってみたのですが、どうしてもComponentが絡むとうまくいかない。そもそもComponentが入っているテーブルがわからない。APIディレクトリにもそれらしきものは見当たらない。

そこでGoogle先生にお伺いすると、以下で同じような状況の方がおられました。

Populate components while creating/updating an entry with query engine api

System Information Strapi Version: 4.11.2 Database: mySQL Node Version: 18.16.0 NPM Version: 9.5.1 Hello, I am using an external API to create some entries on my strapi backend. My collection type uses 2 components and I want to pass data to them. I am using the query engine API but I cannot populate the components.

Componentを含めて登録するには

結局、Entity Service APIにcreate関数が存在しましたので、以下のようにすれば簡単にComponentも含めて登録できます。

await strapi.entityService.create("api::xxx.xxx", { data: item });

うーむ、どこにEntity Service APIのcreate関数の説明があるのだろうか。。。

ではでは。