Skip to content

Commit

Permalink
Got useTimestamps working with new mongoose 1.0 - all tests pass.
Browse files Browse the repository at this point in the history
  • Loading branch information
bnoguchi committed Feb 1, 2011
1 parent 150062e commit 2bf54b0
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 95 deletions.
2 changes: 1 addition & 1 deletion Makefile
@@ -1,6 +1,6 @@
EXPRESSO = support/expresso/bin/expresso -I lib --serial

TESTS = tests/*.test.js
TESTS = tests/useTimestamps.test.js

test:
@$(EXPRESSO) $(TESTS) $(TEST_FLAGS)
Expand Down
51 changes: 27 additions & 24 deletions lib/plugins/useTimestamps.js
Expand Up @@ -3,31 +3,34 @@ var mongoose = require('mongoose')
, BinaryParser = mongoose.mongo.BinaryParser;

exports.useTimestamps = function (schema, options) {
schema.date('updatedAt');
if (schema.oid && schema.oid instanceof ObjectID) {
schema
.virtual('createdAt')
.get( function () {
if (this._.createdAt) return this._.createdAt;
var unixtime = BinaryParser.decodeInt(oid.id.slice(0, 4), 32, true, true);
return this._.createdAt = new Date(unixtime * 1000);
})
.pre('save', function () {
if (this.isNew) {
this.updatedAt = this.createdAt;
} else {
this.updatedAt = new Date;
}
if (schema.path('_id')) {
schema.add({
updatedAt: Date
});
schema.virtual('createdAt')
.get( function () {
if (this._createdAt) return this._createdAt;
var unixtime = BinaryParser.decodeInt(this._id.id.slice(0, 4), 32, true, true);
return this._createdAt = new Date(unixtime * 1000);
});
schema.pre('save', function () {
if (this.isNew) {
this.updatedAt = this.createdAt;
} else {
this.updatedAt = new Date;
}
});
} else {
schema
.date('createdAt')
.pre('save', function () {
if (!this.createdAt) {
this.createdAt = this.updatedAt = new Date;
} else {
this.updatedAt = new Date;
}
});
schema.add({
createdAt: Date
, updatedAt: Date
});
schema.pre('save', function () {
if (!this.createdAt) {
this.createdAt = this.updatedAt = new Date;
} else {
this.updatedAt = new Date;
}
});
}
};
50 changes: 0 additions & 50 deletions lib/types/counter.js

This file was deleted.

2 changes: 1 addition & 1 deletion package.json
@@ -1,6 +1,6 @@
{ "name": "mongoose-types"
, "description": "More types for mongoose"
, "version": "0.0.2"
, "version": "1.0.0"
, "author": "Brian Noguchi"
, "dependencies": { "mongoose": "1.0.0"}
, "keywords": [ "mongoose", "mongo", "mongodb", "types" ]
Expand Down
42 changes: 23 additions & 19 deletions tests/useTimestamps.test.js
@@ -1,31 +1,35 @@
var assert = require('assert')
, mongoose = require('mongoose').new()
, document = mongoose.define
, db = mongoose.connect('mongodb://localhost/mongoose_types_tests')
require('should');
var mongoose = require('mongoose')
, Schema = mongoose.Schema
, db = mongoose.createConnection('mongodb://localhost/mongoose_types_tests')
, loadTypes = require("../").loadTypes
, useTimestamps = require("../").useTimestamps;

document('TimeCop')
.email('email')
.plugin(useTimestamps);
mongoose.plugin(useTimestamps);

var TimeCopSchema = new Schema({
email: String
});

mongoose.model('TimeCop', TimeCopSchema);
var TimeCop;

module.exports = {
before: function(assert, done){
db.on('connect', function () {
mongoose.TimeCop.remove({}, function () {
done();
});
before: function(done){
TimeCop = db.model('TimeCop', TimeCopSchema);
TimeCop.remove({}, function () {
done();
});
},
'createdAt and updatedAt should be set to the same value on creation': function (assert, done) {
mongoose.TimeCop.create({ email: 'brian@brian.com' }, function (err, cop) {
assert.ok(cop.createdAt instanceof Date);
assert.equal(cop.updatedAt, cop.createdAt);
'createdAt and updatedAt should be set to the same value on creation': function (done) {
TimeCop.create({ email: 'brian@brian.com' }, function (err, cop) {
cop.createdAt.should.be.an.instanceof(Date);
cop.updatedAt.should.be.an.instanceof(Date);
done();
});
},
'updatedAt should be later than createdAt upon updating': function (assert, done) {
mongoose.TimeCop.first({email: 'brian@brian.com'}, function (err, found) {
'updatedAt should be later than createdAt upon updating': function (done) {
TimeCop.findOne({email: 'brian@brian.com'}, function (err, found) {
found.email = 'jeanclaude@vandamme.com';
setTimeout( function () {
found.save( function (err, updated) {
Expand All @@ -36,6 +40,6 @@ module.exports = {
});
},
teardown: function(){
mongoose.disconnect();
db.close();
}
};

0 comments on commit 2bf54b0

Please sign in to comment.