<commit_before>'use strict';

var _ = require('lodash'),
    mongo = require('mongodb').MongoClient,
    printf = require('util').format,
    random = require('./random');

var defaults = {
  host: 'localhost',
  port: 27017,
  prefix: null,
  minSuffix : 5,
  minId: 0,
  minPasswordLength: 32,
  dbName : null,
  userName: null,
  userPassword: <PASSWORD>,
  userRole: 'dbOwner',
  authDb : 'admin'
};

function makeName(options){
  var parts = [random.number(options.minSuffix, options.minId)];
  if(options.prefix)
    parts.unshift(options.prefix, '_');
  return parts.join('');
}

function createConfig(options){
  var cfg = _.clone(options);
  cfg.dbName = options.dbName || makeName(options);
  cfg.userName = options.userName || cfg.dbName;
  cfg.userPassword = options.userPassword || random.passwd(options.minPasswordLength);
  return cfg;
}

function buildConnectionString(user, pw, options){
    if(user && pw){ //connect with a username and password
        var format = "mongodb://%s:%s@%s:%s/%s?authSource=%s";
        return printf(format, user, pw, options.host, options.port, options.dbName, options.authDb);
    }
    else{
        //connect without a username and password
        var format = "mongodb://%s:%s/%s?authSource=%s";
        return printf(format, options.host, options.port, options.dbName);
    }
}

function provision(user, pw, options, cb){
  if(_.isFunction(options)){
    cb = options;
    options = {};
  }
  options = _.defaults(options || {}, defaults);
  var config = createConfig(options);

  var mongoUrl = buildConnectionString(user, pw, config);
  mongo.connect(mongoUrl, function(e, db){
    if(e) return cb(e);
    db.command({usersInfo: 1}, function(err, result){ //make sure the database wasn't already created
        if (result.users.length===0){ // no users are in the database, can assume it is new
            db.addUser(config.userName, config.userPassword, {role: options.userRole}, function(err, res){
                if(err) return cb(err);
                    return cb(null, config);
            });
        }
        else return cb(new Error('A database with this name already exists'));
    })
  });
}

module.exports = provision;
<commit_msg>Added allowExistingDb capability<commit_after>'use strict';

var _ = require('lodash'),
    mongo = require('mongodb').MongoClient,
    printf = require('util').format,
    random = require('./random');

var defaults = {
  host: 'localhost',
  port: 27017,
  prefix: null,
  minSuffix : 5,
  minId: 0,
  minPasswordLength: 32,
  dbName : null,
  userName: null,
  userPassword: <PASSWORD>,
  userRole: 'dbOwner',
  authDb : 'admin',
  allowExistingDb : false //allow connecting to an existing Database
};

function makeName(options){
  var parts = [random.number(options.minSuffix, options.minId)];
  if(options.prefix)
    parts.unshift(options.prefix, '_');
  return parts.join('');
}

function createConfig(options){
  var cfg = _.clone(options);
  cfg.dbName = options.dbName || makeName(options);
  cfg.userName = options.userName || makeName(options);
  cfg.userPassword = options.userPassword || random.passwd(options.minPasswordLength);
  return cfg;
}

function buildConnectionString(user, pw, options){
    if(user && pw){ //connect with a username and password
        var format = "mongodb://%s:%s@%s:%s/%s?authSource=%s";
        return printf(format, user, pw, options.host, options.port, options.dbName, options.authDb);
    }
    else{
        //connect without a username and password
        var format = "mongodb://%s:%s/%s?authSource=%s";
        return printf(format, options.host, options.port, options.dbName);
    }
}

function provision(user, pw, options, cb){
  if(_.isFunction(options)){
    cb = options;
    options = {};
  }
  options = _.defaults(options || {}, defaults);
  var config = createConfig(options);

  var mongoUrl = buildConnectionString(user, pw, config);
  mongo.connect(mongoUrl, function(e, db){
    if(e) return cb(e);
    db.command({usersInfo: 1}, function(err, result){ //make sure the database wasn't already created
        if (result.users.length===0){ // no users are in the database, can assume it is new
            db.addUser(config.userName, config.userPassword, {role: options.userRole}, function(err, res){
                if(err) return cb(err);
                    return cb(null, config);
            });
        }
        else if (options.allowExistingDb) {
            db.addUser(config.userName, config.userPassword, {role: options.userRole}, function(err, res){
                if(err) return cb(err);
                return cb(null, config);
            });
        }
        else return cb(new Error('A database with this name already exists and the \'allowExistingDb\' option is false'));
    })
  });
}

module.exports = provision;

{'fraction_non_alphanumeric': 0.1440149625935162, 'fraction_numerical': 0.00457190357439734, 'mean_word_length': 3.266843971631206, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '169873', 'n_tokens_mistral': 1505, 'n_tokens_neox': 1465, 'n_words': 375}