Skip to content

Commit

Permalink
fix(timestamps): set createdAt when creating new single nested subdoc…
Browse files Browse the repository at this point in the history
…uments

Also allow `overwriteImmutable` option to `set()` to allow overwriting immutable fields

Fix #11603
  • Loading branch information
vkarpov15 committed Apr 19, 2022
1 parent 9e9c8b0 commit 89a2384
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 6 deletions.
2 changes: 1 addition & 1 deletion lib/document.js
Original file line number Diff line number Diff line change
Expand Up @@ -1401,7 +1401,7 @@ Document.prototype.$set = function $set(path, val, type, options) {
// later in `$__set()` because we don't take `_doc` when we iterate through
// a single nested doc. That's to make sure we get the correct context.
// Otherwise we would double-call the setter, see gh-7196.
val = schema.applySetters(val, this, false, priorVal);
val = schema.applySetters(val, this, false, priorVal, options);
}

if (Array.isArray(val) &&
Expand Down
5 changes: 4 additions & 1 deletion lib/helpers/schematype/handleImmutable.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,16 @@ module.exports = function(schematype) {
};

function createImmutableSetter(path, immutable) {
return function immutableSetter(v) {
return function immutableSetter(v, _priorVal, _doc, options) {
if (this == null || this.$__ == null) {
return v;
}
if (this.isNew) {
return v;
}
if (options && options.overwriteImmutable) {
return v;
}

const _immutable = typeof immutable === 'function' ?
immutable.call(this, this) :
Expand Down
4 changes: 2 additions & 2 deletions lib/helpers/timestamps/setupTimestamps.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ module.exports = function setupTimestamps(schema, timestamps) {
currentTime() :
this.ownerDocument().constructor.base.now();

if (!skipCreatedAt && this.isNew && createdAt && !this.$__getValue(createdAt) && this.$__isSelected(createdAt)) {
this.$set(createdAt, defaultTimestamp);
if (!skipCreatedAt && (this.isNew || this.$isSubdocument) && createdAt && !this.$__getValue(createdAt) && this.$__isSelected(createdAt)) {
this.$set(createdAt, defaultTimestamp, undefined, { overwriteImmutable: true });
}

if (!skipUpdatedAt && updatedAt && (this.isNew || this.$isModified())) {
Expand Down
4 changes: 2 additions & 2 deletions lib/schematype.js
Original file line number Diff line number Diff line change
Expand Up @@ -1148,15 +1148,15 @@ SchemaType.prototype.getDefault = function(scope, init) {
* @api private
*/

SchemaType.prototype._applySetters = function(value, scope, init, priorVal) {
SchemaType.prototype._applySetters = function(value, scope, init, priorVal, options) {
let v = value;
if (init) {
return v;
}
const setters = this.setters;

for (let i = setters.length - 1; i >= 0; i--) {
v = setters[i].call(scope, v, priorVal, this);
v = setters[i].call(scope, v, priorVal, this, options);
}

return v;
Expand Down

0 comments on commit 89a2384

Please sign in to comment.