mongoose-trx
v0.1.0
Published
A simple helper for hassle-free mongoose transactions
Downloads
9
Readme
mongoose-trx
A simple helper for hassle-free transactions with mongoose.
Requirements
- mongoose (5.2.0 or higher)
Installation
npm install mongoose-trx
Usage
Executing a transaction and getting the result is as simple as doing:
const transaction = require('mongoose-trx');
const txOpts = { readConcern: 'majority', writeConcern: 'majority' };
const [customer] = await transaction(session => Customer.create([{ name: 'Test' }], { session }), txOpts);
// Continue with 'customer'
This is equivalent to the following native mongoose code which is less clean and more verbose at the same time.
const session = await Customer.startSession();
const txOpts = { readConcern: 'majority', writeConcern: 'majority' };
let customer;
await session.withTransaction(async () => {
[customer] = await Customer.create([{ name: 'Test' }], { session });
}, txOpts);
session.endSession();
// Continue with 'customer'