mongoose-tx
v1.0.1
Published
Mongoose transaction helper for MongoDB >= 4.0 and Mongoose >= 5.2.0
Downloads
9
Maintainers
Readme
Mongoose TX
Mongoose tx is a transaction helper that runs transaction with retry and commit with retry according to the Mongodb documentation
Credits
Took a lot of inspiration from the quite promising mongoose ACID library you can find here :
required
nodejs >= 7.6 | mongoose >= 5.2 | mongodb >= 4.0
usage
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) => {
});
})
test
npm test