Created
March 14, 2017 04:59
-
-
Save kaidiren/b136770dd71850eb36d4f0fe168f45eb to your computer and use it in GitHub Desktop.
sequelize transaction sample
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
const ruo = require('ruo'); | |
exports.complete = function *(id) { | |
const t = yield ruo.transaction({autocommit: false}); | |
let bill; | |
try { | |
bill = yield ruo.models.Bill.findOne({ | |
where: { | |
id: id, | |
status: 'failed', | |
}, | |
transaction: t, | |
}); | |
const money = yield ruo.models.Money.findOne({ | |
where: { | |
agentId: bill.agentId, | |
}, | |
transaction: t, | |
}); | |
yield money.decrement('balance', {by: bill.paid, transaction: t}); | |
bill = yield bill.update({status: 'success'}, { | |
fileds: ['status'], | |
transaction: t, | |
}); | |
} catch (e) { | |
t.rollback(); | |
throw e; | |
} | |
t.commit(); | |
return bill; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment