From a7518bdba44eedc476464c0bb8abb9f4e74a46db Mon Sep 17 00:00:00 2001 From: Valeri Karpov Date: Sat, 7 Oct 2017 20:48:54 -0700 Subject: [PATCH] refactor(document): add PoC of using $set internally Re: #1939 --- lib/document.js | 26 ++++++++++++++++---------- lib/schema.js | 3 ++- lib/services/document/compile.js | 5 +++-- 3 files changed, 21 insertions(+), 13 deletions(-) diff --git a/lib/document.js b/lib/document.js index 24b3336b43f..718485fdc5d 100644 --- a/lib/document.js +++ b/lib/document.js @@ -74,7 +74,7 @@ function Document(obj, fields, skipId, options) { if (this.$__original_set) { this.$__original_set(obj, undefined, true); } else { - this.set(obj, undefined, true); + this.$set(obj, undefined, true); } } @@ -499,7 +499,7 @@ Document.prototype.update = function update() { * @api public */ -Document.prototype.set = function(path, val, type, options) { +Document.prototype.$set = function(path, val, type, options) { if (type && utils.getFunctionName(type.constructor) === 'Object') { options = type; type = undefined; @@ -548,7 +548,7 @@ Document.prototype.set = function(path, val, type, options) { if (len === 0 && !this.schema.options.minimize) { if (val) { - this.set(val, {}); + this.$set(val, {}); } return this; } @@ -583,7 +583,7 @@ Document.prototype.set = function(path, val, type, options) { && !(this.schema.paths[pathName] && this.schema.paths[pathName].options && this.schema.paths[pathName].options.ref)) { - this.set(path[key], prefix + key, constructing); + this.$set(path[key], prefix + key, constructing); } else if (strict) { // Don't overwrite defaults with undefined keys (gh-3981) if (constructing && path[key] === void 0 && @@ -599,9 +599,9 @@ Document.prototype.set = function(path, val, type, options) { path[key] instanceof Document) { p = p.toObject({ virtuals: false, transform: false }); } - this.set(prefix + key, p, constructing); + this.$set(prefix + key, p, constructing); } else if (pathtype === 'nested' && path[key] instanceof Document) { - this.set(prefix + key, + this.$set(prefix + key, path[key].toObject({transform: false}), constructing); } else if (strict === 'throw') { if (pathtype === 'nested') { @@ -611,7 +611,7 @@ Document.prototype.set = function(path, val, type, options) { } } } else if (path[key] !== void 0) { - this.set(prefix + key, path[key], constructing); + this.$set(prefix + key, path[key], constructing); } } @@ -629,7 +629,7 @@ Document.prototype.set = function(path, val, type, options) { this.markModified(path); cleanModifiedSubpaths(this, path); } else { - this.set(val, path, constructing); + this.$set(val, path, constructing); } return this; } @@ -681,7 +681,7 @@ Document.prototype.set = function(path, val, type, options) { cur = cur[parts[i]]; curPath += (curPath.length > 0 ? '.' : '') + parts[i]; if (!cur) { - this.set(curPath, {}); + this.$set(curPath, {}); cur = this.getValue(curPath); } } @@ -785,6 +785,12 @@ Document.prototype.set = function(path, val, type, options) { return this; }; +/*! + * ignore + */ + +Document.prototype.set = Document.prototype.$set; + /** * Determine if we should mark this change as modified. * @@ -2575,7 +2581,7 @@ Document.prototype.depopulate = function(path) { return; } delete this.$__.populated[path]; - this.set(path, populatedIds); + this.$set(path, populatedIds); return this; }; diff --git a/lib/schema.js b/lib/schema.js index e4517730287..a849eee9b03 100644 --- a/lib/schema.js +++ b/lib/schema.js @@ -325,7 +325,8 @@ Schema.prototype.defaultOptions = function(options) { noVirtualId: false, // deprecated, use { id: false } id: true, typeKey: 'type', - retainKeyOrder: false + retainKeyOrder: false, + setFunction: 'set' }, options); if (options.read) { diff --git a/lib/services/document/compile.js b/lib/services/document/compile.js index 958c4b672a1..e5b329cf1c1 100644 --- a/lib/services/document/compile.js +++ b/lib/services/document/compile.js @@ -126,7 +126,8 @@ function defineKey(prop, subprops, prototype, prefix, keys, options) { if (v instanceof Document) { v = v.toObject({ transform: false }); } - return (this.$__.scope || this).set(path, v); + var doc = this.$__.scope || this; + return doc[doc.schema.options.setFunction](path, v); } }); } else { @@ -137,7 +138,7 @@ function defineKey(prop, subprops, prototype, prefix, keys, options) { return this.get.call(this.$__.scope || this, path); }, set: function(v) { - return this.set.call(this.$__.scope || this, path, v); + return this[this.schema.options.setFunction].call(this.$__.scope || this, path, v); } }); }