Skip to content

Commit

Permalink
DEV: Added method Collection#bulkInsert
Browse files Browse the repository at this point in the history
  • Loading branch information
eastolfi committed Aug 4, 2016
1 parent 80e0826 commit 8578900
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 1 deletion.
45 changes: 44 additions & 1 deletion lib/Collection.js

Large diffs are not rendered by default.

43 changes: 43 additions & 0 deletions src/Collection.js
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,49 @@ Collection.prototype.insert = function (doc, options, callback) {
return _doc;
};

/**
* Inserts several documents into the collection
*
* @method Collection#bulkInsert
*
* @param {Array} docs - Documents to be inserted
* @param {Object} [options] - Additional options
*
* @param {Boolean} [options.chain=false] - If set to "true" returns this instance, so it can be chained with other methods
*
* @param {Function} [callback=null] Callback function to be called at the end with the results
*
* @returns {Object|Collection} If "options.chain" set to "true" returns this instance, otherwise returns the inserted document
*/
Collection.prototype.bulkInsert = function (docs, options, callback) {
if (_.isNil(docs)) logger.throw("docs parameter required");

if (!_.isArray(docs)) logger.throw("docs must be an array");

if (_.isNil(options)) options = {};

if (_.isFunction(options)) {
callback = options;
options = {};
}

if (!_.isNil(callback) && !_.isFunction(callback)) logger.throw("callback must be a function");

var _docs = [];

for (let i = 0; i < docs.length; i++) {
let doc = docs[i];

_docs.push(this.insert(doc, options));
}

if (callback) callback(null, _docs);

if (options.chain) return this;

return _docs;
};

/**
* Finds all matching documents
*
Expand Down

0 comments on commit 8578900

Please sign in to comment.