Skip to content

Commit 65aad31

Browse files
authored
Merge pull request #54 from dsanel/ver-0-5-0
Ver 0.5.0
2 parents ae5692e + dc186e1 commit 65aad31

File tree

7 files changed

+1269
-253
lines changed

7 files changed

+1269
-253
lines changed

.travis.yml

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
language: node_js
2-
node_js:
3-
- 0.12
2+
node_js: [10, 9, 8, 7, 6, 5, 4]
43
services:
54
- mongodb
65
script: "npm run-script test-travis"
7-
after_script: "npm install coveralls@2.11.6 && cat ./coverage/lcov.info | coveralls"
6+
after_script: "npm install coveralls@3.0.2 && cat ./coverage/lcov.info | coveralls"

CHANGELOG.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,14 @@
1+
## [v0.5.0]
2+
> December 10, 2018
3+
4+
- Add support to mongoose 5.x (@joelmukuthu, @gforge)
5+
- Add `deleteById` static method #16
6+
- Add `countDocuments` method with related override methods (only for v5 Mongoose) #45
7+
- Upgrade all `devDependencies` to latest versions
8+
- Setup `.travis.yml` to test plugin on Node: 10, 9, 8, 7, 6, 5, 4
9+
- Setup `.travis.yml` to use `coveralls@3.0.2`
10+
- Add additional tests
11+
112
## [v0.4.0]
213
> July 10, 2016
314

README.md

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,13 @@ mongoose-delete is simple and lightweight plugin that enables soft deletion of d
88

99
## Features
1010
- [Add __delete()__ method on document (do not override standard __remove()__ method)](#simple-usage)
11+
- [Add __deleteById()__ static method](#simple-usage)
1112
- [Add __deleted__ (true-false) key on document](#simple-usage)
1213
- [Add __deletedAt__ key to store time of deletion](#save-time-of-deletion)
1314
- [Add __deletedBy__ key to record who deleted document](#who-has-deleted-the-data)
1415
- Restore deleted documents using __restore__ method
1516
- [Bulk delete and restore](#bulk-delete-and-restore)
16-
- [Option to override static methods](#examples-how-to-override-one-or-multiple-methods) (__count, find, findOne, findOneAndUpdate, update__)
17+
- [Option to override static methods](#examples-how-to-override-one-or-multiple-methods) (__count, countDocuments, find, findOne, findOneAndUpdate, update__)
1718
- [For overridden methods we have two additional methods](#method-overridden): __methodDeleted__ and __methodWithDeleted__
1819
- [Disable model validation on delete](#disable-model-validation-on-delete)
1920
- [Option to create index on delete fields](#create-index-on-fields) (__deleted__, __deletedAt__, __deletedBy__)
@@ -55,6 +56,14 @@ fluffy.save(function () {
5556
});
5657

5758
});
59+
60+
var examplePetId = mongoose.Types.ObjectId("53da93b16b4a6670076b16bf");
61+
62+
// INFO: Example usage of deleteById static method
63+
Pet.deleteById(examplePetId, function (err, petDocument) {
64+
// mongodb: { deleted: true, name: 'Fluffy', _id: '53da93b1...' }
65+
});
66+
5867
```
5968

6069

@@ -187,7 +196,7 @@ PetSchema.plugin(mongoose_delete, { overrideMethods: true });
187196
// Overide only specific methods
188197
PetSchema.plugin(mongoose_delete, { overrideMethods: ['count', 'find', 'findOne', 'findOneAndUpdate', 'update'] });
189198
// or
190-
PetSchema.plugin(mongoose_delete, { overrideMethods: ['count', 'find'] });
199+
PetSchema.plugin(mongoose_delete, { overrideMethods: ['count', 'countDocuments', 'find'] });
191200
// or (unrecognized method names will be ignored)
192201
PetSchema.plugin(mongoose_delete, { overrideMethods: ['count', 'find', 'errorXyz'] });
193202

index.js

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ module.exports = function (schema, options) {
101101

102102
if (options.overrideMethods) {
103103
var overrideItems = options.overrideMethods;
104-
var overridableMethods = ['count', 'find', 'findOne', 'findOneAndUpdate', 'update'];
104+
var overridableMethods = ['count', 'countDocuments', 'find', 'findOne', 'findOneAndUpdate', 'update'];
105105
var finalList = [];
106106

107107
if ((typeof overrideItems === 'string' || overrideItems instanceof String) && overrideItems === 'all') {
@@ -121,15 +121,23 @@ module.exports = function (schema, options) {
121121
}
122122

123123
finalList.forEach(function(method) {
124-
if (method === 'count' || method === 'find' || method === 'findOne') {
124+
if (['count', 'countDocuments', 'find', 'findOne'].indexOf(method) > -1) {
125+
var modelMethodName = method;
126+
127+
// countDocuments do not exist in Mongoose v4
128+
/* istanbul ignore next */
129+
if (method === 'countDocuments' && typeof Model.countDocuments !== 'function') {
130+
modelMethodName = 'count';
131+
}
132+
125133
schema.statics[method] = function () {
126-
return Model[method].apply(this, arguments).where('deleted').ne(true);
134+
return Model[modelMethodName].apply(this, arguments).where('deleted').ne(true);
127135
};
128136
schema.statics[method + 'Deleted'] = function () {
129-
return Model[method].apply(this, arguments).where('deleted').ne(false);
137+
return Model[modelMethodName].apply(this, arguments).where('deleted').ne(false);
130138
};
131139
schema.statics[method + 'WithDeleted'] = function () {
132-
return Model[method].apply(this, arguments);
140+
return Model[modelMethodName].apply(this, arguments);
133141
};
134142
} else {
135143
schema.statics[method] = function () {
@@ -208,6 +216,19 @@ module.exports = function (schema, options) {
208216
}
209217
};
210218

219+
schema.statics.deleteById = function (id, deletedBy, callback) {
220+
if (arguments.length === 0 || typeof id === 'function') {
221+
var msg = 'First argument is mandatory and must not be a function.';
222+
throw new TypeError(msg);
223+
}
224+
225+
var conditions = {
226+
_id: id
227+
};
228+
229+
return this.delete(conditions, deletedBy, callback);
230+
};
231+
211232
schema.methods.restore = function (callback) {
212233
this.deleted = false;
213234
this.deletedAt = undefined;

0 commit comments

Comments
 (0)