lib/middleware/envMongoDb.js
function getOrCreateEnvironmentDatabase(req, res, next){

  var models = mbaas.getModels();

  log.logger.debug('process getOrCreateEnvironmentDatabase request', req.originalUrl , req.body, req.method, req.params);

  var domain = req.params.domain;
  var env = req.params.environment;
  log.logger.debug('process db create request', {domain: domain, env: env} );

  _getEnvironmentDatabase({
    domain: domain,
    environment: env
  }, function(err, found){
    if(err){
      return next(common.buildErrorObject({
        err: err,
        msg: 'Failed to get mbaas instance',
        httpCode: 500
      }));
    }

    log.logger.debug('process db create request AFTER', {domain: domain, env: env} );

    if(found){
      req.mongoUrl = common.formatDbUri(found.dbConf);
      return next();
    } else {
      // because of the composite unique index on the collection, only the first creation call will succeed.
      // NB the req.params should have the mongo.host and mongo.port set !!
      var cfg = config.getConfig();

      models.Mbaas.createModel(domain, env, cfg, function(err, created){
        if(err){
          return next(common.buildErrorObject({
            err: err,
            msg: 'Failed to create mbaas instance',
            httpCode: 500
          }));
        }
        created.createDb(cfg, function(err, dbConf){
          if(err){
            log.logger.error('Failed to create db, delete model');
            created.remove(function(removeErr){
              if(removeErr){
                log.logger.error(removeErr, 'Failed to remove model');
              }
              return next(common.buildErrorObject({
                err: err,
                msg: 'Failed to create db for domain ' + domain + ' and env ' + env,
                httpCode: 500
              }));
            });
          } else {
            req.mongoUrl = common.formatDbUri(dbConf);
            next();
          }
        });
      });
    }
  });
}
function getOrCreateEnvironmentDatabase(req, res, next){

  var models = mbaas.getModels();

  log.logger.debug('process getOrCreateEnvironmentDatabase request', req.originalUrl , req.body, req.method, req.params);

  var domain = req.params.domain;
  var env = req.params.environment;
  log.logger.debug('process db create request', {domain: domain, env: env} );

  _getEnvironmentDatabase({
    domain: domain,
    environment: env
  }, function(err, found){
    if(err){
      return next(common.buildErrorObject({
        err: err,
        msg: 'Failed to get mbaas instance',
        httpCode: 500
      }));
    }

    log.logger.debug('process db create request AFTER', {domain: domain, env: env} );

    if(found){
      req.mongoUrl = common.formatDbUri(found.dbConf);
      return next();
    } else {
      // because of the composite unique index on the collection, only the first creation call will succeed.
      // NB the req.params should have the mongo.host and mongo.port set !!
      var cfg = config.getConfig();

      models.Mbaas.createModel(domain, env, cfg, function(err, created){
        if(err){
          return next(common.buildErrorObject({
            err: err,
            msg: 'Failed to create mbaas instance',
            httpCode: 500
          }));
        }
        created.createDb(cfg, function(err, dbConf){
          if(err){
            log.logger.error('Failed to create db, delete model');
            created.remove(function(removeErr){
              if(removeErr){
                log.logger.error(removeErr, 'Failed to remove model');
              }
              return next(common.buildErrorObject({
                err: err,
                msg: 'Failed to create db for domain ' + domain + ' and env ' + env,
                httpCode: 500
              }));
            });
          } else {
            req.mongoUrl = common.formatDbUri(dbConf);
            next();
          }
        });
      });
    }
  });
}
function getOrCreateEnvironmentDatabase ( req , res , next ) { var models = mbaas . getModels ( ) ; log . logger . debug ( 'process getOrCreateEnvironmentDatabase request' , req . originalUrl , req . body , req . method , req . params ) ; var domain = req . params . domain ; var env = req . params . environment ; log . logger . debug ( 'process db create request' , { domain : domain , env : env } ) ; _getEnvironmentDatabase ( { domain : domain , environment : env } , function ( err , found ) { if ( err ) { return next ( common . buildErrorObject ( { err : err , msg : 'Failed to get mbaas instance' , httpCode : 500 } ) ) ; } log . logger . debug ( 'process db create request AFTER' , { domain : domain , env : env } ) ; if ( found ) { req . mongoUrl = common . formatDbUri ( found . dbConf ) ; return next ( ) ; } else { // because of the composite unique index on the collection, only the first creation call will succeed. // NB the req.params should have the mongo.host and mongo.port set !! var cfg = config . getConfig ( ) ; models . Mbaas . createModel ( domain , env , cfg , function ( err , created ) { if ( err ) { return next ( common . buildErrorObject ( { err : err , msg : 'Failed to create mbaas instance' , httpCode : 500 } ) ) ; } created . createDb ( cfg , function ( err , dbConf ) { if ( err ) { log . logger . error ( 'Failed to create db, delete model' ) ; created . remove ( function ( removeErr ) { if ( removeErr ) { log . logger . error ( removeErr , 'Failed to remove model' ) ; } return next ( common . buildErrorObject ( { err : err , msg : 'Failed to create db for domain ' + domain + ' and env ' + env , httpCode : 500 } ) ) ; } ) ; } else { req . mongoUrl = common . formatDbUri ( dbConf ) ; next ( ) ; } } ) ; } ) ; } } ) ; }
Function that will return a mongo
@param req
@param res
@param next
Function that will return a mongo