Skip to content

Commit

Permalink
Backport empty bulkwrite fix #13684 to Mongoose v6
Browse files Browse the repository at this point in the history
  • Loading branch information
JavaScriptBach committed Aug 4, 2023
1 parent be5b7d5 commit c520587
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
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)

Check failure on line 3776 in lib/model.js

View workflow job for this annotation

GitHub Actions / Lint JS-Files

Missing semicolon
} 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
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 c520587

Please sign in to comment.