diff --git a/lib/Model.ts b/lib/Model.ts index 8f5389e..14e52fe 100644 --- a/lib/Model.ts +++ b/lib/Model.ts @@ -461,14 +461,14 @@ class Model 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((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[]) => { diff --git a/typings/DefinitelyTyped/lodash/lodash.d.ts b/typings/DefinitelyTyped/lodash/lodash.d.ts index c43f27c..0a9fb3d 100644 --- a/typings/DefinitelyTyped/lodash/lodash.d.ts +++ b/typings/DefinitelyTyped/lodash/lodash.d.ts @@ -277,6 +277,17 @@ declare module _ { **/ compact(): LoDashArrayWrapper; } + + //_.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(array: T[], size?: number): T[][]; + } //_.difference interface LoDashStatic {