<commit_before>'use strict';

const MongoDb = require('mongodb').Db;
const Promise = require('bluebird');

const mongoUtils = require('src/lib/utils/mongoUtils');
const errors = require('lib/errors');

const DEFAULT_PAGE_SIZE = 50;

/**
 * @class Collection
 */
class Collection {
  /**
   * @constructor Collection
   *
   * @param {Object} database - MongoDb object
   * @param {Object} options
   * @param {String} options.name - name of the collection
   * @param {String} options.serverName - name of the server
   * @param {String} options.databaseName - name of the database
   */
  constructor(database, options) {
    if (!(database instanceof MongoDb)) console.error('Collection ctor - database is not an instance of MongoDb');

    options = options || {};

    var _this = this;
    _this.id = options.id;
    _this.name = options.name;
    _this.connection = options.connection;
    _this.database = options.database;
    _this.serverName = options.serverName;
    _this.databaseName = options.databaseName;
    _this._dbCollection = database.collection(_this.name);
  }

  /**
   * @method insertOne
   * @param {Object} doc
   */
  insertOne(doc) {
    var _this = this;

    return new Promise(function(resolve, reject) {
      _this._dbCollection.insertOne(doc, function(err) {
        if (err) return reject(err);
        return resolve(null);
      });
    });
  }

  /**
   * @method find
   * @param {Object} query - mongo query
   * @param {Object} [options] - mongo query options
   */
  find(query, options) {
    var _this = this;
    options = options || {};

    return new Promise(function(resolve, reject) {
      if (!query) return reject(new errors.InvalidArugmentError('query is required'));

      //TODO: validate the query??

      var dbQuery = _this._dbCollection.find(query, options);

      if (options.skip) dbQuery.skip(Number(options.skip));

      dbQuery.limit(options.limit ? options.limit : DEFAULT_PAGE_SIZE);

      if (options.stream === true) {
        return resolve(dbQuery.stream());
      } else {
        dbQuery.toArray(function(err, docs) {
          if (err) return reject(err);
          return resolve(docs);
        });
      }
    });
  }

  /**
   * @method deleteMany
   * @param {Object} query - mongo query
   * @param {Object} [options] - mongo query options
   */
  deleteMany(query, options) {
    var _this = this;
    options = options || {};

    return new Promise(function(resolve, reject) {
      if (!query) return reject(new errors.InvalidArugmentError('query is required'));

      _this._dbCollection.deleteMany(query, options, function(err) {
        if (err) return reject(err);
        return resolve(null);
      });
    });
  }

  /**
   * @method deleteById
   * @param {Object} Mongo ObjectId
   */
  deleteById(objectId) {
    var _this = this;

    return new Promise(function(resolve, reject) {
      if (!objectId) return reject(new errors.InvalidArugmentError('id is required'));

      _this._dbCollection.deleteOne({
        _id: objectId
      }, function(err) {
        if (err) return reject(err);
        return resolve(null);
      });
    });
  }

  /**
   * @method deleteOne
   * @param {Object} query - mongo query
   */
  deleteOne(query) {
    var _this = this;

    return new Promise(function(resolve, reject) {
      if (!query) return reject(new errors.InvalidArugmentError('query is required'));

      _this._dbCollection.deleteOne(query, function(err) {
        if (err) return reject(err);
        return resolve(null);
      });
    });
  }

  /**
   * @method aggregate
   * @param {Object} query - mongo query
   */
  aggregate(query, options) {
    var _this = this;
    options = options || {};

    return new Promise(function(resolve, reject) {
      if (!query) return reject(new errors.InvalidArugmentError('query is required'));

      options.limit = 50;

      //TODO: validate the query??

      _this._dbCollection.aggregate(query, options).toArray(function(err, result) {
        if (err) return reject(err);
        return resolve(result);
      });
    });
  }

  /**
   * @method updateMany
   * @param {Object} query - mongo query
   * @param {Object} updates - updates to apply
   * @param {Object} [options] - mongo query options
   */
  updateMany(query, updates, options) {
    var _this = this;
    options = options || {};

    return new Promise(function(resolve, reject) {
      if (!query) return reject(new errors.InvalidArugmentError('query is required'));
      if (!updates) return reject(new errors.InvalidArugmentError('updates is required'));

      _this._dbCollection.updateMany(query, updates, options, function(err) {
        if (err) return reject(err);
        return resolve(null);
      });
    });
  }

  /**
   * @method updateById
   * @param {Object} Mongo ObjectId
   * @param {Object} updates - updates to apply
   */
  updateById(objectId, updates) {
    var _this = this;

    return new Promise(function(resolve, reject) {
      if (!objectId) return reject(new errors.InvalidArugmentError('id is required'));
      if (!updates) return reject(new errors.InvalidArugmentError('updates is required'));
      if (!mongoUtils.isObjectId(objectId)) return reject(new errors.InvalidArugmentError('objectId must be an instance of ObjectId'));

      _this._dbCollection.updateOne({
        _id: objectId
      }, updates, function(err) {
        if (err) return reject(err);
        return resolve(null);
      });
    });
  }

  /**
   * @method updateOne
   * @param {Object} query - mongo query
   * @param {Object} updates - updates to apply
   */
  updateOne(query, updates) {
    var _this = this;

    return new Promise(function(resolve, reject) {
      if (!query) return reject(new errors.InvalidArugmentError('query is required'));
      if (!updates) return reject(new errors.InvalidArugmentError('updates is required'));

      _this._dbCollection.updateOne(query, updates, function(err) {
        if (err) return reject(err);
        return resolve(null);
      });
    });
  }

  /**
   * @method drop
   */
  drop() {
    var _this = this;

    return new Promise(function(resolve, reject) {
      _this._dbCollection.drop(function(err) {
        if (err) return reject(err);
        return resolve(null);
      });
    });
  }
}

/**
 * @exports
 */
module.exports = Collection;
<commit_msg>delete stream from options, otherwise it’s treated as a search query<commit_after>'use strict';

const MongoDb = require('mongodb').Db;
const Promise = require('bluebird');

const mongoUtils = require('src/lib/utils/mongoUtils');
const errors = require('lib/errors');

const DEFAULT_PAGE_SIZE = 50;

/**
 * @class Collection
 */
class Collection {
  /**
   * @constructor Collection
   *
   * @param {Object} database - MongoDb object
   * @param {Object} options
   * @param {String} options.name - name of the collection
   * @param {String} options.serverName - name of the server
   * @param {String} options.databaseName - name of the database
   */
  constructor(database, options) {
    if (!(database instanceof MongoDb)) console.error('Collection ctor - database is not an instance of MongoDb');

    options = options || {};

    var _this = this;
    _this.id = options.id;
    _this.name = options.name;
    _this.connection = options.connection;
    _this.database = options.database;
    _this.serverName = options.serverName;
    _this.databaseName = options.databaseName;
    _this._dbCollection = database.collection(_this.name);
  }

  /**
   * @method insertOne
   * @param {Object} doc
   */
  insertOne(doc) {
    var _this = this;

    return new Promise(function(resolve, reject) {
      _this._dbCollection.insertOne(doc, function(err) {
        if (err) return reject(err);
        return resolve(null);
      });
    });
  }

  /**
   * @method find
   * @param {Object} query - mongo query
   * @param {Object} [options] - mongo query options
   */
  find(query, options) {
    var _this = this;
    options = options || {};

    return new Promise(function(resolve, reject) {
      if (!query) return reject(new errors.InvalidArugmentError('query is required'));

      let stream = options.stream;
      delete options.stream;

      let dbQuery = _this._dbCollection.find(query, options);

      if (options.skip) dbQuery.skip(Number(options.skip));

      dbQuery.limit(options.limit ? options.limit : DEFAULT_PAGE_SIZE);

      if (stream === true) {
        return resolve(dbQuery.stream());
      } else {
        dbQuery.toArray(function(err, docs) {
          if (err) return reject(err);
          return resolve(docs);
        });
      }
    });
  }

  /**
   * @method deleteMany
   * @param {Object} query - mongo query
   * @param {Object} [options] - mongo query options
   */
  deleteMany(query, options) {
    var _this = this;
    options = options || {};

    return new Promise(function(resolve, reject) {
      if (!query) return reject(new errors.InvalidArugmentError('query is required'));

      _this._dbCollection.deleteMany(query, options, function(err) {
        if (err) return reject(err);
        return resolve(null);
      });
    });
  }

  /**
   * @method deleteById
   * @param {Object} Mongo ObjectId
   */
  deleteById(objectId) {
    var _this = this;

    return new Promise(function(resolve, reject) {
      if (!objectId) return reject(new errors.InvalidArugmentError('id is required'));

      _this._dbCollection.deleteOne({
        _id: objectId
      }, function(err) {
        if (err) return reject(err);
        return resolve(null);
      });
    });
  }

  /**
   * @method deleteOne
   * @param {Object} query - mongo query
   */
  deleteOne(query) {
    var _this = this;

    return new Promise(function(resolve, reject) {
      if (!query) return reject(new errors.InvalidArugmentError('query is required'));

      _this._dbCollection.deleteOne(query, function(err) {
        if (err) return reject(err);
        return resolve(null);
      });
    });
  }

  /**
   * @method aggregate
   * @param {Object} query - mongo query
   */
  aggregate(query, options) {
    var _this = this;
    options = options || {};

    return new Promise(function(resolve, reject) {
      if (!query) return reject(new errors.InvalidArugmentError('query is required'));

      options.limit = 50;

      //TODO: validate the query??

      _this._dbCollection.aggregate(query, options).toArray(function(err, result) {
        if (err) return reject(err);
        return resolve(result);
      });
    });
  }

  /**
   * @method updateMany
   * @param {Object} query - mongo query
   * @param {Object} updates - updates to apply
   * @param {Object} [options] - mongo query options
   */
  updateMany(query, updates, options) {
    var _this = this;
    options = options || {};

    return new Promise(function(resolve, reject) {
      if (!query) return reject(new errors.InvalidArugmentError('query is required'));
      if (!updates) return reject(new errors.InvalidArugmentError('updates is required'));

      _this._dbCollection.updateMany(query, updates, options, function(err) {
        if (err) return reject(err);
        return resolve(null);
      });
    });
  }

  /**
   * @method updateById
   * @param {Object} Mongo ObjectId
   * @param {Object} updates - updates to apply
   */
  updateById(objectId, updates) {
    var _this = this;

    return new Promise(function(resolve, reject) {
      if (!objectId) return reject(new errors.InvalidArugmentError('id is required'));
      if (!updates) return reject(new errors.InvalidArugmentError('updates is required'));
      if (!mongoUtils.isObjectId(objectId)) return reject(new errors.InvalidArugmentError('objectId must be an instance of ObjectId'));

      _this._dbCollection.updateOne({
        _id: objectId
      }, updates, function(err) {
        if (err) return reject(err);
        return resolve(null);
      });
    });
  }

  /**
   * @method updateOne
   * @param {Object} query - mongo query
   * @param {Object} updates - updates to apply
   */
  updateOne(query, updates) {
    var _this = this;

    return new Promise(function(resolve, reject) {
      if (!query) return reject(new errors.InvalidArugmentError('query is required'));
      if (!updates) return reject(new errors.InvalidArugmentError('updates is required'));

      _this._dbCollection.updateOne(query, updates, function(err) {
        if (err) return reject(err);
        return resolve(null);
      });
    });
  }

  /**
   * @method drop
   */
  drop() {
    var _this = this;

    return new Promise(function(resolve, reject) {
      _this._dbCollection.drop(function(err) {
        if (err) return reject(err);
        return resolve(null);
      });
    });
  }
}

/**
 * @exports
 */
module.exports = Collection;

{'fraction_non_alphanumeric': 0.13249531542785759, 'fraction_numerical': 0.0006246096189881324, 'mean_word_length': 2.8442376950780313, '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': '753267', 'n_tokens_mistral': 3842, 'n_tokens_neox': 3745, 'n_words': 979}