Skip to content

Commit

Permalink
Merge branch 'master' into 5.10
Browse files Browse the repository at this point in the history
  • Loading branch information
vkarpov15 committed Jun 15, 2020
2 parents 7cc8823 + d7f1068 commit a632705
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 3 deletions.
16 changes: 16 additions & 0 deletions History.md
@@ -1,3 +1,19 @@
5.9.19 / 2020-06-15
===================
* fix: upgrade mongodb driver -> 3.5.9 #9124 [AbdelrahmanHafez](https://github.com/AbdelrahmanHafez)
* fix: copy `required` validator on single nested subdoc correctly when calling `Schema#clone()` #8819
* fix(discriminator): handle `tiedValue` when casting update on nested paths #9108
* fix(model): allow empty arrays for bulkWrite #9132 #9131 [AbdelrahmanHafez](https://github.com/AbdelrahmanHafez)
* fix(schema): correctly set partialFilterExpression for nested schema indexes #9091
* fix(castArrayFilters): handle casting on all fields of array filter #9122 [lafeuil](https://github.com/lafeuil)
* fix(update): handle nested path createdAt when overwriting parent path #9105
* docs(subdocs): add some notes on the difference between single nested subdocs and nested paths #9085
* docs(subdocs): improve docs on `typePojoToMixed` #9085
* docs: add note about connections in `globalSetup` with Jest #9063
* docs: add schema and how to set default sub-schema to schematype options #9111 [dfle](https://github.com/dfle)
* docs(index): use `const` instead of `var` in examples #9125 [dmcgrouther](https://github.com/dmcgrouther)
* docs: corrected markdown typo #9117

5.9.18 / 2020-06-05
===================
* fix: improve atlas error in the event of incorrect password #9095
Expand Down
27 changes: 27 additions & 0 deletions lib/helpers/getDefaultBulkwriteResult.js
@@ -0,0 +1,27 @@
'use strict';
function getDefaultBulkwriteResult() {
return {
result: {
ok: 1,
writeErrors: [],
writeConcernErrors: [],
insertedIds: [],
nInserted: 0,
nUpserted: 0,
nMatched: 0,
nModified: 0,
nRemoved: 0,
upserted: []
},
insertedCount: 0,
matchedCount: 0,
modifiedCount: 0,
deletedCount: 0,
upsertedCount: 0,
upsertedIds: {},
insertedIds: {},
n: 0
};
}

module.exports = getDefaultBulkwriteResult;
6 changes: 5 additions & 1 deletion lib/model.js
Expand Up @@ -31,6 +31,7 @@ const applyStatics = require('./helpers/model/applyStatics');
const applyWriteConcern = require('./helpers/schema/applyWriteConcern');
const assignVals = require('./helpers/populate/assignVals');
const castBulkWrite = require('./helpers/model/castBulkWrite');
const getDefaultBulkwriteResult = require('./helpers/getDefaultBulkwriteResult');
const discriminator = require('./helpers/model/discriminator');
const each = require('./helpers/each');
const getDiscriminatorByValue = require('./helpers/discriminator/getDiscriminatorByValue');
Expand Down Expand Up @@ -3493,14 +3494,17 @@ Model.bulkWrite = function(ops, options, callback) {
const validations = ops.map(op => castBulkWrite(this, op, options));

callback = this.$handleCallbackError(callback);

return promiseOrCallback(callback, cb => {
cb = this.$wrapCallback(cb);
each(validations, (fn, cb) => fn(cb), error => {
if (error) {
return cb(error);
}

if (ops.length === 0) {
return cb(null, getDefaultBulkwriteResult());
}

this.collection.bulkWrite(ops, options, (error, res) => {
if (error) {
return cb(error);
Expand Down
2 changes: 1 addition & 1 deletion package.json
@@ -1,7 +1,7 @@
{
"name": "mongoose",
"description": "Mongoose MongoDB ODM",
"version": "5.9.18",
"version": "5.9.19",
"author": "Guillermo Rauch <guillermo@learnboost.com>",
"keywords": [
"mongodb",
Expand Down
36 changes: 35 additions & 1 deletion test/model.test.js
Expand Up @@ -6799,10 +6799,44 @@ describe('Model', function() {
assert.equal(typeof users[0].updatedAt, 'number');
assert.equal(typeof users[1].updatedAt, 'number');

// not-lean queries casts to number even if stored on DB as a date
// not-lean queries cast to number even if stored on DB as a date
assert.equal(users[0] instanceof User, false);
assert.equal(users[1] instanceof User, false);
});
});

it('Model#bulkWrite(...) does not throw an error when provided an empty array (gh-9131)', function() {
return co(function*() {
const userSchema = new Schema();
const User = db.model('User', userSchema);

const res = yield User.bulkWrite([]);

assert.deepEqual(
res,
{
result: {
ok: 1,
writeErrors: [],
writeConcernErrors: [],
insertedIds: [],
nInserted: 0,
nUpserted: 0,
nMatched: 0,
nModified: 0,
nRemoved: 0,
upserted: []
},
insertedCount: 0,
matchedCount: 0,
modifiedCount: 0,
deletedCount: 0,
upsertedCount: 0,
upsertedIds: {},
insertedIds: {},
n: 0
}
);
});
});
});

0 comments on commit a632705

Please sign in to comment.