Skip to content

Commit 530050d

Browse files
committed
Handling #50, #56, #52. Adding additional tests
dsanel/mongoose-delete#52 Counter mongo index bug with $ne and count() dsanel/mongoose-delete#50 do not use $ne operator dsanel/mongoose-delete#56 Fix Mongoose DeprecationWarning: collection.update is deprecated
1 parent 4f46aac commit 530050d

File tree

5 files changed

+208
-15
lines changed

5 files changed

+208
-15
lines changed

CHANGELOG.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,12 @@
1+
## [v0.5.1]
2+
> September 3, 2019
3+
4+
- Add option to disable use of `$ne` operator using `{use$neOperator: false}` (@bdelville, @gabzim) #50
5+
- Fix Mongoose DeprecationWarning: collection.update is deprecated (@cardimajs, @jebarjonet)
6+
- Upgrade all `devDependencies` to latest versions
7+
- Fix security vulnerabilities in dependencies
8+
- Add additional tests for `updateMany`, `countDocuments`, `use$neOperator`
9+
110
## [v0.5.0]
211
> December 10, 2018
312

README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,11 @@ mongoose-delete is simple and lightweight plugin that enables soft deletion of d
1414
- [Add __deletedBy__ key to record who deleted document](#who-has-deleted-the-data)
1515
- Restore deleted documents using __restore__ method
1616
- [Bulk delete and restore](#bulk-delete-and-restore)
17-
- [Option to override static methods](#examples-how-to-override-one-or-multiple-methods) (__count, countDocuments, find, findOne, findOneAndUpdate, update__)
17+
- [Option to override static methods](#examples-how-to-override-one-or-multiple-methods) (__count, countDocuments, find, findOne, findOneAndUpdate, update, updateMany__)
1818
- [For overridden methods we have two additional methods](#method-overridden): __methodDeleted__ and __methodWithDeleted__
1919
- [Disable model validation on delete](#disable-model-validation-on-delete)
2020
- [Option to create index on delete fields](#create-index-on-fields) (__deleted__, __deletedAt__, __deletedBy__)
21+
- 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).
2122

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

182185
### Examples how to override one or multiple methods
183186

index.js

Lines changed: 35 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ var mongoose = require('mongoose'),
55

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

7979
module.exports = function (schema, options) {
8080
options = options || {};
81-
var indexFields = parseIndexFields(options)
81+
var indexFields = parseIndexFields(options);
8282

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

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

96+
var use$neOperator = true;
97+
if (options.use$neOperator !== undefined && typeof options.use$neOperator === "boolean") {
98+
use$neOperator = options.use$neOperator;
99+
}
100+
95101
schema.pre('save', function (next) {
96102
if (!this.deleted) {
97103
this.deleted = false;
@@ -101,7 +107,7 @@ module.exports = function (schema, options) {
101107

102108
if (options.overrideMethods) {
103109
var overrideItems = options.overrideMethods;
104-
var overridableMethods = ['count', 'countDocuments', 'find', 'findOne', 'findOneAndUpdate', 'update'];
110+
var overridableMethods = ['count', 'countDocuments', 'find', 'findOne', 'findOneAndUpdate', 'update', 'updateMany'];
105111
var finalList = [];
106112

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

127133
// countDocuments do not exist in Mongoose v4
128134
/* istanbul ignore next */
129-
if (method === 'countDocuments' && typeof Model.countDocuments !== 'function') {
135+
if (mongooseMajorVersion < 5 && method === 'countDocuments' && typeof Model.countDocuments !== 'function') {
130136
modelMethodName = 'count';
131137
}
132138

133139
schema.statics[method] = function () {
134-
return Model[modelMethodName].apply(this, arguments).where('deleted').ne(true);
140+
if (use$neOperator) {
141+
return Model[modelMethodName].apply(this, arguments).where('deleted').ne(true);
142+
} else {
143+
return Model[modelMethodName].apply(this, arguments).where({deleted: false});
144+
}
135145
};
136146
schema.statics[method + 'Deleted'] = function () {
137-
return Model[modelMethodName].apply(this, arguments).where('deleted').ne(false);
147+
if (use$neOperator) {
148+
return Model[modelMethodName].apply(this, arguments).where('deleted').ne(false);
149+
} else {
150+
return Model[modelMethodName].apply(this, arguments).where({deleted: true});
151+
}
138152
};
139153
schema.statics[method + 'WithDeleted'] = function () {
140154
return Model[modelMethodName].apply(this, arguments);
@@ -143,15 +157,23 @@ module.exports = function (schema, options) {
143157
schema.statics[method] = function () {
144158
var args = parseUpdateArguments.apply(undefined, arguments);
145159

146-
args[0].deleted = {'$ne': true};
160+
if (use$neOperator) {
161+
args[0].deleted = {'$ne': true};
162+
} else {
163+
args[0].deleted = false;
164+
}
147165

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

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

154-
args[0].deleted = {'$ne': false};
172+
if (use$neOperator) {
173+
args[0].deleted = {'$ne': false};
174+
} else {
175+
args[0].deleted = true;
176+
}
155177

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

166188
schema.methods.delete = function (deletedBy, cb) {
167189
if (typeof deletedBy === 'function') {
168-
cb = deletedBy
169-
deletedBy = null
190+
cb = deletedBy;
191+
deletedBy = null;
170192
}
171193

172194
this.deleted = true;
@@ -212,7 +234,7 @@ module.exports = function (schema, options) {
212234
if (this.updateWithDeleted) {
213235
return this.updateWithDeleted(conditions, doc, { multi: true }, callback);
214236
} else {
215-
return this.update(conditions, doc, { multi: true }, callback);
237+
return this[mainUpdateMethod](conditions, doc, { multi: true }, callback);
216238
}
217239
};
218240

@@ -251,7 +273,7 @@ module.exports = function (schema, options) {
251273
if (this.updateWithDeleted) {
252274
return this.updateWithDeleted(conditions, doc, { multi: true }, callback);
253275
} else {
254-
return this.update(conditions, doc, { multi: true }, callback);
276+
return this[mainUpdateMethod](conditions, doc, { multi: true }, callback);
255277
}
256278
};
257279
};

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "mongoose-delete",
3-
"version": "0.5.0",
3+
"version": "0.5.1",
44
"description": "Mongoose soft delete plugin",
55
"author": "Sanel Deljkic <dsanel@gmail.com> (http://dsanel.github.io/)",
66
"main": "index.js",

0 commit comments

Comments
 (0)