Skip to content

Commit

Permalink
Handling #50, #56, #52. Adding additional tests
Browse files Browse the repository at this point in the history
#52 Counter mongo index bug with $ne and count()
#50 do not use $ne operator
#56 Fix Mongoose DeprecationWarning: collection.update is deprecated
  • Loading branch information
dsanel committed Sep 3, 2019
1 parent 4f46aac commit 530050d
Show file tree
Hide file tree
Showing 5 changed files with 208 additions and 15 deletions.
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
## [v0.5.1]
> September 3, 2019
- Add option to disable use of `$ne` operator using `{use$neOperator: false}` (@bdelville, @gabzim) #50
- Fix Mongoose DeprecationWarning: collection.update is deprecated (@cardimajs, @jebarjonet)
- Upgrade all `devDependencies` to latest versions
- Fix security vulnerabilities in dependencies
- Add additional tests for `updateMany`, `countDocuments`, `use$neOperator`

## [v0.5.0]
> December 10, 2018
Expand Down
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,11 @@ mongoose-delete is simple and lightweight plugin that enables soft deletion of d
- [Add __deletedBy__ key to record who deleted document](#who-has-deleted-the-data)
- Restore deleted documents using __restore__ method
- [Bulk delete and restore](#bulk-delete-and-restore)
- [Option to override static methods](#examples-how-to-override-one-or-multiple-methods) (__count, countDocuments, find, findOne, findOneAndUpdate, update__)
- [Option to override static methods](#examples-how-to-override-one-or-multiple-methods) (__count, countDocuments, find, findOne, findOneAndUpdate, update, updateMany__)
- [For overridden methods we have two additional methods](#method-overridden): __methodDeleted__ and __methodWithDeleted__
- [Disable model validation on delete](#disable-model-validation-on-delete)
- [Option to create index on delete fields](#create-index-on-fields) (__deleted__, __deletedAt__, __deletedBy__)
- Option to disable use of `$ne` operator using `{use$neOperator: false}`. Before you start to use this option please check [#50](https://github.com/dsanel/mongoose-delete/issues/50).

## Installation
Install using [npm](https://npmjs.org)
Expand Down Expand Up @@ -174,10 +175,12 @@ We have the option to override all standard methods or only specific methods. Ov
| only not deleted documents | only deleted documents | all documents |
|----------------------------|-------------------------|-----------------------------|
| count() | countDeleted | countWithDeleted |
| countDocuments() | countDocumentsDeleted | countDocumentsWithDeleted |
| find() | findDeleted | findWithDeleted |
| findOne() | findOneDeleted | findOneWithDeleted |
| findOneAndUpdate() | findOneAndUpdateDeleted | findOneAndUpdateWithDeleted |
| update() | updateDeleted | updateWithDeleted |
| updateMany() | updateManyDeleted | updateManyWithDeleted |

### Examples how to override one or multiple methods

Expand Down
48 changes: 35 additions & 13 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ var mongoose = require('mongoose'),

/**
* This code is taken from official mongoose repository
* https://github.com/Automattic/mongoose/blob/master/lib/query.js#L1996-L2018
* https://github.com/Automattic/mongoose/blob/master/lib/query.js#L3847-L3873
*/
/* istanbul ignore next */
function parseUpdateArguments (conditions, doc, options, callback) {
Expand Down Expand Up @@ -78,10 +78,11 @@ function createSchemaObject (typeKey, typeValue, options) {

module.exports = function (schema, options) {
options = options || {};
var indexFields = parseIndexFields(options)
var indexFields = parseIndexFields(options);

var typeKey = schema.options.typeKey;

var mongooseMajorVersion = +mongoose.version[0]; // 4, 5...
var mainUpdateMethod = mongooseMajorVersion < 5 ? 'update' : 'updateMany';
schema.add({ deleted: createSchemaObject(typeKey, Boolean, { default: false, index: indexFields.deleted }) });

if (options.deletedAt === true) {
Expand All @@ -92,6 +93,11 @@ module.exports = function (schema, options) {
schema.add({ deletedBy: createSchemaObject(typeKey, options.deletedByType || Schema.Types.ObjectId, { index: indexFields.deletedBy }) });
}

var use$neOperator = true;
if (options.use$neOperator !== undefined && typeof options.use$neOperator === "boolean") {
use$neOperator = options.use$neOperator;
}

schema.pre('save', function (next) {
if (!this.deleted) {
this.deleted = false;
Expand All @@ -101,7 +107,7 @@ module.exports = function (schema, options) {

if (options.overrideMethods) {
var overrideItems = options.overrideMethods;
var overridableMethods = ['count', 'countDocuments', 'find', 'findOne', 'findOneAndUpdate', 'update'];
var overridableMethods = ['count', 'countDocuments', 'find', 'findOne', 'findOneAndUpdate', 'update', 'updateMany'];
var finalList = [];

if ((typeof overrideItems === 'string' || overrideItems instanceof String) && overrideItems === 'all') {
Expand All @@ -126,15 +132,23 @@ module.exports = function (schema, options) {

// countDocuments do not exist in Mongoose v4
/* istanbul ignore next */
if (method === 'countDocuments' && typeof Model.countDocuments !== 'function') {
if (mongooseMajorVersion < 5 && method === 'countDocuments' && typeof Model.countDocuments !== 'function') {
modelMethodName = 'count';
}

schema.statics[method] = function () {
return Model[modelMethodName].apply(this, arguments).where('deleted').ne(true);
if (use$neOperator) {
return Model[modelMethodName].apply(this, arguments).where('deleted').ne(true);
} else {
return Model[modelMethodName].apply(this, arguments).where({deleted: false});
}
};
schema.statics[method + 'Deleted'] = function () {
return Model[modelMethodName].apply(this, arguments).where('deleted').ne(false);
if (use$neOperator) {
return Model[modelMethodName].apply(this, arguments).where('deleted').ne(false);
} else {
return Model[modelMethodName].apply(this, arguments).where({deleted: true});
}
};
schema.statics[method + 'WithDeleted'] = function () {
return Model[modelMethodName].apply(this, arguments);
Expand All @@ -143,15 +157,23 @@ module.exports = function (schema, options) {
schema.statics[method] = function () {
var args = parseUpdateArguments.apply(undefined, arguments);

args[0].deleted = {'$ne': true};
if (use$neOperator) {
args[0].deleted = {'$ne': true};
} else {
args[0].deleted = false;
}

return Model[method].apply(this, args);
};

schema.statics[method + 'Deleted'] = function () {
var args = parseUpdateArguments.apply(undefined, arguments);

args[0].deleted = {'$ne': false};
if (use$neOperator) {
args[0].deleted = {'$ne': false};
} else {
args[0].deleted = true;
}

return Model[method].apply(this, args);
};
Expand All @@ -165,8 +187,8 @@ module.exports = function (schema, options) {

schema.methods.delete = function (deletedBy, cb) {
if (typeof deletedBy === 'function') {
cb = deletedBy
deletedBy = null
cb = deletedBy;
deletedBy = null;
}

this.deleted = true;
Expand Down Expand Up @@ -212,7 +234,7 @@ module.exports = function (schema, options) {
if (this.updateWithDeleted) {
return this.updateWithDeleted(conditions, doc, { multi: true }, callback);
} else {
return this.update(conditions, doc, { multi: true }, callback);
return this[mainUpdateMethod](conditions, doc, { multi: true }, callback);
}
};

Expand Down Expand Up @@ -251,7 +273,7 @@ module.exports = function (schema, options) {
if (this.updateWithDeleted) {
return this.updateWithDeleted(conditions, doc, { multi: true }, callback);
} else {
return this.update(conditions, doc, { multi: true }, callback);
return this[mainUpdateMethod](conditions, doc, { multi: true }, callback);
}
};
};
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "mongoose-delete",
"version": "0.5.0",
"version": "0.5.1",
"description": "Mongoose soft delete plugin",
"author": "Sanel Deljkic <dsanel@gmail.com> (http://dsanel.github.io/)",
"main": "index.js",
Expand Down
Loading

0 comments on commit 530050d

Please sign in to comment.