Skip to main content

实体服务 API

¥Entity Service API

☑️ 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 Entity Service API is the recommended API to interact with your application's database. The Entity Service is the layer that handles Strapi's 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 Entity 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: