Skip to content

Commit

Permalink
refactor(document): add PoC of using $set internally
Browse files Browse the repository at this point in the history
Re: #1939
  • Loading branch information
vkarpov15 committed Oct 8, 2017
1 parent 68f57b8 commit a7518bd
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 13 deletions.
26 changes: 16 additions & 10 deletions lib/document.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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 &&
Expand All @@ -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') {
Expand All @@ -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);
}
}

Expand All @@ -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;
}
Expand Down Expand Up @@ -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);
}
}
Expand Down Expand Up @@ -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.
*
Expand Down Expand Up @@ -2575,7 +2581,7 @@ Document.prototype.depopulate = function(path) {
return;
}
delete this.$__.populated[path];
this.set(path, populatedIds);
this.$set(path, populatedIds);
return this;
};

Expand Down
3 changes: 2 additions & 1 deletion lib/schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
5 changes: 3 additions & 2 deletions lib/services/document/compile.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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);
}
});
}
Expand Down

0 comments on commit a7518bd

Please sign in to comment.