lib/mongo.js
function connect(obj, cb) {
    if (obj.db) {
        return cb();
    }
    if (obj.pending) {
        return setImmediate(function () {
            connect(obj, cb);
        });
    }
    obj.pending = true;

    var url = constructMongoLink(obj.config.name, obj.config.prefix, obj.config.servers, obj.config.URLParam, obj.config.credentials);
    if (!url) {
        return cb(generateError(190));
    }

    mongoSkin.connect(url, obj.config.extraParam, function (err, db) {
        if (err) {
            obj.pending = false;
            return cb(err);
        } else {
            db.on('timeout', function () {
                console.log("Connection To Mongo has timed out!");
                obj.flushDb();
            });
            db.on('close', function () {
                console.log("Connection To Mongo has been closed!");
                obj.flushDb();
            });
            if (obj.db)
                obj.db.close();

            obj.db = db;
            obj.pending = false;
            return cb();
        }
    });

    /**
     *constructMongoLink: is a function that takes the below param and return the URL need to by mongoskin.connect
     *
     * @param dbName
     * @param prefix
     * @param servers
     * @param params
     * @param credentials
     * @returns {*}
     */
    function constructMongoLink(dbName, prefix, servers, params, credentials) {
        if (dbName && Array.isArray(servers)) {
            var url = "mongodb://";
            if (credentials && Object.hasOwnProperty.call(credentials, 'username') && credentials.hasOwnProperty.call(credentials, 'password')) {
                url = url.concat(credentials.username, ':', credentials.password, '@');
            }

            servers.forEach(function (element, index, array) {
                url = url.concat(element.host, ':', element.port, (index === array.length - 1 ? '' : ','));
            });

            url = url.concat('/');
            if (prefix) url = url.concat(prefix);
            url = url.concat(dbName);

            if (params && 'object' === typeof params && Object.keys(params).length) {
                url = url.concat('?');
                for (var i = 0; i < Object.keys(params).length; i++) {
                    url = url.concat(Object.keys(params)[i], '=', params[Object.keys(params)[i]], i === Object.keys(params).length - 1 ? '' : "&");
                }
            }
            return url;
        }
        return null;
    }
}
function connect(obj, cb) {
    if (obj.db) {
        return cb();
    }
    if (obj.pending) {
        return setImmediate(function () {
            connect(obj, cb);
        });
    }
    obj.pending = true;

    var url = constructMongoLink(obj.config.name, obj.config.prefix, obj.config.servers, obj.config.URLParam, obj.config.credentials);
    if (!url) {
        return cb(generateError(190));
    }

    mongoSkin.connect(url, obj.config.extraParam, function (err, db) {
        if (err) {
            obj.pending = false;
            return cb(err);
        } else {
            db.on('timeout', function () {
                console.log("Connection To Mongo has timed out!");
                obj.flushDb();
            });
            db.on('close', function () {
                console.log("Connection To Mongo has been closed!");
                obj.flushDb();
            });
            if (obj.db)
                obj.db.close();

            obj.db = db;
            obj.pending = false;
            return cb();
        }
    });

    /**
     *constructMongoLink: is a function that takes the below param and return the URL need to by mongoskin.connect
     *
     * @param dbName
     * @param prefix
     * @param servers
     * @param params
     * @param credentials
     * @returns {*}
     */
    function constructMongoLink(dbName, prefix, servers, params, credentials) {
        if (dbName && Array.isArray(servers)) {
            var url = "mongodb://";
            if (credentials && Object.hasOwnProperty.call(credentials, 'username') && credentials.hasOwnProperty.call(credentials, 'password')) {
                url = url.concat(credentials.username, ':', credentials.password, '@');
            }

            servers.forEach(function (element, index, array) {
                url = url.concat(element.host, ':', element.port, (index === array.length - 1 ? '' : ','));
            });

            url = url.concat('/');
            if (prefix) url = url.concat(prefix);
            url = url.concat(dbName);

            if (params && 'object' === typeof params && Object.keys(params).length) {
                url = url.concat('?');
                for (var i = 0; i < Object.keys(params).length; i++) {
                    url = url.concat(Object.keys(params)[i], '=', params[Object.keys(params)[i]], i === Object.keys(params).length - 1 ? '' : "&");
                }
            }
            return url;
        }
        return null;
    }
}
function connect ( obj , cb ) { if ( obj . db ) { return cb ( ) ; } if ( obj . pending ) { return setImmediate ( function ( ) { connect ( obj , cb ) ; } ) ; } obj . pending = true ; var url = constructMongoLink ( obj . config . name , obj . config . prefix , obj . config . servers , obj . config . URLParam , obj . config . credentials ) ; if ( ! url ) { return cb ( generateError ( 190 ) ) ; } mongoSkin . connect ( url , obj . config . extraParam , function ( err , db ) { if ( err ) { obj . pending = false ; return cb ( err ) ; } else { db . on ( 'timeout' , function ( ) { console . log ( "Connection To Mongo has timed out!" ) ; obj . flushDb ( ) ; } ) ; db . on ( 'close' , function ( ) { console . log ( "Connection To Mongo has been closed!" ) ; obj . flushDb ( ) ; } ) ; if ( obj . db ) obj . db . close ( ) ; obj . db = db ; obj . pending = false ; return cb ( ) ; } } ) ; /**
     *constructMongoLink: is a function that takes the below param and return the URL need to by mongoskin.connect
     *
     * @param dbName
     * @param prefix
     * @param servers
     * @param params
     * @param credentials
     * @returns {*}
     */ function constructMongoLink ( dbName , prefix , servers , params , credentials ) { if ( dbName && Array . isArray ( servers ) ) { var url = "mongodb://" ; if ( credentials && Object . hasOwnProperty . call ( credentials , 'username' ) && credentials . hasOwnProperty . call ( credentials , 'password' ) ) { url = url . concat ( credentials . username , ':' , credentials . password , '@' ) ; } servers . forEach ( function ( element , index , array ) { url = url . concat ( element . host , ':' , element . port , ( index === array . length - 1 ? '' : ',' ) ) ; } ) ; url = url . concat ( '/' ) ; if ( prefix ) url = url . concat ( prefix ) ; url = url . concat ( dbName ) ; if ( params && 'object' === typeof params && Object . keys ( params ) . length ) { url = url . concat ( '?' ) ; for ( var i = 0 ; i < Object . keys ( params ) . length ; i ++ ) { url = url . concat ( Object . keys ( params ) [ i ] , '=' , params [ Object . keys ( params ) [ i ] ] , i === Object . keys ( params ) . length - 1 ? '' : "&" ) ; } } return url ; } return null ; } }
Ensure a connection to mongo without any race condition problem

@param {Object} obj
@param {Function} cb
@returns {*}
Ensure a connection to mongo without any race condition problem