Skip to main content

使用文档服务 API 方法触发数据库生命周期钩子的方式不同

¥Database lifecycle hooks are triggered differently with the Document Service API methods

在 Strapi 5 中,数据库生命周期钩子通过各种 文档服务 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 处理草稿和发布、i18n 以及任何底层 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.

 Is this breaking change affecting plugins?Yes
 Is this breaking change automatically handled by a codemod?No

重大更改描述

¥Breaking change description

在 Strapi v4 中

¥In Strapi v4

在 Strapi v4 中,生命周期钩子的工作方式与 Strapi v4 文档  中记录的一样。

¥In Strapi v4, lifecycle hooks work as documented in the Strapi v4 documentation .

在 Strapi 5 中

¥In Strapi 5

生命周期钩子的工作方式与 Strapi v4 相同,但触发方式不同,具体取决于触发哪些文档服务 API 方法。完整参考可用(参见 notes)。

¥Lifecycle hooks work the same way as in Strapi v4 but are triggered differently, based on which Document Service API methods are triggered. A complete reference is available (see notes).

迁移

¥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(after) findOne
findMany()before(after) findMany
create()before(after) 创建
create({ status: 'published' })
update()
update({ status: 'published' })
delete()before(after) Delete
Can be triggered multiple times if deleting multiple locales (one per each locale)
publish()
unpublish()删除之前(之后)
  • 删除文档的所有已发布版本
  • 如果删除多个语言环境(每个语言环境一个)则可多次触发
discardDraft()
count()before(after) 计数
注意

批量操作生命周期(createManyupdateManydeleteMany)永远不会由文档服务 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.

其他资源

你可能会在 这篇博客文章  中找到有关生命周期钩子的其他信息。

¥You might find additional information about lifecycle hooks in this blog article .