egg.js sequelize 事务处理

let transaction;
try {
// 建立事务对象
transaction = await this.ctx.model.transaction();

// 事务增操作
await this.ctx.model.VirtualDeptMember.create({
JSON格式数据
}, {
transaction,
});

// 事务批量增操作
await this.ctx.model.VirtualDeptMember.bulkCreate({
JSON格式数据
}, {
transaction,
});

// 事务删操作
await this.ctx.model.VirtualDeptMember.destroy({
where: {
JSON格式数据
},
transaction,
});

// 事务改操作
await this.ctx.model.Device.update({
修改的JSON数据
}, {
where: {
查询的JSON数据
},
transaction,
});

// 事务查操作
await this.ctx.model.VirtualDeptMember.findAll({
where: {
...
},
transaction,
});

// 提交事务
await transaction.commit();
} catch (err) {
// 事务回滚
await transaction.rollback();
}

 

posted on 2022-01-21 10:57  zhangzongshan  阅读(290)  评论(0编辑  收藏  举报

导航