Mongoose tx is a transaction helper that runs transaction with retry and commit with retry according to the Mongodb documentation
Took a lot of inspiration from the quite promising mongoose ACID library you can find here :
nodejs >= 7.6 | mongoose >= 5.2 | mongodb >= 4.0
const Tx = require('mongoose-tx');
await Tx( async (session) => {
await People.findOneAndUpdate({ name: 'Tx' },{ $inc: { balance: 30 } },{ session });
await Account.findOneAndUpdate({ name: 'Blank'},{ $inc: { balance: -30 } },{ session });
// ...
});
const app = new Koa();
app.use(Tx.middleware());
app.use(async (ctx) => {
await ctx.tx(async (session) => {
});
})
npm test