<commit_before>var Collection = require('./collection')
var mongodb = require('mongodb')
var xtend = require('xtend')
var thunky = require('thunky')
var parse = require('parse-mongo-url')
var util = require('util')
var EventEmitter = require('events').EventEmitter

var noop = function () {}

var Database = function (connString, cols, options) {
  var self = this

  EventEmitter.call(this)

  // TODO: Cleanup theses options - including xtend stuff in index.js
  if (typeof connString === 'string') {
    this._dbname = parse(connString).dbName

    // TODO: Fix - protocol, host and port should come from but should be 'added' in case they are missing - also take rs, mongos in consideration
    if (connString.indexOf('mongodb://') < 0) {
      connString = 'mongodb://localhost/' + connString
    }

    this._getConnection = thunky(function (cb) {
      mongodb.connect(connString, options, function (err, db) {
        if (err) {
          // TODO: Should we really emit error? This requires a listener attached? Only if listeners attached?
          if (self.listeners('error').length > 0) {
            self.emit('error', err)
          }

          return cb(err)
        }

        // TODO: Should we fire a connect event for all other cases too?
        self.emit('connect')
        cb(null, db)
      })
    })
  } else if (typeof connString._getConnection === 'function') { // mongojs
    this._dbname = connString._dbname
    this._getConnection = connString._getConnection
  } else { // try mongodb-native
    // TODO: Make this safe - even for rs connection strings
    this._dbname = parse(connString.options.url).dbName

    this._getConnection = thunky(function (cb) {
      cb(null, connString)
    })
  }

  this.ObjectId = mongodb.ObjectId

  // TODO: Should we leave splitting collection names to mongodb?
  cols = cols || []
  cols.forEach(function (colName) {
    self[colName] = self.collection(colName)

    var parts = colName.split('.')

    var last = parts.pop()
    var parent = parts.reduce(function (parent, prefix) {
      parent[prefix] = parent[prefix] || {}
      return parent[prefix]
    }, self)

    parent[last] = self.collection(colName)
  })
}

util.inherits(Database, EventEmitter)

Database.prototype.collection = function (colName) {
  return new Collection({name: colName}, this._getConnection)
}<commit_msg>Fix connection string building for connection strings omitting mongodb:// prefix or host of db<commit_after>var Collection = require('./collection')
var mongodb = require('mongodb')
var xtend = require('xtend')
var thunky = require('thunky')
var parse = require('parse-mongo-url')
var util = require('util')
var EventEmitter = require('events').EventEmitter

var noop = function () {}

var Database = function (connString, cols, options) {
  var self = this

  EventEmitter.call(this)

  if (typeof connString === 'string') {
    this._dbname = parse(connString).dbName

    // Fix short cut connection URLs consisting only of a db name or db + host
    if (connString.indexOf('/') < 0) {
      connString = 'localhost/' + connString
    }

    if (connString.indexOf('mongodb://') < 0) {
      connString = 'mongodb://' + connString
    }

    this._getConnection = thunky(function (cb) {
      mongodb.connect(connString, options, function (err, db) {
        if (err) {
          self.emit('error', err) // It's safer to emit an error instead of rely on the cb to handle the error
          return cb(err)
        }

        self.emit('connect')
        cb(null, db)
      })
    })
  } else if (typeof connString._getConnection === 'function') { // mongojs
    this._dbname = connString._dbname
    this._getConnection = connString._getConnection
  } else { // try mongodb-native
    this._dbname = parse(connString.options.url).dbName

    this._getConnection = thunky(function (cb) {
      cb(null, connString)
    })
  }

  this.ObjectId = mongodb.ObjectId

  cols = cols || []
  cols.forEach(function (colName) {
    self[colName] = self.collection(colName)

    var parts = colName.split('.')

    var last = parts.pop()
    var parent = parts.reduce(function (parent, prefix) {
      parent[prefix] = parent[prefix] || {}
      return parent[prefix]
    }, self)

    parent[last] = self.collection(colName)
  })
}

util.inherits(Database, EventEmitter)

Database.prototype.collection = function (colName) {
  return new Collection({name: colName}, this._getConnection)
}
{'fraction_non_alphanumeric': 0.12773147105203875, 'fraction_numerical': 0.0009011038522189682, 'mean_word_length': 3.107308048103608, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 4, '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': '2589650', 'n_tokens_mistral': 1352, 'n_tokens_neox': 1310, 'n_words': 368}