-
Notifications
You must be signed in to change notification settings - Fork 63
Expand file tree
/
Copy pathindex.js
More file actions
129 lines (111 loc) · 3.72 KB
/
index.js
File metadata and controls
129 lines (111 loc) · 3.72 KB
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
/*!
* Mongoose Timestamps Plugin
* Copyright(c) 2012 Nicholas Penree <nick@penree.com>
* 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;
}
function setupCreatedVirtual(){
schema.virtual(createdAt)
.get( function () {
if (this["_" + createdAt]) return this["_" + createdAt];
return this["_" + createdAt] = this._id.getTimestamp();
});
}
function setupPreSave(condition,createdAt){
schema.pre('save', function(next) {
if (condition(this)) {
var newDate = new Date;
if (createdAt) this[createdAt] = newDate;
if (updatedAt) this[updatedAt] = newDate;
} else if (this.isModified() && updatedAt) {
this[updatedAt] = new Date;
}
next();
});
}
if (schema.path(createdAt)) {
if (!schema.path(updatedAt) && updatedAt) {
schema.add(dataObj);
}
if (schema.virtual(createdAt).get) {
setupCreatedVirtual();
}
setupPreSave(function(that){return that.isNew; },createdAt); //Also update createdAt
} else {
if (dataObj[updatedAt]) {
schema.add(dataObj);
}
setupPreSave(function(that){return !that[createdAt]; }); //do not create createdAt in db, so undefined in second parameter
setupCreatedVirtual();
}
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;