数据库事务
¥Database transactions
这是一个实验性功能,可能会在未来版本中发生变化。
¥This is an experimental feature and is subject to change in future versions.
Strapi 5 提供一个 API 来将一组操作封装在一个事务中,以确保数据的完整性。
¥Strapi 5 provide an API to wrap a set of operations in a transaction that ensures the integrity of data.
事务是一组作为单个单元一起执行的操作。如果任何操作失败,则整个事务失败,数据将回滚到其先前的状态。如果所有操作都成功,则提交事务并将数据永久保存到数据库中。
¥Transactions are a set of operations that are executed together as a single unit. If any of the operations fail, the entire transaction fails and the data is rolled back to its previous state. If all operations succeed, the transaction is committed and the data is permanently saved to the database.
用法
¥Usage
事务通过将处理程序函数传递到 strapi.db.transaction
来处理:
¥Transactions are handled by passing a handler function into strapi.db.transaction
:
await strapi.db.transaction(async ({ trx, rollback, commit, onCommit, onRollback }) => {
// It will implicitly use the transaction
await strapi.entityService.create();
await strapi.entityService.create();
});
执行事务处理程序后,如果所有操作都成功,则提交事务。如果任何操作抛出,则回滚事务并将数据恢复到其先前的状态。
¥After the transaction handler is executed, the transaction is committed if all operations succeed. If any of the operations throws, the transaction is rolled back and the data is restored to its previous state.
在事务块中执行的每个 strapi.entityService
或 strapi.db.query
操作都将隐式使用该事务。
¥Every strapi.entityService
or strapi.db.query
operation performed in a transaction block will implicitly use the transaction.
事务处理程序属性
¥Transaction handler properties
处理程序函数接收具有以下属性的对象:
¥The handler function receives an object with the following properties:
属性 | 描述 |
---|---|
trx | 事务对象。它可用于在事务中执行 knex 查询。 |
commit | 用于提交事务的函数。 |
rollback | 用于回滚事务的函数。 |
onCommit | 用于注册将在事务提交后执行的回调的函数。 |
onRollback | 用于注册将在事务回滚后执行的回调的函数。 |
嵌套事务
¥Nested transactions
事务可以嵌套。当事务嵌套时,当外部事务提交或回滚时,内部事务也会提交或回滚。
¥Transactions can be nested. When a transaction is nested, the inner transaction is committed or rolled back when the outer transaction is committed or rolled back.
await strapi.db.transaction(async () => {
// It will implicitly use the transaction
await strapi.entityService.create();
// Nested transactions will implicitly use the outer transaction
await strapi.db.transaction(async ({}) => {
await strapi.entityService.create();
});
});
onCommit 和 onRollback
¥onCommit and onRollback
onCommit
和 onRollback
钩子可用于在事务提交或回滚后执行代码。
¥The onCommit
and onRollback
hooks can be used to execute code after the transaction is committed or rolled back.
await strapi.db.transaction(async ({ onCommit, onRollback }) => {
// It will implicitly use the transaction
await strapi.entityService.create();
await strapi.entityService.create();
onCommit(() => {
// This will be executed after the transaction is committed
});
onRollback(() => {
// This will be executed after the transaction is rolled back
});
});
使用 knex 查询
¥Using knex queries
事务也可以与 knex 查询一起使用,但在这些情况下必须明确调用 .transacting(trx)
。
¥Transactions can also be used with knex queries, but in those cases .transacting(trx)
must be explicitly called.
await strapi.db.transaction(async ({ trx, rollback, commit }) => {
await knex('users').where('id', 1).update({ name: 'foo' }).transacting(trx);
});
何时使用事务
¥When to use transactions