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

> Source: https://strapi.nodejs.cn/cms/migration/v4-to-v5/breaking-changes/lifecycle-hooks-document-service

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

在 Strapi 5 中，当使用文档服务 API 方法时，数据库生命周期钩子的触发方式有所不同，这取决于操作类型和由于新的草稿与发布系统而导致的状态变化。

🌐 In Strapi 5, database lifecycle hooks are triggered differently when using Document Service API methods, varying by operation type and status changes due to the new Draft & Publish system.

在 Strapi 5 中，由于 [Draft & Publish](/cms/features/draft-and-publish) 功能的新工作方式，数据库生命周期钩子在使用各种 [Document Service API](/cms/api/document-service) 方法时会以不同的方式触发。

🌐 In Strapi 5, database lifecycle hooks are triggered differently with the various [Document Service API](/cms/api/document-service) methods, mainly due to the new way the [Draft & Publish](/cms/features/draft-and-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.

此页面是[重大更改数据库](/cms/migration/v4-to-v5/breaking-changes)的一部分，提供关于重大更改的信息以及从 Strapi v4 迁移到 Strapi 5 的附加说明。

🌐 This page is part of the [breaking changes database](/cms/migration/v4-to-v5/breaking-changes) 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}

🌐 Breaking change description

**在 Strapi v4 中**

在 Strapi v4 中，生命周期钩子按 [Strapi v4 documentation](https://docs-v4.strapi.io/cms/backend-customization/models#lifecycle-hooks)中记录的方式工作。

**在 Strapi 5 中**

生命周期钩子的工作方式与 Strapi v4 相同，但触发方式不同，取决于触发了哪些文档服务 API 方法。完整的参考资料可查阅（参见 [notes](#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](#notes)).

## 迁移 {#migration}

🌐 Migration

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

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

### 注意 {#notes}

🌐 Notes

#### 由文档服务 API 方法触发的数据库生命周期钩子 {#table}

🌐 Database lifecycle hooks triggered by the Document Service API methods 

根据调用的 [文档服务 API 方法](/cms/api/document-service)，将触发以下数据库生命周期钩子：

🌐 Depending on the [Document Service API methods](/cms/api/document-service) called, the following database lifecycle hooks are triggered:

| 文档服务 API 方法 | 触发的数据库生命周期钩子 |
|-----------------------------------|--------------------------------------|
| `findOne()` | before(after) findOne |
| `findFirst()` | before(之后) findOne |
| `findMany()` | before(之后) findMany |
| `create()` | 创建前（后） |
| `create({ status: 'published' })` | <ul><li>之前(之后) 创建️<br/>触发两次，因为它会创建草稿和已发布版本</li><li>之前(之后) 删除<ul><li>删除文档的先前草稿版本</li><li>如果删除多个语言版本，可以多次触发（每个语言版本一次）</li></ul></li></ul> |
| `update()` | <ul><li>在创建文档的新语言版本之前(之后) 创建<br/>在创建文档的新语言版本时</li><li>在更新文档的现有版本之前(之后) 更新<br/>在更新文档的现有版本时</li></ul> |
| `update({ status: 'published' })` | <ul><li>之前（之后）创建<br/>如果删除多个语言版本，可以触发多次（每个语言版本一次）</li><li>之前（之后）更新<br/>在更新文档的现有版本时</li><li>之前（之后）删除<ul><li>删除文档之前发布的版本</li><li>如果删除多个语言版本，可以触发多次（每个语言版本一次）</li></ul></li></ul> |
| `delete()` | 在删除<br/>之前（之后）如果删除多个语言环境，可以触发多次（每个语言环境一次） |
| `publish()` | <ul><li>之前（之后）创建<br/>如果删除多个本地化版本（每个本地化版本一次），可以多次触发</li><li>之前（之后）删除<ul><li>删除文档的先前发布版本</li><li>如果删除多个本地化版本（每个本地化版本一次），可以多次触发</li></ul></li></ul> |
| `unpublish()` | 删除前(后)<ul><li>删除文档的所有已发布版本</li><li>如果删除多个语言版本，可以多次触发（每个语言版本触发一次）</li></ul> |
| `discardDraft()` | <ul><li>之前(之后) 创建<ul><li>创建新的草稿版本</li><li>如果删除多个语言环境，可触发多次（每个语言环境一次）</li></ul></li><li>之前(之后) 删除<ul><li>删除文档的先前草稿版本</li><li>如果删除多个语言环境，可触发多次（每个语言环境一次）</li></ul></li></ul> |
| `count()` | 前(后)计数 |

:::note

批量操作生命周期（`createMany`、`updateMany`、`deleteMany`）永远不会被文档服务 API 方法触发。

🌐 Bulk actions lifecycles (`createMany`, `updateMany`, `deleteMany`) will never be triggered by a Document Service API method.

:::

### 手动操作 {#manual-procedure}

🌐 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.

:::strapi Additional resources

你可能会在 [this blog article](https://strapi.io/blog/when-to-use-lifecycle-hooks-in-strapi)中找到关于生命周期钩子的更多信息。

:::
