lib/multiplexer.js
function (config) {
        if (!config) { return; }

        var replConfig,
            peerNames,

            sanitisePeerConfig,
            source = this.sources[config.identity]; // fn

        // at this stage, the source should not be there
        if (source) {
            source.end(); // in case sources exist (almost unlikely, end them)
            console.log('Warn: duplicate setup of connection found in replication setup for ' + config.identity);
        }

        // set default for configuration objects and also clone them
        // ---------------------------------------------------------

        // clone and set defaults for the configuration variables.
        config = util.clone(config); // clone config. do it here to clone replication config too
        replConfig = util.fill(config.replication || {
            enabled: false // blank config means replication disabled
        }, DEFAULT_REPL_SOURCE_CFG); // extract repl config from main config

        !replConfig.sources && (replConfig.sources = {}); // set default to no source if none specified
        delete config.replication; // we remove the recursive config from main config clone.

        // setup a dumb source as a precaution and since we should fall back to original connection if setup fails
        source = this.sources[config.identity] = db.oneDumbSource();

        // replication explicitly disabled or no peers defined. we do this after ending any ongoing sources
        source._replication = !!replConfig.enabled;
        if (source._replication === false) {
            return;
        }

        // create default connection parameters for cluster
        // ------------------------------------------------

        // get the list of peer names defined in sources and setup the config defaults for peer sources
        peerNames = _.filter(Object.keys(replConfig.sources) || [], function (peer) { // remove all disabled peers
            return (peer = replConfig.sources[peer]) && (peer.enabled !== false);
        });

        sanitisePeerConfig = function (peerConfig) {
            return util.fill(util.extend(peerConfig, {
                database: peerConfig._database || config.database, // force db name to be same
                pool: true,
                waitForConnections: true,
                multipleStatements: true
            }), (replConfig.inheritMaster === true) && config);
        };

        // depending upon the number of peers defined, create simple connection or clustered connection
        // --------------------------------------------------------------------------------------------

        // nothing much to do, if there are no replication source defined
        if (peerNames.length === 0) {
            sails && sails.log.info('smt is re-using master as readonly source');
            // if it is marked to inherit master in replication, we simply create a new source out of the master config
            if (replConfig.inheritMaster === true) {
                this.sources[config.identity] = db.createSource(sanitisePeerConfig({}));
            }
            return;
        }

        // for a single peer, the configuration is simple, we do not need a cluster but a simple pool
        if (peerNames.length === 1) {
            sails && sails.log.info('smt is using 1 "' + config.identity + '" readonly source - ' + peerNames[0]);
            // ensure that the single source's config is taken care of by setting the default and if needed inheriting
            // from master
            this.sources[config.identity] = db.createSource(sanitisePeerConfig(replConfig.sources[peerNames[0]]));
            return;
        }

        // iterate over all sources and normalise and validate their configuration
        util.each(replConfig.sources, sanitisePeerConfig);
        sails && sails.log.info('smt is using ' + peerNames.length + ' "' + config.identity + '" readonly sources - ' +
            peerNames.join(', '));

        // create connections for read-replicas and add it to the peering list
        this.sources[config.identity] = db.createCluster(replConfig);
    }
function (config) {
        if (!config) { return; }

        var replConfig,
            peerNames,

            sanitisePeerConfig,
            source = this.sources[config.identity]; // fn

        // at this stage, the source should not be there
        if (source) {
            source.end(); // in case sources exist (almost unlikely, end them)
            console.log('Warn: duplicate setup of connection found in replication setup for ' + config.identity);
        }

        // set default for configuration objects and also clone them
        // ---------------------------------------------------------

        // clone and set defaults for the configuration variables.
        config = util.clone(config); // clone config. do it here to clone replication config too
        replConfig = util.fill(config.replication || {
            enabled: false // blank config means replication disabled
        }, DEFAULT_REPL_SOURCE_CFG); // extract repl config from main config

        !replConfig.sources && (replConfig.sources = {}); // set default to no source if none specified
        delete config.replication; // we remove the recursive config from main config clone.

        // setup a dumb source as a precaution and since we should fall back to original connection if setup fails
        source = this.sources[config.identity] = db.oneDumbSource();

        // replication explicitly disabled or no peers defined. we do this after ending any ongoing sources
        source._replication = !!replConfig.enabled;
        if (source._replication === false) {
            return;
        }

        // create default connection parameters for cluster
        // ------------------------------------------------

        // get the list of peer names defined in sources and setup the config defaults for peer sources
        peerNames = _.filter(Object.keys(replConfig.sources) || [], function (peer) { // remove all disabled peers
            return (peer = replConfig.sources[peer]) && (peer.enabled !== false);
        });

        sanitisePeerConfig = function (peerConfig) {
            return util.fill(util.extend(peerConfig, {
                database: peerConfig._database || config.database, // force db name to be same
                pool: true,
                waitForConnections: true,
                multipleStatements: true
            }), (replConfig.inheritMaster === true) && config);
        };

        // depending upon the number of peers defined, create simple connection or clustered connection
        // --------------------------------------------------------------------------------------------

        // nothing much to do, if there are no replication source defined
        if (peerNames.length === 0) {
            sails && sails.log.info('smt is re-using master as readonly source');
            // if it is marked to inherit master in replication, we simply create a new source out of the master config
            if (replConfig.inheritMaster === true) {
                this.sources[config.identity] = db.createSource(sanitisePeerConfig({}));
            }
            return;
        }

        // for a single peer, the configuration is simple, we do not need a cluster but a simple pool
        if (peerNames.length === 1) {
            sails && sails.log.info('smt is using 1 "' + config.identity + '" readonly source - ' + peerNames[0]);
            // ensure that the single source's config is taken care of by setting the default and if needed inheriting
            // from master
            this.sources[config.identity] = db.createSource(sanitisePeerConfig(replConfig.sources[peerNames[0]]));
            return;
        }

        // iterate over all sources and normalise and validate their configuration
        util.each(replConfig.sources, sanitisePeerConfig);
        sails && sails.log.info('smt is using ' + peerNames.length + ' "' + config.identity + '" readonly sources - ' +
            peerNames.join(', '));

        // create connections for read-replicas and add it to the peering list
        this.sources[config.identity] = db.createCluster(replConfig);
    }
function ( config ) { if ( ! config ) { return ; } var replConfig , peerNames , sanitisePeerConfig , source = this . sources [ config . identity ] ; // fn // at this stage, the source should not be there if ( source ) { source . end ( ) ; // in case sources exist (almost unlikely, end them) console . log ( 'Warn: duplicate setup of connection found in replication setup for ' + config . identity ) ; } // set default for configuration objects and also clone them // --------------------------------------------------------- // clone and set defaults for the configuration variables. config = util . clone ( config ) ; // clone config. do it here to clone replication config too replConfig = util . fill ( config . replication || { enabled : false // blank config means replication disabled } , DEFAULT_REPL_SOURCE_CFG ) ; // extract repl config from main config ! replConfig . sources && ( replConfig . sources = { } ) ; // set default to no source if none specified delete config . replication ; // we remove the recursive config from main config clone. // setup a dumb source as a precaution and since we should fall back to original connection if setup fails source = this . sources [ config . identity ] = db . oneDumbSource ( ) ; // replication explicitly disabled or no peers defined. we do this after ending any ongoing sources source . _replication = ! ! replConfig . enabled ; if ( source . _replication === false ) { return ; } // create default connection parameters for cluster // ------------------------------------------------ // get the list of peer names defined in sources and setup the config defaults for peer sources peerNames = _ . filter ( Object . keys ( replConfig . sources ) || [ ] , function ( peer ) { // remove all disabled peers return ( peer = replConfig . sources [ peer ] ) && ( peer . enabled !== false ) ; } ) ; sanitisePeerConfig = function ( peerConfig ) { return util . fill ( util . extend ( peerConfig , { database : peerConfig . _database || config . database , // force db name to be same pool : true , waitForConnections : true , multipleStatements : true } ) , ( replConfig . inheritMaster === true ) && config ) ; } ; // depending upon the number of peers defined, create simple connection or clustered connection // -------------------------------------------------------------------------------------------- // nothing much to do, if there are no replication source defined if ( peerNames . length === 0 ) { sails && sails . log . info ( 'smt is re-using master as readonly source' ) ; // if it is marked to inherit master in replication, we simply create a new source out of the master config if ( replConfig . inheritMaster === true ) { this . sources [ config . identity ] = db . createSource ( sanitisePeerConfig ( { } ) ) ; } return ; } // for a single peer, the configuration is simple, we do not need a cluster but a simple pool if ( peerNames . length === 1 ) { sails && sails . log . info ( 'smt is using 1 "' + config . identity + '" readonly source - ' + peerNames [ 0 ] ) ; // ensure that the single source's config is taken care of by setting the default and if needed inheriting // from master this . sources [ config . identity ] = db . createSource ( sanitisePeerConfig ( replConfig . sources [ peerNames [ 0 ] ] ) ) ; return ; } // iterate over all sources and normalise and validate their configuration util . each ( replConfig . sources , sanitisePeerConfig ) ; sails && sails . log . info ( 'smt is using ' + peerNames . length + ' "' + config . identity + '" readonly sources - ' + peerNames . join ( ', ' ) ) ; // create connections for read-replicas and add it to the peering list this . sources [ config . identity ] = db . createCluster ( replConfig ) ; }
Creates ORM setup sequencing for all replica set pools.
@param  {object} config
Creates ORM setup sequencing for all replica set pools .