npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2025 – Pkg Stats / Ryan Hefner

sql-lock

v0.1.3

Published

distributed lock manager based on mysql

Downloads

64

Readme

Build Status npm version

sql-lock

sql-lock is a distributed lock manager for NodeJS, which works with the help of a MySQL Server.

Motivation

There are a lot of use cases that require an exclusive access to a resource. In a distributed system, with several containers running the same piece of code in parallel, achieving this can get quite difficult.

sql-lock solves this problem, by allowing you to get distributed locks in your code from a MySQL server.

How does it work

Simple Locking Mechanism

  • On initialization it creates a table lockings containing a column id.
  • Whenever someone tries to get a lock, a transaction is started in MySQL and an entry is created with id equal to lock key.
  • This gives a row lock to the transaction, which prevents someone from trying to get the same lock again.
  • To release the lock, the transaction is committed and the row deleted. Anyone else trying to get a lock with same lock key can then proceed.
  • More information can be found in our blog posts -
    • https://medium.com/uc-engineering/pessimistic-locking-for-a-distributed-system-part-1-cc85c755c357
    • https://medium.com/uc-engineering/pessimistic-locking-for-a-distributed-system-part-2-f2d224567284

Requirements

  • MySQL Server
  • NodeJS > 6

Installation

npm install sql-lock

Usage

Initialization

const sqlLock = require('sql-lock');
sqlLock.initialize(MysqlURI, { locking_ttl: 30000 });
  • MysqlURI - MySQL connection string - Eg mysql://travis@127.0.0.1:3306/test.
  • Options:
    • locking_ttl - Default timeout for your locks (in ms)

This will automatically create a table named locking in the database.

Code

const sqlLock = require('sql-lock');
const lockReleaser = await sqlLock.getLock('lock_key', 3000);
await someAsyncWork();
lockReleaser(); //Release lock

This code gets a lock on the key lock_key which is released when either lockReleaser function is called or the lock times out.

If someone else tries to get a lock with the same key, they will have to wait till the first lock is released.

getLock accepts two parameters -

  • lockKey: string - mandatory
  • TTL: number - timeout in ms for the lock. If not given, the TTL given during initialization is considered as the timeout.

Contributions

Contributions are welcome. Please create a pull-request if you want to add new features, test-support or enhance the existing code.

License

MIT