Skip to main content
Unlisted page
This page is unlisted. Search engines will not index it, and only users having a direct link can access it.

实体服务 API

🌐 Entity Service API

Caution

在 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,构建在 Query Engine API 之上。实体服务是处理 Strapi 复杂内容结构(如 组件动态区域)的层,并在底层使用 Query Engine 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 content structures like components and dynamic zones, and uses the Query Engine API under the hood to execute database queries.

Entity Service API vs. Query Engine API

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

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

  • 文档服务 API 是与你的应用数据库交互的推荐 API。文档服务是处理 Strapi 文档模型以及组件和动态区域等复杂内容结构的层,底层层不知晓这些内容结构。
  • 查询引擎 API 在更低的层次与数据库层交互,并在后台用于执行数据库查询。它提供对数据库层的不受限制的内部访问,但只有在文档服务 API 无法满足你的使用场景时才应使用。
  • 如果你需要直接访问 knex 函数,请使用 strapi.db.connection
Disambiguation: Services vs. Entity Service

虽然服务可以使用实体服务 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: