-
Notifications
You must be signed in to change notification settings - Fork 64
/
index.js
130 lines (116 loc) · 3.83 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
/*!
* Mongoose Timestamps Plugin
* Copyright(c) 2012 Nicholas Penree <[email protected]>
* Original work Copyright(c) 2012 Brian Noguchi
* MIT Licensed
*/
var defaults = require('defaults');
function timestampsPlugin(schema, options) {
var updatedAt = 'updatedAt';
var createdAt = 'createdAt';
var updatedAtOpts = Date;
var createdAtOpts = Date;
var dataObj = {};
if (typeof options === 'object') {
if (typeof options.updatedAt === 'string') {
updatedAt = options.updatedAt;
} else if (typeof options.updatedAt === 'object') {
updatedAtOpts = defaults(options.updatedAt, {
name: updatedAt,
type: Date
});
updatedAt = updatedAtOpts.name;
}
if (typeof options.createdAt === 'string') {
createdAt = options.createdAt;
} else if (typeof options.createdAt === 'object') {
createdAtOpts = defaults(options.createdAt, {
name: createdAt,
type: Date
});
createdAt = createdAtOpts.name;
}
}
if (!schema.path(updatedAt) && updatedAt) {
dataObj[updatedAt] = updatedAtOpts;
}
if (schema.path(createdAt)) {
if (!schema.path(updatedAt) && updatedAt) {
schema.add(dataObj);
}
if (schema.virtual(createdAt).get) {
schema.virtual(createdAt)
.get( function () {
if (this["_" + createdAt]) return this["_" + createdAt];
return this["_" + createdAt] = this._id.getTimestamp();
});
}
schema.pre('save', function(next) {
if (this.isNew) {
var newDate = new Date;
if (createdAt) this[createdAt] = newDate;
if (updatedAt) this[updatedAt] = newDate;
} else if (this.isModified() && updatedAt) {
this[updatedAt] = new Date;
}
next();
});
} else {
if (createdAt) {
dataObj[createdAt] = createdAtOpts;
}
if (dataObj[createdAt] || dataObj[updatedAt]) {
schema.add(dataObj);
}
schema.pre('save', function(next) {
if (!this[createdAt]) {
var newDate = new Date;
if (createdAt) this[createdAt] = newDate;
if (updatedAt) this[updatedAt] = newDate;
} else if (this.isModified() && updatedAt) {
this[updatedAt] = new Date;
}
next();
});
}
schema.pre('findOneAndUpdate', function(next) {
if (this.op === 'findOneAndUpdate') {
var newDate = new Date;
this._update = this._update || {};
if (createdAt) {
if (this._update[createdAt]) {
delete this._update[createdAt];
}
this._update['$setOnInsert'] = this._update['$setOnInsert'] || {};
this._update['$setOnInsert'][createdAt] = newDate;
}
if (updatedAt) {
this._update[updatedAt] = newDate;
}
}
next();
});
schema.pre('update', function(next) {
if (this.op === 'update') {
var newDate = new Date;
this._update = this._update || {};
if (createdAt) {
if (this._update[createdAt]) {
delete this._update[createdAt];
}
this._update['$setOnInsert'] = this._update['$setOnInsert'] || {};
this._update['$setOnInsert'][createdAt] = newDate;
}
if (updatedAt) {
this._update[updatedAt] = newDate;
}
}
next();
});
if(!schema.methods.hasOwnProperty('touch') && updatedAt)
schema.methods.touch = function(callback){
this[updatedAt] = new Date;
this.save(callback);
}
}
module.exports = timestampsPlugin;