核心服务方法使用文档服务 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.
重大变更描述
🌐 Breaking change description
在 Strapi v4 中
核心控制器和 createCoreService 工厂默认使用实体服务 API。
例如,像 find、update 和 delete 这样的方法接收一个 entityId。
在 Strapi 5 中
核心控制器和 createCoreService 工厂使用 文档服务 API。
例如,find、update 和 delete 等方法接收一个 documentId。
迁移
🌐 Migration
本节重新组合了有关引入的重大更改的有用说明和程序。
🌐 This section regroups useful notes and procedures about the introduced breaking change.
注意
🌐 Notes
一些核心方法在 Strapi v4 中调用 super.find(ctx),而 super.find(ctx) 内部调用实体服务方法,而在 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 中:
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 中:
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:
- 查找所有带有自定义的
createCoreService调用。 - 如果
findOne, delete, update的任何函数针对集合类型扩展了核心方法,请按照 notes 中的说明更新它们。
此外,请参考 Entity Service API 到 Document Service API 的迁移 文档。
🌐 Additionally, please refer to the Entity Service API to Document Service API migration documentation.