-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
74 lines (55 loc) · 2.21 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import * as mongoose from 'mongoose';
export default function SoftDeletePlugin (schema, options) {
schema.add({ deleted: { type: Boolean, default: false }});
schema.statics.deleteOne = function(filters: any) {
return this.updateOne(filters, {deleted: true});
};
schema.statics.deleteMany = function(filters: any) {
return this.updateMany(filters, {deleted: true});
};
schema.statics.hardDeleteOne = function(filters: any) {
return mongoose.Model.deleteOne.apply(this, arguments);
};
schema.statics.hardDeleteMany= function(filters: any) {
return mongoose.Model.deleteMany.apply(this, arguments);
};
schema.statics.find = function() {
return mongoose.Model.find.apply(this, arguments).where('deleted').ne(true);
};
schema.statics.findOne = function() {
return mongoose.Model.findOne.apply(this, arguments).where('deleted').ne(true);
};
schema.statics.findWithDeleted = function() {
return mongoose.Model.find.apply(this, arguments);
};
schema.statics.findOneWithDeleted = function() {
return mongoose.Model.findOne.apply(this, arguments);
};
schema.statics.update = function() {
return mongoose.Model.update.apply(this, arguments).where('deleted').ne(true);
};
schema.statics.updateOne = function() {
return mongoose.Model.updateOne.apply(this, arguments).where('deleted').ne(true);
};
schema.statics.updateWithDeleted = function() {
return mongoose.Model.update.apply(this, arguments);
};
schema.statics.updateOneWithDeleted = function() {
return mongoose.Model.updateOne.apply(this, arguments);
};
schema.statics.findOneAndUpdate = function() {
return mongoose.Model.findOneAndUpdate.apply(this, arguments).where('deleted').ne(true);
};
schema.statics.findOneAndUpdateWithDeleted = function() {
return mongoose.Model.findOneAndUpdate.apply(this, arguments);
};
schema.statics.countDocuments = function() {
return mongoose.Model.countDocuments.apply(this, arguments).where('deleted').ne(true);
}
schema.statics.countDocumentsWithDeleted = function() {
return mongoose.Model.countDocuments.apply(this, arguments);
}
if (options && options.index) {
schema.path('deleted').index(options.index);
}
}