-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.js
131 lines (109 loc) · 4.26 KB
/
index.js
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
let mongoose = require('mongoose');
let Model = mongoose.Model;
// mongoose.plugin(softDelete, { index: 'deleteAt', deletedAt: 'custom date function' });
module.exports = exports = function (schema, opts) {
opts = opts || {};
let indexField = 'deletedAt';
function setIndexFieldType(opts) {
if (opts.hasOwnProperty(indexField) && opts[indexField] ) {
return opts[indexField];
}
return new Date();
}
function getSoftDeleteSchema() {
if (typeof indexFieldType === 'function') {
return { [indexField]: eval(indexFieldType), deleted: true }
}
else {
return { [indexField]: indexFieldType, deleted: true }
}
}
/*
For custom index field value
for example instead of deletedAt=true you can pass deletedAt=[custom date function]
if you pass function to the index field then make sure it returns types supported by the mongoose
schema types.
*/
let indexFieldType = setIndexFieldType(opts);
let defaultType = typeof indexFieldType
/*
For custom index field name
for example: instead of deletedAt => delete
*/
if (opts.hasOwnProperty('index')) {
if (opts.hasOwnProperty(opts.index)) {
if (typeof opts.index === 'string') {
indexField = opts.index;
} else {
throw "Index field should be a string only."
}
} else {
throw "Index field should be defined."
}
}
if (indexFieldType && indexField) {
schema.add({
[indexField] : {
type: defaultType,
index: true
},
deleted: {
type: Boolean,
default: false
}
});
} else {
throw "The value of index can be custom date function, true or any value that you want to set." +
" Except: null, undefined and false."
}
let overrideMethods = [
'find',
'findByIdAndUpdate',
'findOne',
'findOneAndUpdate',
'replaceOne',
'updateMany',
'updateOne'
];
overrideMethods.forEach(function (method) {
schema.statics[method] = function () {
return Model[method].apply(this, arguments).where('deleted').equals(false);
};
schema.statics[method + 'Deleted'] = function () {
return Model[method].apply(this, arguments).where('deleted').equals(true);
};
schema.statics[method + 'WithDeleted'] = function () {
return Model[method].apply(this, arguments);
};
});
schema.statics.findById = function () {
return Model['findOne'].apply(this, {}).where('_id', arguments[0]).where('deleted').equals(false)
}
schema.statics.findByIdDeleted = function () {
return Model['findOne'].apply(this, {}).where('_id', arguments[0]).where('deleted').equals(true)
}
schema.statics.findOneAndRemove = function (conditions, callback) {
return this.findOneAndUpdate(conditions, getSoftDeleteSchema(), { new: true }, callback);
};
schema.statics.findOneAndDelete = function (conditions, callback) {
return this.findOneAndUpdate(conditions, getSoftDeleteSchema(), { new: true }, callback);
};
schema.statics.findByIdAndRemove = function (id, options = {}, callback) {
return this.findByIdAndUpdate(id, getSoftDeleteSchema(), options, callback);
};
schema.statics.findByIdAndDelete = function (id, options = {}, callback) {
return this.findByIdAndUpdate(id, getSoftDeleteSchema(), options, callback);
};
schema.statics.removeOne = function (conditions, options = {}, callback) {
return this.updateOne(conditions, getSoftDeleteSchema(), options, callback);
};
schema.statics.removeMany = function (conditions, options = {}, callback) {
return this.updateMany(conditions, getSoftDeleteSchema(), options, callback);
};
/*
This function helps you to restore the soft deleted document based on the conditions.
*/
schema.statics.restore = function (conditions, options = {}, callback) {
return this.updateManyWithDeleted(conditions, { [indexField]: null, deleted: false }, options, callback);
};
};