Skip to main content

使用文档服务 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.

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

重大变更描述

🌐 Breaking change description

在 Strapi v4 中

在 Strapi v4 中,生命周期钩子按 Strapi v4 documentation中记录的方式工作。

在 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(之后) findOne
findMany()before(之后) findMany
create()创建前(后)
create({ status: 'published' })
  • 之前(之后) 创建️
    触发两次,因为它会创建草稿和已发布版本
  • 之前(之后) 删除
    • 删除文档的先前草稿版本
    • 如果删除多个语言版本,可以多次触发(每个语言版本一次)
update()
  • 在创建文档的新语言版本之前(之后) 创建
    在创建文档的新语言版本时
  • 在更新文档的现有版本之前(之后) 更新
    在更新文档的现有版本时
update({ status: 'published' })
  • 之前(之后)创建
    如果删除多个语言版本,可以触发多次(每个语言版本一次)
  • 之前(之后)更新
    在更新文档的现有版本时
  • 之前(之后)删除
    • 删除文档之前发布的版本
    • 如果删除多个语言版本,可以触发多次(每个语言版本一次)
delete()在删除
之前(之后)如果删除多个语言环境,可以触发多次(每个语言环境一次)
publish()
  • 之前(之后)创建
    如果删除多个本地化版本(每个本地化版本一次),可以多次触发
  • 之前(之后)删除
    • 删除文档的先前发布版本
    • 如果删除多个本地化版本(每个本地化版本一次),可以多次触发
unpublish()删除前(后)
  • 删除文档的所有已发布版本
  • 如果删除多个语言版本,可以多次触发(每个语言版本触发一次)
discardDraft()
  • 之前(之后) 创建
    • 创建新的草稿版本
    • 如果删除多个语言环境,可触发多次(每个语言环境一次)
  • 之前(之后) 删除
    • 删除文档的先前草稿版本
    • 如果删除多个语言环境,可触发多次(每个语言环境一次)
count()前(后)计数
Note

批量操作生命周期(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.

Additional resources

你可能会在 this blog article中找到关于生命周期钩子的更多信息。