如何在我的节点js中通过sequelize使用事务

时间:2017-12-05 08:18:29

标签: sql node.js transactions sequelize.js

在将旧的长sql程序转换为续集器时,我遇到了为异步函数进行事务处理的麻烦。

我读过续集器的交易文件。但是没能清楚地理解。

这是我的代码。

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);
  });
};

我的功能是用几个异步功能组装的。它需要绑定一个事务。

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

1 个答案:

答案 0 :(得分:0)

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

您需要关注的是将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