mongoose-trx

0.1.0 • Public • Published

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'

Readme

Keywords

Package Sidebar

Install

npm i mongoose-trx

Weekly Downloads

0

Version

0.1.0

License

MIT

Unpacked Size

10.4 kB

Total Files

7

Last publish

Collaborators

  • aleg94