Skip to main content

实体服务 API

¥Entity Service API

提醒

Strapi v5 中的实体服务 API 已弃用。请考虑改用 文档服务 API

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

☑️ Prerequisites

在深入了解实体服务 API 文档之前,建议你阅读以下介绍:

¥Before diving deeper into the Entity Service API documentation, it is recommended that you read the following introductions:

Strapi 后端提供了一个构建在 查询引擎 API 之上的实体服务 API。实体服务是处理 Strapi 复杂数据结构(如 components动态区域)的层,并在幕后使用查询引擎 API 来执行数据库查询。

¥The Strapi backend provides an Entity Service API, built on top of the Query Engine API. The Entity Service is the layer that handles Strapi's complex data structures like components and dynamic zones, and uses the Query Engine API under the hood to execute database queries.

🤓 实体服务 API 与查询引擎 API

Strapi v4 提供了多个层来与后端交互并构建查询:

¥Strapi v4 offers several layers to interact with the backend and build your queries:

  • 文档服务 API 是推荐的与应用数据库交互的 API。文档服务是处理 Strapi 文档模型和复杂数据结构(如组件和动态区域)的层,更底层的层不知道这些数据结构。

    ¥The Document Service API is the recommended API to interact with your application's database. The Document Service is the layer that handles Strapi's document model and the complex data structures like components and dynamic zones, which the lower-level layers are not aware of.

  • 查询引擎 API 与更底层的数据库层交互,并在幕后用于执行数据库查询。它提供对数据库层的无限制内部访问,但仅应在文档服务 API 未涵盖你的用例时使用。

    ¥The Query Engine API interacts with the database layer at a lower level and is used under the hood to execute database queries. It gives unrestricted internal access to the database layer, but should be used only if the Document Service API does not cover your use case.

  • 如果你需要直接访问 knex 功能,请使用 strapi.db.connection

    ¥If you need direct access to knex functions, use strapi.db.connection.

👀 消歧义:服务与实体服务

虽然 services 可以使用实体服务 API,但服务和实体服务 API 并不直接相关。你可以在 后端定制 文档中找到有关 Strapi 后端核心元素的更多信息。

¥While services can use the Entity Service API, services and the Entity Service API are not directly related. You can find more information about the core elements of the Strapi back end in the back-end customization documentation.

基本用法

¥Basic usage

实体服务可通过 strapi.entityService 获得:

¥The Entity Service is available through strapi.entityService:



const entry = await strapi.entityService.findOne('api::article.article', 1, {


populate: { someRelation: true },
});

可用操作

¥Available operations

实体服务 API 允许对实体执行以下操作:

¥The Entity Service API allows the following operations on entities: