0.1.0 • Published 3 years ago

mongoose-trx v0.1.0

Weekly downloads
4
License
MIT
Repository
github
Last release
3 years ago

mongoose-trx

CircleCI NPM Version Coverage Status License

A simple helper for hassle-free transactions with mongoose.

Requirements

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'