diff --git a/lib/helpers/model/applyHooks.js b/lib/helpers/model/applyHooks.js index 7ed7895d4b3..093b037b39a 100644 --- a/lib/helpers/model/applyHooks.js +++ b/lib/helpers/model/applyHooks.js @@ -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); diff --git a/lib/model.js b/lib/model.js index 052414d2b76..72e59acf279 100644 --- a/lib/model.js +++ b/lib/model.js @@ -1009,53 +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!') } - - 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, err => { - if (!err) { - this.$__.isDeleted = true; - this.$emit('deleteOne', this); - this.constructor.emit('deleteOne', this); - return cb(null, this); - } - this.$__.isDeleted = false; - cb(err); - }); + return query; }; /**