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 24fa3a1cc08..ceb5a4b2f01 100644 --- a/lib/model.js +++ b/lib/model.js @@ -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!') } - - 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; }; /**