sql - 如何在我的 Node js中通过sequelize使用事务

标签 sql node.js transactions sequelize.js

在将遗留的长 SQL 过程转换为 Sequelize 程序时,我在对异步函数进行事务处理时遇到了麻烦。

我阅读了sequelizer的交易文件。但没能理解清楚。

这是我的代码。

const models = require('../models/models.js');
const sequelize = models.Sequelize;


async function isExistFeeHist(dansokSeqNo) {
  log.debug("isExistFeeHist()");
  let feeHist = await models.FeeHist.findOne({  
    where: { 
      DansokSeqNo: dansokSeqNo,
      FeeStatus: {[Op.ne]: null}, //is not null
      DelYN: false
    }
  });
  return !!feeHist;
}

async function isPaid(dansokSeqNo) {
  ...
}

async function getNextDansokHistSerialNo(dansokSeqNo) {
  ...
}

async function getVBankSeqNo(dansokSeqNo) {
  ...                
}

async function updateVBankList(dansokSeqNo, vBankSeqNo) {
  ...
}

//check if can cancel
async function checkCancelable(dansokSeqNo) {
  log.debug("checkCancelable() ", dansokSeqNo);

  if (await isExistFeeHist(dansokSeqNo)) {
    let e = {status:400, message: 'already imposed dansokSeqNo ' + dansokSeqNo };
    return Promise.reject({status:400, message: e.message });
  }

  if (await isPaid(dansokSeqNo)) {
    let e = {status:400, message: 'already paid dansokSeqNo ' + dansokSeqNo };
    return Promise.reject({status:400, message: e.message });
  }

  return Promise.resolve();
}

....

async function doCancel(dansokSeqNo, cancelCauseCode, histMemo) {

  try {
    await checkCancelable(dansokSeqNo); 

    //// <== Here I want to start transaction

    let nextDansokSerialNo =  await getNextDansokHistSerialNo(dansokSeqNo);
    let dansokHist = await insertNewDansokHist(dansokSeqNo, nextDansokSerialNo, cancelCauseCode, histMemo);
    await updateDansokHist(dansokSeqNo, cancelCauseCode);
    let vBankSeqNo = await getVBankSeqNo(dansokSeqNo);
    if (vBankSeqNo > 0) {
      await updateVBankList(dansokSeqNo, vBankSeqNo);
      let vBankList = await getVBankList(dansokSeqNo);
    }

    // <== Here I want to commit transaction
  } catch (e) {
    // <== Here I want to rollback transaction
    return Promise.reject({status:e.status, message: e.message });
  }
}

exports.cancelDansok = function (req, res) {
  res.setHeader("Content-Type", "application/json; charset=utf-8");
  ...
  jwtAcessAuth(accessToken)
  .then((decoded) => {
    log.info("jwt success, ", decoded);
    worker = decoded.loginName;
    return doCancel(dansokSeqNo, cancelCauseCode, histMemo);
  })
  .then(() => {
    res.status(200).json({ message: 'cancelDansok success.' });
  })
  .catch(e => {
    return res.status(e.status).json(e);
  });
};

我的函数由多个异步函数组装而成。并且需要绑定(bind)一笔交易。

在我的几个异步等待函数中使用事务的最佳实践是什么?

最佳答案

这是 Sequlize for Transaction 提供的最佳示例:

您需要关心的是将交易传递到下一级链接

return sequelize.transaction(function (t) {

  // chain all your queries here. make sure you return them.
  return User.create({
    firstName: 'Abraham',
    lastName: 'Lincoln'
  }, {transaction: t}).then(function (user) {
    return user.setShooter({
      firstName: 'John',
      lastName: 'Boothe'
    }, {transaction: t});
  });

}).then(function (result) {
  // Transaction has been committed
  // result is whatever the result of the promise chain returned to the transaction callback
}).catch(function (err) {
  // Transaction has been rolled back
  // err is whatever rejected the promise chain returned to the transaction callback
});

了解更多详情: Transactions

关于sql - 如何在我的 Node js中通过sequelize使用事务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47648935/

相关文章:

sql - 如何通过基于用户的授权最大限度地减少 SQL 往返次数?

php - mysqli : know what caused the query error

javascript - 实现全面的快速路由解决方案后,获取 API 调用失败 : Unexpected token < in JSON at position 0

node.js - npm 包安装失败 "npm ERR! Error: EACCES: permission denied"

transactions - 数据库事务损坏时是否存在断点?

mysql - 如何计算一个数字重复了多少次以及多于一列

mysql - 如何进行正确的加盟?

node.js - 如何在 Node 中发送 OAuth 请求

php - Mysql - 并发事务中的行级锁定死锁

javascript - "atomic"操作被异步 ajax 回调干扰