Skip to content

Commit

Permalink
made the changes but some failing tests
Browse files Browse the repository at this point in the history
  • Loading branch information
IslandRhythms committed Jul 17, 2023
1 parent 94b78c0 commit 61e9041
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 43 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
56 changes: 14 additions & 42 deletions lib/model.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};

/**
Expand Down

0 comments on commit 61e9041

Please sign in to comment.