使用文档服务 API 方法时,数据库生命周期钩子的触发方式不同
🌐 Database lifecycle hooks are triggered differently with the Document Service API methods
在 Strapi 5 中,由于 Draft & Publish 功能的新工作方式,数据库生命周期钩子在使用各种 Document Service API 方法时会以不同的方式触发。
🌐 In Strapi 5, database lifecycle hooks are triggered differently with the various Document Service API methods, mainly due to the new way the Draft & Publish feature works.
大多数使用案例应该只使用文档服务。文档服务 API 处理草稿与发布、国际化以及任何底层的 Strapi 逻辑。
🌐 The majority of use cases should only use the Document Service. The Document Service API handles Draft & Publish, i18n, and any underlying strapi logic.
然而,文档服务 API 可能并不适合你所有的使用场景;因此数据库层是开放的,允许你在数据库上进行任何操作而没有任何限制。用户随后需要依赖数据库生命周期钩子作为扩展数据库行为的系统。
🌐 However, the Document Service API might not suit all your use cases; the database layer is therefore exposed allowing you to do anything on the database without any restriction. Users would then need to resort to the database lifecycle hooks as a system to extend the database behaviour.
此页面是重大更改数据库的一部分,提供关于重大更改的信息以及从 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 中
在 Strapi v4 中,生命周期钩子按 Strapi v4 documentation中记录的方式工作。
迁移
🌐 Migration
本节重新组合了有关引入的重大更改的有用说明和程序。
🌐 This section regroups useful notes and procedures about the introduced breaking change.
注意
🌐 Notes
由文档服务 API 方法触发的数据库生命周期钩子
🌐 Database lifecycle hooks triggered by the Document Service API methods
根据调用的 文档服务 API 方法,将触发以下数据库生命周期钩子:
🌐 Depending on the Document Service API methods called, the following database lifecycle hooks are triggered:
| 文档服务 API 方法 | 触发的数据库生命周期钩子 |
|---|---|
findOne() | before(after) findOne |
findFirst() | before(之后) findOne |
findMany() | before(之后) findMany |
create() | 创建前(后) |
create({ status: 'published' }) |
|
update() |
|
update({ status: 'published' }) |
|
delete() | 在删除 之前(之后)如果删除多个语言环境,可以触发多次(每个语言环境一次) |
publish() |
|
unpublish() | 删除前(后)
|
discardDraft() |
|
count() | 前(后)计数 |
批量操作生命周期(createMany、updateMany、deleteMany)永远不会被文档服务 API 方法触发。
🌐 Bulk actions lifecycles (createMany, updateMany, deleteMany) will never be triggered by a Document Service API method.
手动操作
🌐 Manual procedure
用户可能需要调整其自定义代码以适应 Strapi 5 中的文档服务 API 方法如何触发生命周期钩子。
🌐 Users might need to adapt their custom code to how lifecycle hooks are triggered by Document Service API methods in Strapi 5.
你可能会在 this blog article中找到关于生命周期钩子的更多信息。