Skip to content

Commit bd04ffd

Browse files
committed
Added a useTimestamps plugin (with test), to automatically save a timestamp to a 'createdAt' and 'updatedAt' attribute on any schema.
1 parent a4ec6b4 commit bd04ffd

File tree

5 files changed

+71
-16
lines changed

5 files changed

+71
-16
lines changed

index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
module.exports = require("./lib/types");
1+
module.exports = require("./lib");

lib/index.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
exports.loadTypes = function () {
2+
var mongoose = arguments[0],
3+
types = Array.prototype.slice.call(arguments, 1);
4+
if (types.length) {
5+
types.forEach( function (type) {
6+
require("./types/" + type).loadType(mongoose);
7+
});
8+
} else {
9+
var files = require("fs").readdirSync(__dirname + "/types");
10+
files.forEach( function (filename) {
11+
var base = filename.slice(0, filename.length-3);
12+
require("./types/" + base).loadType(mongoose);
13+
});
14+
}
15+
};
16+
17+
exports.useTimestamps = require("./plugins/useTimestamps").useTimestamps;

lib/plugins/useTimestamps.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
exports.useTimestamps = function (schema, options) {
2+
schema
3+
.date('createdAt')
4+
.date('updatedAt')
5+
.pre('save', function () {
6+
if (!this.createdAt) {
7+
this.createdAt = this.updatedAt = new Date;
8+
} else {
9+
this.updatedAt = new Date;
10+
}
11+
});
12+
};

lib/types/index.js

Lines changed: 0 additions & 15 deletions
This file was deleted.

tests/useTimestamps.test.js

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
var assert = require('assert')
2+
, mongoose = require('mongoose').new()
3+
, document = mongoose.define
4+
, db = mongoose.connect('mongodb://localhost/mongoose_types_tests')
5+
, loadTypes = require("../").loadTypes
6+
, useTimestamps = require("../").useTimestamps;
7+
8+
document('TimeCop')
9+
.email('email')
10+
.plugin(useTimestamps);
11+
12+
module.exports = {
13+
before: function(assert, done){
14+
db.on('connect', function () {
15+
mongoose.TimeCop.remove({}, function () {
16+
done();
17+
});
18+
});
19+
},
20+
'createdAt and updatedAt should be set to the same value on creation': function (assert, done) {
21+
mongoose.TimeCop.create({ email: 'brian@brian.com' }, function (err, cop) {
22+
assert.ok(cop.createdAt instanceof Date);
23+
assert.equal(cop.updatedAt, cop.createdAt);
24+
done();
25+
});
26+
},
27+
'updatedAt should be later than createdAt upon updating': function (assert, done) {
28+
mongoose.TimeCop.first({email: 'brian@brian.com'}, function (err, found) {
29+
found.email = 'jeanclaude@vandamme.com';
30+
setTimeout( function () {
31+
found.save( function (err, updated) {
32+
assert.ok(updated.updatedAt > updated.createdAt);
33+
done();
34+
});
35+
}, 1000);
36+
});
37+
},
38+
teardown: function(){
39+
mongoose.disconnect();
40+
}
41+
};

0 commit comments

Comments
 (0)