gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 00:14:49 28 4
gpt4 key购买 nike

在将遗留的长 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/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com