mongoose-plugin.js
1.3 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
// # boot - mongoose plugin
var _ = require('underscore')
var jsonSelect = require('mongoose-json-select')
exports = module.exports = function(settings) {
return function(Schema) {
// NOTE: To allow `.sort('-created_at')` to work
// we need to have these as actual paths
Schema.add({
updated_at: Date,
created_at: Date
})
if (settings.mongooseVersioning)
Schema.add({
__versions: []
})
Schema.pre('save', function(next) {
var that = this
that.updated_at = (!that.created_at) ? Date.now() : that._id.getTimestamp()
if (!that.created_at)
that.created_at = that._id.getTimestamp()
// version history (alternative to `mongoose-version` and `mongoose-history`)
if (settings.mongooseVersioning) {
var version = that.toObject()
version.__v = _.isNumber(version.__v) ? version.__v + 1 : 0
delete version.__versions
that.__versions.push(version)
}
next()
})
Schema.set('toObject', {
virtuals: true,
getters: true
})
Schema.set('toJSON', {
virtuals: true,
getters: true
})
Schema.plugin(jsonSelect, '-_id -__v')
return Schema
}
}
exports['@singleton'] = true
exports['@require'] = [ 'igloo/settings' ]