Skip to content

Commit

Permalink
Merge branch 'IslandRhythms/gh-13369' of github.com:Automattic/mongoo…
Browse files Browse the repository at this point in the history
…se into IslandRhythms/gh-13369
  • Loading branch information
vkarpov15 committed Jul 17, 2023
2 parents 8c17b91 + 61e9041 commit d9fd789
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 45 deletions.
2 changes: 1 addition & 1 deletion lib/helpers/model/applyHooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ function applyHooks(model, schema, options) {

objToDecorate.$__originalValidate = objToDecorate.$__originalValidate || objToDecorate.$__validate;

for (const method of ['save', 'validate', 'remove', 'deleteOne']) {
for (const method of ['save', 'validate', 'remove']) {
const toWrap = method === 'validate' ? '$__originalValidate' : `$__${method}`;
const wrapped = middleware.
createWrapper(method, objToDecorate[toWrap], null, kareemOptions);
Expand Down
58 changes: 14 additions & 44 deletions lib/model.js
Original file line number Diff line number Diff line change
Expand Up @@ -1009,55 +1009,25 @@ Model.prototype.deleteOne = async function deleteOne(options) {
options = {};
}

if (options.hasOwnProperty('session')) {
this.$session(options.session);
if (!this._id) {
throw new Error('No _id found on document!')

Check failure on line 1013 in lib/model.js

View workflow job for this annotation

GitHub Actions / Lint JS-Files

Missing semicolon
}

const res = await new Promise((resolve, reject) => {
this.$__deleteOne(options, (err, res) => {
if (err != null) {
return reject(err);
}
resolve(res);
});
});

return res;
};

/*!
* ignore
*/

Model.prototype.$__deleteOne = function $__deleteOne(options, cb) {
if (this.$__.isDeleted) {
return immediate(() => cb(null, this));
}

const where = this.$__where();
if (where instanceof MongooseError) {
return cb(where);
return;
}
const self = this;
const query = self.constructor.deleteOne(options);
query.pre(function queryPreDeleteOne(cb) {
self.constructor._middleware.execPre('deleteOne', self, [self], cb);
});
query.post(function queryPostDeleteOne(cb) {
self.constructor._middleware.execPost('deleteOne', self, [self], {}, cb);
});

_applyCustomWhere(this, where);

const session = this.$session();
if (!options.hasOwnProperty('session')) {
options.session = session;
if (options.hasOwnProperty('session')) {
self.$session(options.session);
}

this[modelCollectionSymbol].deleteOne(where, options).then(
() => {
this.$__.isDeleted = true;
this.$emit('deleteOne', this);
this.constructor.emit('deleteOne', this);
return cb(null, this);
},
err => {
this.$__.isDeleted = false;
cb(err);
}
);
return query;
};

/**
Expand Down

2 comments on commit d9fd789

@l-e-u
Copy link

@l-e-u l-e-u commented on d9fd789 Jul 19, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hello,
I see that Model.prototype.deleteOne is an async function, and the original issue #13223 is wanting deleteOne to return a query.
Also, if self.constructor.deleteOne is going to be called, Model.deleteOne calls Query which applies the middleware.

Please correct me if I'm wrong because there might be something I'm missing or misunderstanding. I would like to help if possible.
Cheers!

@vkarpov15
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's a very good point, thanks for noticing this.

Please sign in to comment.