Get unlimited public & private packages + package-based permissions with npm Pro.Get started »

wait4mongodb

1.1.0 • Public • Published

wait4MongoDB

Wait For MongoDB is a little NodeJS module that will execute a task as soon MongoDB is ready. Very useful for multi container environments like Docker Compose.

Installing

Execute the next command line for to install the module using npm.

npm install wait4mongodb --save

Running the tests

Run the tests using the next command.

npm test

Usage

Parameters

wait4mongodb.tryConnect(url, times, interval, [options], [callback]);
  • url: The connection URI string
  • times: How many times will try to connect.
  • interval: Time in milliseconds between retries.
  • options (optional): MongoClient settings. See MongoClient documentation for more info.
  • callback (optional): Callback function.

Using callbacks

const wait4mongodb = require('wait4mongodb');
 
/* The next example will try to connect to MongoDB 20 times (every 500 milliseconds) 
 *   and will return a timeout (if MongoDB is down) after 10 sec. 
 */
wait4mongodb.tryConnect('mongodb://localhost:27017', 20, 500, {poolSize: 3}, (err, client) => {
  if (err) {
    console.log('MongoDB timeout');
  } else {
    console.log('MongoDB is ready!');
    // You will have available MongoDB client from here.
  }
});

Using Promises

const wait4mongodb = require('wait4mongodb');
 
/* The next example will try to connect to MongoDB 20 times (every 500 milliseconds) 
 *   and will return a timeout (if MongoDB is down) after 10 sec. 
 */
wait4mongodb.tryConnect('mongodb://localhost:27017', 20, 500, {poolSize: 3}).then(client => {
  console.log('MongoDB is ready!');
  // You will have available MongoDB client from here.
}).catch(err => {
  console.log('MongoDB timeout');
});

Authors

  • Jose Antonio - Initial work

Donations

If you found this useful. Please, consider support with a small donation:

  • BTC - 1PPn4qvCQ1gRGFsFnpkufQAZHhJRoGo2g5
  • BCH - qr66rzdwlcpefqemkywmfze9pf80kwue0v2gsfxr9m
  • ETH - 0x5022cf2945604CDE2887068EE46608ed6B57cED8

License

This project is licensed under the ISC License - see the LICENSE file for details

Acknowledgments

Install

npm i wait4mongodb

DownloadsWeekly Downloads

0

Version

1.1.0

License

ISC

Unpacked Size

10.7 kB

Total Files

5

Last publish

Collaborators

  • avatar