Skip to content

Commit 2068a4b

Browse files
committed
refactor: get rid of weird setFunction, just use $set internally re: #1939
1 parent 9b0525b commit 2068a4b

File tree

4 files changed

+62
-51
lines changed

4 files changed

+62
-51
lines changed

lib/document.js

Lines changed: 42 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -435,8 +435,18 @@ function init(self, obj, doc, prefix) {
435435
*/
436436

437437
for (var k in hooks) {
438-
if (k === 'pre' || k === 'post') {
438+
if (k === 'post') {
439439
Document.prototype['$' + k] = Document['$' + k] = hooks[k];
440+
} else if (k === 'pre') {
441+
Document.prototype.$pre = Document.$pre = function mongoosePreWrapper() {
442+
if (arguments[0] === 'set') {
443+
// Make set hooks also work for `$set`
444+
var $setArgs = Array.prototype.slice.call(arguments);
445+
$setArgs[0] = '$set';
446+
hooks.pre.apply(this, $setArgs);
447+
}
448+
return hooks.pre.apply(this, arguments);
449+
};
440450
} else {
441451
Document.prototype[k] = Document[k] = hooks[k];
442452
}
@@ -468,29 +478,7 @@ Document.prototype.update = function update() {
468478
};
469479

470480
/**
471-
* Sets the value of a path, or many paths.
472-
*
473-
* ####Example:
474-
*
475-
* // path, value
476-
* doc.set(path, value)
477-
*
478-
* // object
479-
* doc.set({
480-
* path : value
481-
* , path2 : {
482-
* path : value
483-
* }
484-
* })
485-
*
486-
* // on-the-fly cast to number
487-
* doc.set(path, value, Number)
488-
*
489-
* // on-the-fly cast to string
490-
* doc.set(path, value, String)
491-
*
492-
* // changing strict mode behavior
493-
* doc.set(path, value, { strict: false });
481+
* Alias for `set()`, used internally to avoid conflicts
494482
*
495483
* @param {String|Object} path path or object of key/vals to set
496484
* @param {Any} val the value to set
@@ -785,8 +773,36 @@ Document.prototype.$set = function(path, val, type, options) {
785773
return this;
786774
};
787775

788-
/*!
789-
* ignore
776+
/**
777+
* Sets the value of a path, or many paths.
778+
*
779+
* ####Example:
780+
*
781+
* // path, value
782+
* doc.set(path, value)
783+
*
784+
* // object
785+
* doc.set({
786+
* path : value
787+
* , path2 : {
788+
* path : value
789+
* }
790+
* })
791+
*
792+
* // on-the-fly cast to number
793+
* doc.set(path, value, Number)
794+
*
795+
* // on-the-fly cast to string
796+
* doc.set(path, value, String)
797+
*
798+
* // changing strict mode behavior
799+
* doc.set(path, value, { strict: false });
800+
*
801+
* @param {String|Object} path path or object of key/vals to set
802+
* @param {Any} val the value to set
803+
* @param {Schema|String|Number|Buffer|*} [type] optionally specify a type for "on-the-fly" attributes
804+
* @param {Object} [options] optionally specify options that modify the behavior of the set
805+
* @api public
790806
*/
791807

792808
Document.prototype.set = Document.prototype.$set;

lib/schema.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -349,8 +349,7 @@ Schema.prototype.defaultOptions = function(options) {
349349
noVirtualId: false, // deprecated, use { id: false }
350350
id: true,
351351
typeKey: 'type',
352-
retainKeyOrder: false,
353-
setFunction: 'set'
352+
retainKeyOrder: false
354353
}, options);
355354

356355
if (options.read) {

lib/services/document/compile.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ function defineKey(prop, subprops, prototype, prefix, keys, options) {
127127
v = v.toObject({ transform: false });
128128
}
129129
var doc = this.$__.scope || this;
130-
return doc[doc.schema.options.setFunction](path, v);
130+
return doc.$set(path, v);
131131
}
132132
});
133133
} else {
@@ -138,7 +138,7 @@ function defineKey(prop, subprops, prototype, prefix, keys, options) {
138138
return this.get.call(this.$__.scope || this, path);
139139
},
140140
set: function(v) {
141-
return this[this.schema.options.setFunction].call(this.$__.scope || this, path, v);
141+
return this.$set.call(this.$__.scope || this, path, v);
142142
}
143143
});
144144
}

package-lock.json

Lines changed: 17 additions & 21 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)