<commit_before>
/**
 * Dependencies
 */

var mongodb = require('mongodb')
var slice = require('sliced')
var log = require('./log')
var p = require('./p')

/**
 * Expose
 */

module.exports = exports = create;

function create (db) {
  var DB = createConstructor(db);
  return new DB;
}

/**
 * We create DB constructors dynamically to avoid
 * assigning the db or collections directly to the
 * database instance, thereby avoiding them displaying
 * in autocomplete mixed together with the collection
 * names.
 *
 * @param {Database} db
 * @return {Function}
 */

function createConstructor (db) {
  var collections = [];

  function DB () {
    this.collections();
  }

  /**
   * Drop this database
   */

  DB.prototype.drop = function (cb) {
    var name = db.databaseName;

    db.dropDatabase(cb || function (err, worked) {
      if (err) {
        return console.error(err);
      }
      log('database %s was dropped', name);
    });
  }

  /**
   * Close this database connection
   */

  DB.prototype.close = function (force, cb) {
    var args = slice(arguments);
    var last = args[args.length];

    if ('function' != typeof last) {
      args.push(handleError);
    }

    db.close.apply(db, args);
  }

  /**
   * Use a different database
   */

  DB.prototype.use = function (name) {
    return create(db.db(name));
  }

  /**
   * console.log helper
   */

  DB.prototype.inspect = function () {
    return db.databaseName;
  }

  /**
   * Refresh and return the list of collections on this database
   */

  DB.prototype.collections = function (cb) {
    var self = this;

    db.collectionNames({ namesOnly: true }, function (err, names) {
      if (err) {
        if (cb) return cb(err);
        console.error(err.stack);
        return;
      }<commit_msg>db; add collection access method<commit_after>
/**
 * Dependencies
 */

var mongodb = require('mongodb')
var slice = require('sliced')
var log = require('./log')
var p = require('./p')

/**
 * Expose
 */

module.exports = exports = create;

function create (db) {
  var DB = createConstructor(db);
  return new DB;
}

/**
 * We create DB constructors dynamically to avoid
 * assigning the db or collections directly to the
 * database instance, thereby avoiding them displaying
 * in autocomplete mixed together with the collection
 * names.
 *
 * @param {Database} db
 * @return {Function}
 */

function createConstructor (db) {
  var collections = [];

  function DB () {
    this.collections();
  }

  /**
   * Drop this database
   */

  DB.prototype.drop = function (cb) {
    var name = db.databaseName;

    db.dropDatabase(cb || function (err, worked) {
      if (err) {
        return console.error(err);
      }
      log('database %s was dropped', name);
    });
  }

  /**
   * Close this database connection
   */

  DB.prototype.close = function (force, cb) {
    var args = slice(arguments);
    var last = args[args.length];

    if ('function' != typeof last) {
      args.push(handleError);
    }

    db.close.apply(db, args);
  }

  /**
   * Use a different database
   */

  DB.prototype.use = function (name) {
    return create(db.db(name));
  }

  /**
   * Access a collection
   */

  DB.prototype.collection = function (name, opts) {
    if (this[name]) {
      return this[name];
    }

    return this[name] = db.collection(name, opts);
  }

  /**
   * console.log helper
   */

  DB.prototype.inspect = function () {
    return db.databaseName;
  }

  /**
   * Refresh and return the list of collections on this database
   */

  DB.prototype.collections = function (cb) {
    var self = this;

    db.collectionNames({ namesOnly: true }, function (err, names) {
      if (err) {
        if (cb) return cb(err);
        console.error(err.stack);
        return;
      }
{'fraction_non_alphanumeric': 0.13820492454328834, 'fraction_numerical': 0.0, 'mean_word_length': 2.5341440598690363, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '2784860', 'n_tokens_mistral': 1196, 'n_tokens_neox': 1147, 'n_words': 333}