Skip to content

Commit

Permalink
refactor: avoid relying on overwrite option for timestamps and castin…
Browse files Browse the repository at this point in the history
…g re: #13578
  • Loading branch information
vkarpov15 committed Oct 17, 2023
1 parent d14e94d commit f50d88d
Show file tree
Hide file tree
Showing 4 changed files with 4 additions and 24 deletions.
2 changes: 1 addition & 1 deletion lib/helpers/timestamps/setupTimestamps.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ module.exports = function setupTimestamps(schema, timestamps) {
updatedAt,
this.getUpdate(),
this._mongooseOptions,
this.schema
replaceOps.has(this.op)
);
applyTimestampsToChildren(now, this.getUpdate(), this.model.schema);
next();
Expand Down
5 changes: 2 additions & 3 deletions lib/helpers/update/applyTimestampsToUpdate.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,9 @@ module.exports = applyTimestampsToUpdate;
* ignore
*/

function applyTimestampsToUpdate(now, createdAt, updatedAt, currentUpdate, options) {
function applyTimestampsToUpdate(now, createdAt, updatedAt, currentUpdate, options, isReplace) {
const updates = currentUpdate;
let _updates = updates;
const overwrite = get(options, 'overwrite', false);
const timestamps = get(options, 'timestamps', true);

// Support skipping timestamps at the query level, see gh-6980
Expand All @@ -26,7 +25,7 @@ function applyTimestampsToUpdate(now, createdAt, updatedAt, currentUpdate, optio
const skipCreatedAt = timestamps != null && timestamps.createdAt === false;
const skipUpdatedAt = timestamps != null && timestamps.updatedAt === false;

if (overwrite) {
if (isReplace) {
if (currentUpdate && currentUpdate.$set) {
currentUpdate = currentUpdate.$set;
updates.$set = {};
Expand Down
10 changes: 1 addition & 9 deletions lib/query.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ const castUpdate = require('./helpers/query/castUpdate');
const clone = require('./helpers/clone');
const completeMany = require('./helpers/query/completeMany');
const getDiscriminatorByValue = require('./helpers/discriminator/getDiscriminatorByValue');
const hasDollarKeys = require('./helpers/query/hasDollarKeys');
const helpers = require('./queryHelpers');
const immediate = require('./helpers/immediate');
const internalToObjectOptions = require('./options').internalToObjectOptions;
Expand Down Expand Up @@ -3501,7 +3500,6 @@ Query.prototype.findOneAndReplace = function(filter, replacement, options) {
options.returnOriginal = returnOriginal;
}
this.setOptions(options);
this.setOptions({ overwrite: true });

return this;
};
Expand Down Expand Up @@ -3736,13 +3734,8 @@ async function _updateThunk(op) {
this._applyTranslateAliases(options);

this._update = clone(this._update, options);
const isOverwriting = this._mongooseOptions.overwrite && !hasDollarKeys(this._update);
const isOverwriting = op === 'replaceOne';
if (isOverwriting) {
if (op === 'updateOne' || op === 'updateMany') {
throw new MongooseError('The MongoDB server disallows ' +
'overwriting documents using `' + op + '`. See: ' +
'https://mongoosejs.com/docs/deprecations.html#update');
}
this._update = new this.model(this._update, null, true);
} else {
this._update = this._castUpdate(this._update, this._mongooseOptions.overwrite);
Expand Down Expand Up @@ -4046,7 +4039,6 @@ Query.prototype.replaceOne = function(conditions, doc, options, callback) {
callback = undefined;
}

this.setOptions({ overwrite: true });
return _update(this, 'replaceOne', conditions, doc, options, callback);
};

Expand Down
11 changes: 0 additions & 11 deletions test/query.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2962,17 +2962,6 @@ describe('Query', function() {
delete db.base.options.maxTimeMS;
});

it('throws error with updateOne() and overwrite (gh-7475)', function() {
const Model = db.model('Test', Schema({ name: String }));

return Model.updateOne({}, { name: 'bar' }, { overwrite: true }).then(
() => { throw new Error('Should have failed'); },
err => {
assert.ok(err.message.indexOf('updateOne') !== -1);
}
);
});

describe('merge()', function() {
it('copies populate() (gh-1790)', async function() {
const Car = db.model('Car', {
Expand Down

0 comments on commit f50d88d

Please sign in to comment.