我如何使用 sequelize (MySQL) 和 node.js 自动将事务传递给每个查询

How can i pass transaction to each query automatically using sequelize (MySQL) and node.js

在 sequelize(MySQL) 中将事务附加到每个查询的函数意味着不将事务传递给每个查询。谁能帮帮我。

您可以使用 CLS:

const cls = require('cls-hooked');
const Sequelize = require('@sequelize/core');

const namespace = cls.createNamespace('my-very-own-namespace');
Sequelize.useCLS(namespace);

new Sequelize(....);
...
sequelize.transaction((t1) => {
  // With CLS enabled, the user will be created inside the transaction
  return User.create({ name: 'Alice' });
});

pass a transaction to all queries