diff --git a/lib/model.js b/lib/model.js index 1624341e5c1..ec3e085fdec 100644 --- a/lib/model.js +++ b/lib/model.js @@ -15,6 +15,7 @@ const MongooseMap = require('./types/map'); const OverwriteModelError = require('./error').OverwriteModelError; const PromiseProvider = require('./promise_provider'); const Query = require('./query'); +const SaveOptions = require('./options/saveOptions'); const Schema = require('./schema'); const VersionError = require('./error').VersionError; const ParallelSaveError = require('./error').ParallelSaveError; @@ -362,7 +363,8 @@ Model.prototype.$__save = function(options, callback) { if (numAffected <= 0) { // the update failed. pass an error back - const err = options.$versionError || new VersionError(this, version, modifiedPaths); + const err = this.$__.$versionError || + new VersionError(this, version, modifiedPaths); return callback(err); } @@ -449,17 +451,12 @@ Model.prototype.save = function(options, fn) { options = undefined; } - if (options != null) { - options = utils.clone(options); - } else { - options = {}; - } - if (fn) { fn = this.constructor.$wrapCallback(fn); } - options.$versionError = generateVersionError(this, this.modifiedPaths()); + options = new SaveOptions(options); + this.$__.$versionError = generateVersionError(this, this.modifiedPaths()); return utils.promiseOrCallback(fn, cb => { if (parallelSave) { diff --git a/lib/options/saveOptions.js b/lib/options/saveOptions.js new file mode 100644 index 00000000000..50308deccd7 --- /dev/null +++ b/lib/options/saveOptions.js @@ -0,0 +1,14 @@ +'use strict'; + +const utils = require('../utils'); + +class SaveOptions { + constructor(obj) { + if (obj == null) { + return; + } + Object.assign(this, utils.clone(obj)); + } +} + +module.exports = SaveOptions; \ No newline at end of file