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.
Execute the next command line for to install the module using npm.
npm install wait4mongodb --save
Run the tests using the next command.
npm test
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.
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.
}
});
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');
});
- Jose Antonio - Initial work
If you found this useful. Please, consider support with a small donation:
- BTC - 1PPn4qvCQ1gRGFsFnpkufQAZHhJRoGo2g5
- BCH - qr66rzdwlcpefqemkywmfze9pf80kwue0v2gsfxr9m
- ETH - 0x5022cf2945604CDE2887068EE46608ed6B57cED8
This project is licensed under the ISC License - see the LICENSE file for details
- Project inspired on arunoda/wait-for-mongo.