Skip to content

Instantly share code, notes, and snippets.

@kaidiren
Created March 14, 2017 04:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kaidiren/b136770dd71850eb36d4f0fe168f45eb to your computer and use it in GitHub Desktop.
Save kaidiren/b136770dd71850eb36d4f0fe168f45eb to your computer and use it in GitHub Desktop.
sequelize transaction sample
'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