Skip to content

Commit

Permalink
Chunk inserts so that you can insert more than 1000 items
Browse files Browse the repository at this point in the history
  • Loading branch information
notheotherben committed Jun 2, 2015
1 parent b65a913 commit 690f96c
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
4 changes: 2 additions & 2 deletions lib/Model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -461,14 +461,14 @@ class Model<TDocument extends { _id?: any }, TInstance> implements ModelInterfac
});
}
else
return this._handlers.creatingDocuments(objects).then((objects) => {
return this._handlers.creatingDocuments(objects).then(objects => _.chunk(objects, 1000)).map((objects: any[]) => {
return new Bluebird<any[]>((resolve, reject) => {
this.collection.insertMany(objects, queryOptions,(err, result) => {
if (err) return reject(err);
return resolve(result.ops);
});
});
});
}).then(results => _.flatten(results));
}).map((inserted: any) => {
return this._handlers.documentReceived(null, inserted,(document, isNew?, isPartial?) => this._helpers.wrapDocument(document, isNew, isPartial), { cache: options.cache });
}).then((results: TInstance[]) => {
Expand Down
11 changes: 11 additions & 0 deletions typings/DefinitelyTyped/lodash/lodash.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,17 @@ declare module _ {
**/
compact(): LoDashArrayWrapper<T>;
}

//_.chunk
interface LoDashStatic {
/**
* Creates an array of elements split into groups the length of size. If collection can’t be split evenly, the final chunk will be the remaining elements.
* @param array The array to process.
* @param size The length of each chunk.
* @return Returns the new array containing chunks.
*/
chunk<T>(array: T[], size?: number): T[][];
}

//_.difference
interface LoDashStatic {
Expand Down

0 comments on commit 690f96c

Please sign in to comment.