Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

transactions-mongoose

Package Overview
Dependencies
Maintainers
1
Versions
18
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

transactions-mongoose

Transactions for mongoose

  • 1.1.5
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
3
Maintainers
1
Weekly downloads
 
Created
Source

🇺🇦 Transactions for mongoose

npm version Downloads/month

Transactions allow you to perform multiple group operations in isolation and undo all operations if one of them fails.

This module allows you to combine different transactions and operations into a group and execute them together. Pre-checks the schema for errors, duplication of unique indexes/fields

Install module

npm i transactions-mongoose

Usage

const Transaction = require("transactions-mongoose");
const transaction = new Transaction();
// or with debug log
const transaction = new Transaction().setSendbox(true);

An exemplary use case

Create / Insert new document

const Transaction = require("transactions-mongoose");
const transaction = new Transaction().setSendbox(true);

const transactionData1 = transaction.add(Person, {
    firstname: 'Sancho',
    lastname: 'Panse',
    age: 22,
    sex: 'male',
    status: 'free'
});
const transactionData2 = transaction.add(Person, {
    firstname: 'Janna',
    lastname: 'Dark',
    age: 21,
    sex: 'famale',
    status: 'free'
});
transaction.add(Person, {
    firstname: 'Hulio',
    lastname: 'Iglessias',
    age: 35,
    sex: 'male',
    status: 'free'
});
await transaction.commit();

console.log('transaction 1', transactionData1.result) // the result of the save() operation
console.log('transaction 2 document', transactionData2.document)

Update an existing one

const Transaction = require("transactions-mongoose");
const transaction = new Transaction().setSendbox(true);

// variant #1 - use standard setters
let personSancho = await Person.findById('...Sancho id');
personSancho.age += 1;
personSancho.status = 'married';
personSancho.friend_id = '...Hulio Iglessias id';
transaction.add(Person, personSancho);

// variant #2 - by document and update object
let personJanna = await Person.findById('...Janna id');
transaction.add(Person, personJanna).update({
    age: ++personJanna.age,
    status: 'married',
    friend_id: personSancho._id,
    bodyFriend_id: '...Hulio Iglessias id',
});

// variant #3 - by ObjectId
transaction.add(Person, {_id: '...Sancho id'}).update({
    friend_id: personJanna._id
});

await transaction.commit();
const fetch = require("node-fetch");
const Transaction = require("transactions-mongoose");
const transaction = new Transaction().setSendbox(true);

const getAvatar = async (id) => {
    const response = await fetch('https://i.pravatar.cc/300?u=' + id);
    const blob = await response.blob();
    return new Promise((onSuccess, onError) => {
        const reader = new FileReader() ;
        reader.onload = function(){ onSuccess(this.result) } ;
        reader.readAsDataURL(blob) ;
    });
};

const transactionData = transaction.execute(async () => {
    let personSancho = await Person.findById('...Sancho id');
    transaction.add(Person, personSancho).update({
        updatedAt: Date.now(),
        __v: ++personSancho.__v
    });
    // there may be a timeout error or a reader processing error
    personSancho.avatar = await getAvatar(personSancho._id)

    transaction.execute(async () => {
        let personHulio = await Person.findById('...Hulio id');
        personHulio.avatar = await getAvatar(personHulio._id);
        transaction.add(Person, personHulio)
    });

    let personJanna = await Person.findById('...Janna id');
    personJanna.avatar = await getAvatar(personJanna._id);
    const td = transaction.add(Person, personJanna)
    
    // The result can be whatever you want
    // we will return the Janna document update result
    // https://mongoosejs.com/docs/api/query.html#Query.prototype.updateOne()
    return td.result 
});

// and also execute it :)
personJanna.updatedAt = Date.now()
transaction.add(Person, personJanna)

await transaction.commit();
console.log('transaction result', transactionData.result);

With session executor

const Transaction = require("transactions-mongoose");
const transaction = new Transaction().setSendbox(true);

transaction.session(async (session) => {
    let personJanna = await Person.findById('...Janna id');
    personJanna.age++;
    personJanna.updatedAt = Date.now()
    await personJanna.save()

    let personHulio = await Person.findById('...Hulio id');
    personHulio.age++;
    personHulio.updatedAt = Date.now()
    await personHulio.save()

    let personSancho = await Person.findById('...Sancho id');
    transaction.add(Person, personSancho).update({
        updatedAt: Date.now(),
        __v: ++personSancho.__v
    });

    throw new Error('Test an error - or remark me') // No changes will be saved

    // there must be a return result - and it must be a mongo document
    return personJanna
});

await transaction.commit();

Keywords

FAQs

Package last updated on 05 Oct 2023

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc