connect-once
Connect once and memorize connection for next usages
Usage
Install package with npm:
npm i connect-once --save
Then create a connection:
var connectOnce = require('connect-once');
var connection = new connectOnce({
retries: 60,
reconnectWait: 1000
}, MongoClient.connect, 'mongodb://localhost/test');
connection.when('available', function (err, db) {
// Do stuff
});
Heartbeat
Heartbeat is useful, when you want check connection from time to time. For example, if you working with mongodb and cache connection to db - what happens when server, which connection binded to goes to heaven? Connection is lost and programmer should recreate it.
Note: for now there is no way to stop heartbeats.
var connectOnce = require('connect-once');
var connection = new connectOnce({
heartbeat: function (db, beat) {
db.stats(function (err) {
if (!err) { beat(); }
});
}
}, MongoClient.connect, 'mongodb://localhost/test');
connection.when('available', function (err, db) {
// Do stuff
});
API
Read the documentation of Connection class.
License
(MIT License) (c) 2013 Vsevolod Strukchinsky (floatdrop@gmail.com)