Skip to content

Commit

Permalink
Merge pull request #13701 from JavaScriptBach/backport-bulkwrite
Browse files Browse the repository at this point in the history
Backport empty bulkwrite fix #13684 to Mongoose v6
  • Loading branch information
vkarpov15 authored Aug 6, 2023
2 parents be5b7d5 + 6f5adfb commit d4a1080
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 11 deletions.
26 changes: 15 additions & 11 deletions lib/model.js
Original file line number Diff line number Diff line change
Expand Up @@ -3772,17 +3772,21 @@ Model.bulkWrite = function(ops, options, callback) {
let remaining = validations.length;
let validOps = [];
let validationErrors = [];
for (let i = 0; i < validations.length; ++i) {
validations[i]((err) => {
if (err == null) {
validOps.push(i);
} else {
validationErrors.push({ index: i, error: err });
}
if (--remaining <= 0) {
completeUnorderedValidation.call(this);
}
});
if (remaining === 0) {
completeUnorderedValidation.call(this);
} else {
for (let i = 0; i < validations.length; ++i) {
validations[i]((err) => {
if (err == null) {
validOps.push(i);
} else {
validationErrors.push({ index: i, error: err });
}
if (--remaining <= 0) {
completeUnorderedValidation.call(this);
}
});
}
}

validationErrors = validationErrors.
Expand Down
9 changes: 9 additions & 0 deletions test/model.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7852,6 +7852,15 @@ describe('Model', function() {

});

it('Model.bulkWrite(...) does not hang with empty array and ordered: false (gh-13664)', async function() {
const userSchema = new Schema({ name: String });
const User = db.model('User', userSchema);

const err = await User.bulkWrite([], { ordered: false }).then(() => null, err => err);
assert.ok(err);
assert.equal(err.name, 'MongoInvalidArgumentError');
});

it('allows calling `create()` after `bulkWrite()` (gh-9350)', async function() {
const schema = Schema({ foo: Boolean });
const Model = db.model('Test', schema);
Expand Down

0 comments on commit d4a1080

Please sign in to comment.