Skip to main content

核心服务方法使用文档服务 API

¥Core service methods use the Document Service API

在 Strapi 5 中,核心服务方法使用文档服务 API 而不是实体服务 API。

¥In Strapi 5, core service methods use the Document Service API instead of the Entity Service API.

此页面是 重大变更数据库 的一部分,提供有关重大更改的信息以及从 Strapi v4 迁移到 Strapi 5 的其他说明。

¥This page is part of the breaking changes database and provides information about the breaking change and additional instructions to migrate from Strapi v4 to Strapi 5.

\🔌 此重大更改是否会影响插件?\
\🤖 此重大更改是否由 codemod 自动处理?\

重大更改描述

¥Breaking change description

在 Strapi v4 中

¥In Strapi v4

核心控制器和 createCoreService 工厂默认使用实体服务 API。
例如,findupdatedelete 等方法会收到 entityId

¥The core controllers and the createCoreService factory by default use the Entity Service API.
Methods such as, for instance, find, update, and delete receive an entityId.

在 Strapi 5 中

¥In Strapi 5

核心控制器和 createCoreService 工厂使用 文档服务 API
例如,findupdatedelete 等方法接收 documentId

¥The core controllers and the createCoreService factory use the Document Service API.
Methods such as, for instance, find, update, and delete receive a documentId.

迁移

¥Migration

本节重新组合了有关引入的重大更改的有用说明和程序。

¥This section regroups useful notes and procedures about the introduced breaking change.

注意

¥Notes

一些核心方法正在调用 super.find(ctx),它在 Strapi v4 中内部调用实体服务方法,而在 Strapi 5 中它们调用文档服务 API 方法。这可能会导致某些查询不再起作用,或者返回的结果与预期略有不同。

¥Some core methods are calling super.find(ctx) which internally calls entity service methods in Strapi v4, while they call Document Service API methods in Strapi 5. This may result in some queries no longer working, or returning slightly different results from expecting.

以下示例显示了应如何更新代码:

¥The following examples show how the code should be updated:

在 Strapi v4 中:

¥In Strapi v4:

/src/api/my-api-name/services/my-service.js
const { createCoreService } = require('@strapi/strapi').factories;

module.exports = createCoreService('api::address.address', {

findOne(entityId, params) {
// customization
super.findOne(entityId, params);

// or to show a bit more context
strapi.entityService.findOne(uid, entityId, params);
},

update(entityId, params) {
// customization
super.update(entityId, params);
},

delete(entityId, params) {
// customization
super.delete(entityId, params)
}

});

在 Strapi 5 中:

¥In Strapi 5:

/src/api/my-api-name/services/my-service.js
const { createCoreService } = require('@strapi/strapi').factories;

module.exports = createCoreService('api::address.address', {

findOne(documentId, params) {
// customization
super.findOne(documentId, params);

// or to show a bit more context
strapi.documents(uid).findOne(documentId, params);
},

update(documentId, params) {
// customization
super.update(documentId, params);
},

delete(documentId, params) {
// customization
super.delete(documentId, params)
}
});

手动程序

¥Manual procedure

要更新你的自定义代码:

¥To update your custom code:

  1. 查找所有对 createCoreService 的自定义调用。

    ¥Find all calls to createCoreService with customization.

  2. 如果集合类型的任何 findOne, delete, update 函数都在扩展核心方法,请按照 notes 中的说明更新它们。

    ¥If any of findOne, delete, update function for a collection type are extending core methods, update them as explained in the notes.

此外,请参阅 实体服务 API 到文档服务 API 的迁移 文档。

¥Additionally, please refer to the Entity Service API to Document Service API migration documentation.