Skip to content

Commit

Permalink
Merge pull request #9036 from AbdelrahmanHafez/gh-9032
Browse files Browse the repository at this point in the history
feat(mongoose): add support for setting `setDefaultsOnInsert` as a global option
  • Loading branch information
vkarpov15 committed May 23, 2020
2 parents 63dbf7c + 4833de1 commit bee8920
Show file tree
Hide file tree
Showing 6 changed files with 87 additions and 7 deletions.
16 changes: 16 additions & 0 deletions History.md
@@ -1,3 +1,19 @@
5.9.15 / 2020-05-18
===================
* fix(schema): treat creating dotted path with no parent as creating a nested path #9020
* fix(documentarray): make sure you can call `unshift()` after `map()` #9012 [philippejer](https://github.com/philippejer)
* fix(model): cast bulkwrite according to discriminator schema if discriminator key is present #8982 [AbdelrahmanHafez](https://github.com/AbdelrahmanHafez)
* fix(schema): remove `db` from reserved keywords #8940
* fix(populate): treat populating a doc array that doesn't have a `ref` as a no-op #8946
* fix(timestamps): set createdAt and updatedAt on doubly nested subdocs when upserting #8894
* fix(model): allow POJOs as schemas for model.discriminator(...) #8991 [AbdelrahmanHafez](https://github.com/AbdelrahmanHafez)
* fix(model): report `insertedDocs` on `insertMany()` errors #8938
* fix(model): ensure consistent `writeErrors` property on insertMany error with `ordered: false`, even if only one op failed #8938
* docs: add anchor tag to strictQuery and strict #9014 [AbdelrahmanHafez](https://github.com/AbdelrahmanHafez)
* docs(faq): remove faq ipv6 #9004
* docs: add note about throwing error only after validation and fix broken reference to api/CastError #8993 [AbdelrahmanHafez](https://github.com/AbdelrahmanHafez)
* docs: fix typos in documents.pug #9005 [dandv](https://github.com/dandv)

5.9.14 / 2020-05-13
===================
* fix(cursor): add index as second parameter to eachAsync callback #8972 [AbdelrahmanHafez](https://github.com/AbdelrahmanHafez)
Expand Down
6 changes: 6 additions & 0 deletions index.pug
Expand Up @@ -343,9 +343,15 @@ html(lang='en')
<a href="https://casinohex.co.za/online-casinos/">
<img class="sponsor" src="https://images.opencollective.com/casinohex-co-za/470843d/logo/256.png" style="height: 100px">
</a>
<a href="https://uk.edubirdie.com/">
<img class="sponsor" src="https://images.opencollective.com/uk-edubirdie-com/b1d51ab/logo/256.png" style="height: 100px">
</a>
<a href="https://www.casinofever.ca/">
<img class="sponsor" src="https://images.opencollective.com/casinofever-ca1/4ad150e/logo/256.png" style="height: 100px">
</a>
<a href="https://www.parhaatnettikasinot.com/">
<img class="sponsor" src="https://images.opencollective.com/parhaatnettikasinot-com/fc53017/logo/256.png" style="height: 100px">
</a>
</div>
</div>

Expand Down
17 changes: 11 additions & 6 deletions lib/helpers/setDefaultsOnInsert.js
Expand Up @@ -14,6 +14,17 @@ const modifiedPaths = require('./common').modifiedPaths;
*/

module.exports = function(filter, schema, castedDoc, options) {
options = options || {};

const shouldSetDefaultsOnInsert =
options.hasOwnProperty('setDefaultsOnInsert') ?
options.setDefaultsOnInsert :
schema.base.options.setDefaultsOnInsert;

if (!options.upsert || !shouldSetDefaultsOnInsert) {
return castedDoc;
}

const keys = Object.keys(castedDoc || {});
const updatedKeys = {};
const updatedValues = {};
Expand All @@ -22,12 +33,6 @@ module.exports = function(filter, schema, castedDoc, options) {

let hasDollarUpdate = false;

options = options || {};

if (!options.upsert || !options.setDefaultsOnInsert) {
return castedDoc;
}

for (let i = 0; i < numKeys; ++i) {
if (keys[i].startsWith('$')) {
modifiedPaths(castedDoc[keys[i]], '', modified);
Expand Down
1 change: 1 addition & 0 deletions lib/validoptions.js
Expand Up @@ -16,6 +16,7 @@ const VALID_OPTIONS = Object.freeze([
'objectIdGetter',
'runValidators',
'selectPopulatedPaths',
'setDefaultsOnInsert',
'strict',
'strictQuery',
'toJSON',
Expand Down
2 changes: 1 addition & 1 deletion package.json
@@ -1,7 +1,7 @@
{
"name": "mongoose",
"description": "Mongoose MongoDB ODM",
"version": "5.9.14",
"version": "5.9.15",
"author": "Guillermo Rauch <guillermo@learnboost.com>",
"keywords": [
"mongodb",
Expand Down
52 changes: 52 additions & 0 deletions test/index.test.js
Expand Up @@ -778,5 +778,57 @@ describe('mongoose module:', function() {
done();
});
});

it('can set `setDefaultsOnInsert` as a global option (gh-9032)', function() {
return co(function* () {
const m = new mongoose.Mongoose();
m.set('setDefaultsOnInsert', true);
const db = yield m.connect('mongodb://localhost:27017/mongoose_test_9032');

const schema = new m.Schema({
title: String,
genre: { type: String, default: 'Action' }
}, { collection: 'movies_1' });

const Movie = db.model('Movie', schema);


yield Movie.updateOne(
{},
{ title: 'Cloud Atlas' },
{ upsert: true }
);

// lean is necessary to avoid defaults by casting
const movie = yield Movie.findOne({ title: 'Cloud Atlas' }).lean();
assert.equal(movie.genre, 'Action');
});
});

it('setting `setDefaultOnInsert` on operation has priority over base option (gh-9032)', function() {
return co(function* () {
const m = new mongoose.Mongoose();
m.set('setDefaultsOnInsert', true);
const db = yield m.connect('mongodb://localhost:27017/mongoose_test_9032');

const schema = new m.Schema({
title: String,
genre: { type: String, default: 'Action' }
}, { collection: 'movies_2' });

const Movie = db.model('Movie', schema);


yield Movie.updateOne(
{},
{ title: 'The Man From Earth' },
{ upsert: true, setDefaultsOnInsert: false }
);

// lean is necessary to avoid defaults by casting
const movie = yield Movie.findOne({ title: 'The Man From Earth' }).lean();
assert.ok(!movie.genre);
});
});
});
});

0 comments on commit bee8920

Please sign in to comment.