Skip to content

Commit

Permalink
refactor: remove not-useful castDoc() helper re: #11559
Browse files Browse the repository at this point in the history
  • Loading branch information
vkarpov15 committed Apr 10, 2022
1 parent 6611203 commit 888f116
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 24 deletions.
4 changes: 3 additions & 1 deletion lib/helpers/query/castUpdate.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ module.exports = function castUpdate(schema, obj, options, context, filter) {
} else if (!options.overwriteDiscriminatorKey) {
delete obj[schema.options.discriminatorKey];
}
if (options.upsert) {
if (options.upsert && !options.overwrite) {
moveImmutableProperties(schema, obj, context);
}

Expand Down Expand Up @@ -217,6 +217,7 @@ function walkUpdatePath(schema, obj, op, options, context, filter, pref) {
}

if (op !== '$setOnInsert' &&
!options.overwrite &&
handleImmutable(schematype, strict, obj, key, prefix + key, context)) {
continue;
}
Expand Down Expand Up @@ -311,6 +312,7 @@ function walkUpdatePath(schema, obj, op, options, context, filter, pref) {

// You can use `$setOnInsert` with immutable keys
if (op !== '$setOnInsert' &&
!options.overwrite &&
handleImmutable(schematype, strict, obj, key, prefix + key, context)) {
continue;
}
Expand Down
4 changes: 2 additions & 2 deletions lib/helpers/update/applyTimestampsToChildren.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ function applyTimestampsToChildren(now, update, schema) {
}

const keys = Object.keys(update);
const hasDollarKey = keys.some(key => key.startsWith('$'));
const hasDollarKey = keys.some(key => key[0] === '$');

if (hasDollarKey) {
if (update.$push) {
Expand All @@ -38,7 +38,7 @@ function applyTimestampsToChildren(now, update, schema) {
}
}

const updateKeys = Object.keys(update).filter(key => !key.startsWith('$'));
const updateKeys = Object.keys(update).filter(key => key[0] !== '$');
for (const key of updateKeys) {
applyTimestampsToUpdateKey(schema, key, update, now);
}
Expand Down
1 change: 0 additions & 1 deletion lib/helpers/update/applyTimestampsToUpdate.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,6 @@ function applyTimestampsToUpdate(now, createdAt, updatedAt, currentUpdate, optio
}

if (!skipCreatedAt && createdAt) {

if (currentUpdate[createdAt]) {
delete currentUpdate[createdAt];
}
Expand Down
31 changes: 11 additions & 20 deletions lib/query.js
Original file line number Diff line number Diff line change
Expand Up @@ -3737,15 +3737,15 @@ Query.prototype._findOneAndReplace = wrapThunk(function(callback) {
const runValidators = _getOption(this, 'runValidators', false);
if (runValidators === false) {
try {
this._castUpdate(this._update, true);
this._update = this._castUpdate(this._update, true);
} catch (err) {
const validationError = new ValidationError();
validationError.errors[err.path] = err;
callback(validationError);
return null;
}

this._collection.collection.findOneAndReplace(filter, this._update, options, _wrapThunkCallback(this, (err, res) => {
this._collection.collection.findOneAndReplace(filter, this._update || {}, options, _wrapThunkCallback(this, (err, res) => {
if (err) {
return callback(err);
}
Expand Down Expand Up @@ -3884,7 +3884,11 @@ Query.prototype._findAndModify = function(type, callback) {
}

if (!isOverwriting) {
this._update = castDoc(this, opts.overwrite);
try {
this._update = this._castUpdate(this._update, opts.overwrite);
} catch (err) {
return callback(err);
}
const _opts = Object.assign({}, opts, {
setDefaultsOnInsert: this._mongooseOptions.setDefaultsOnInsert
});
Expand Down Expand Up @@ -4079,10 +4083,10 @@ function _updateThunk(op, callback) {
}
this._update = new this.model(this._update, null, true);
} else {
this._update = castDoc(this, options.overwrite);

if (this._update instanceof Error) {
callback(this._update);
try {
this._update = this._castUpdate(this._update, options.overwrite);
} catch (err) {
callback(err);
return null;
}

Expand Down Expand Up @@ -4921,19 +4925,6 @@ function castQuery(query) {
}
}

/*!
* castDoc
* @api private
*/

function castDoc(query, overwrite) {
try {
return query._castUpdate(query._update, overwrite);
} catch (err) {
return err;
}
}

/**
* Specifies paths which should be populated with other documents.
*
Expand Down

0 comments on commit 888f116

Please sign in to comment.