You're Invited:Meet the Socket Team at BlackHat and DEF CON in Las Vegas, Aug 4-6.RSVP
Socket
Book a DemoInstallSign in
Socket

mongo-distributed-locks

Package Overview
Dependencies
Maintainers
1
Versions
6
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

mongo-distributed-locks

Distributed locking based on Mongodb

1.0.0
Source
npmnpm
Version published
Weekly downloads
1
-96.15%
Maintainers
1
Weekly downloads
 
Created
Source

mongo-distributed-locks

Node.js distributed locking based on Mongodb.

Build Status npm version

Commands

# Add to project
$ npm i mongo-distributed-locks

Usage

const DLocks = require('mongo-distributed-locks');

async function takeFee({ userId, fee }) {
  let user = await User.findOne({ _id: userId });
  user.balance -= fee;
  return user.save();
}

function takeFeeWithLock(params) {
  return DLocks.exec({
    prefix: 'createPayment',
    objectId: params.userId,
    fn: takeFee.bind(null, params)
  });
}

function doWork() {
  let userId = 'some id';
  return Promise.all([
    takeFeeWithLock({ userId, fee: 10 }),
    takeFeeWithLock({ userId, fee: 25 })
  ]);
}

API

  • static registerLoggerErrorFn(loggerErrorFn)
    Registers logger error function (DLocks can log a few error messages), if not provided console.error is used`.

    • loggerErrorFn - logger error function. Example of usage: DLocks.registerLoggerErrorFn(logger.error.bind(logger));
  • static exec({ prefix, objectId, fn })
    Creates a DLocks instance and executes its exec method. Shortcut for let instance = new DLocks(params); instance.exec();.

    • prefix - the name of operation, createPayment, deleteUser, etc.
    • objectId - the id of locking object.
    • fn - the function that changes the locking object.
  • exec()
    Locks the resource and executes the provided function. Returns a promise.

Author

Alexander Mac

License

Licensed under the MIT license.

Keywords

distributed

FAQs

Package last updated on 08 Feb 2018

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

About

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.

  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc

U.S. Patent No. 12,346,443 & 12,314,394. Other pending.