Gathering detailed insights and metrics for mongo-leader
Gathering detailed insights and metrics for mongo-leader
Gathering detailed insights and metrics for mongo-leader
Gathering detailed insights and metrics for mongo-leader
npm install mongo-leader
Typescript
Module System
Node Version
NPM Version
JavaScript (94.81%)
Makefile (5.19%)
Total Downloads
0
Last Day
0
Last Week
0
Last Month
0
Last Year
0
MIT License
8 Stars
323 Commits
5 Forks
1 Watchers
4 Branches
4 Contributors
Updated on Jul 10, 2025
Latest Version
1.2.152
Package Id
mongo-leader@1.2.152
Unpacked Size
14.09 kB
Size
5.45 kB
File Count
15
NPM Version
10.8.2
Node Version
20.18.0
Published on
Nov 10, 2024
Cumulative downloads
Total Downloads
mongo-leader
is a Node.js package for leader election backed by MongoDB. It is inspired by the redis-leader library. The main class Leader
extends the Node.js EventEmmiter class, allowing instances to emit events when they gain or lose leadership status. This makes it a powerful tool for managing distributed systems where only one instance should be in control at any given time.
To install mongo-leader
package with npm, you can use the following command:
1npm install mongo-leader
1const client = await MongoClient.connect(url) 2 3const leader = new Leader(client.db('test')) 4await leader.start() // Optional. If not called, a lazy start is initiated when isLeader is called for the first time. 5 6setInterval(async () => { 7 const isLeader = await leader.isLeader() 8 console.log(`Am I leader? : ${isLeader}`) 9}, 100)
1.1.144
Breaking changes have been made when upgrading from a version earlier than 1.1.144
. All asynchronous operations have been shifted from the constructor to a new method named start()
, which needs to be called separately like in the example above.
Creates a new Leader instance.
db
: A MongoClient object.options
: An object with the following properties:
ttl
: Lock time to live in milliseconds. The lock will be automatically released after this time. Default and minimum values are 1000.wait
: Time between tries getting elected in milliseconds. Default and minimum values are 100.key
: Unique identifier for the group of instances trying to be elected as leader. Default value is 'default'.When the Leader
constructor is invoked, it immediately initiates the election process to become the leader. This means that as soon as a Leader
instance is created, it starts competing with other instances (if any) to gain the leadership role. This is done by attempting to acquire a lock in the MongoDB collection. If the lock is successfully acquired, the instance becomes the leader. The lock has a time-to-live (TTL) associated with it, after which it is automatically released. This allows for a continuous and dynamic leadership election process where leadership can change over time, especially in scenarios where the current leader instance becomes unavailable or is shut down.
This method triggers the election process. It carries out the required setup in database and kick-starts the election procedure.
This method checks whether the current instance is the leader or not. It returns a Promise that resolves to a boolean value. If the returned value is true
, it means the current instance is the leader. If the returned value is false
, it means the current instance is not the leader. If the instance has not been initialized yet, a lazy start is performed.
This method is used to pause the leader election process. When called, the instance will stop trying to become a leader. This can be useful in scenarios where you want to manually control when your instance is trying to become a leader.
Note: The
pause()
method does not make the instance resign if it is currently a leader. It simply stops the instance from attempting to become a leader in the future.
This method is used to resume the leader election process. When called, the instance will start trying to become a leader again. This can be useful in scenarios where you have previously paused the leader election process and now want to allow your instance to become a leader again.
Note: The
resume()
method does not make the instance become a leader immediately. It simply allows the instance to start attempting to become a leader again.
The elected
event is emitted when the instance successfully becomes a leader. This event can be listened to in order to perform actions that should only be done by the leader instance. For example, you might want to start certain tasks or services only when the instance has been elected as the leader.
The revoked
event is emitted when the instance loses its leadership status. This event can be listened to in order to perform actions when the instance is no longer the leader. For example, you might want to stop certain tasks or services when the instance has been revoked from being the leader.
This project is licensed under the MIT License.
No vulnerabilities found.
Reason
no dangerous workflow patterns detected
Reason
no binaries found in the repo
Reason
license file detected
Details
Reason
3 existing vulnerabilities detected
Details
Reason
dependency not pinned by hash detected -- score normalized to 5
Details
Reason
Found 1/25 approved changesets -- score normalized to 0
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
detected GitHub workflow tokens with excessive permissions
Details
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
Reason
project is not fuzzed
Details
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
Score
Last Scanned on 2025-07-07
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn MoreLast Day
0%
NaN
Compared to previous day
Last Week
0%
NaN
Compared to previous week
Last Month
0%
NaN
Compared to previous month
Last Year
0%
NaN
Compared to previous year