diff --git a/index.js b/index.js index 0dd97b6..ccf1f57 100644 --- a/index.js +++ b/index.js @@ -52,12 +52,12 @@ function timestampsPlugin(schema, options) { }); } schema.pre('save', function(next) { + var newDate = Date.now() 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; + this[updatedAt] = newDate; } next(); }); @@ -70,12 +70,12 @@ function timestampsPlugin(schema, options) { schema.add(dataObj); } schema.pre('save', function(next) { + var newDate = Date.now() 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; + this[updatedAt] = newDate; } next(); }); @@ -83,7 +83,7 @@ function timestampsPlugin(schema, options) { schema.pre('findOneAndUpdate', function(next) { if (this.op === 'findOneAndUpdate') { - var newDate = new Date; + var newDate = Date.now(); this._update = this._update || {}; if (createdAt) { if (this._update[createdAt]) { @@ -102,7 +102,7 @@ function timestampsPlugin(schema, options) { schema.pre('update', function(next) { if (this.op === 'update') { - var newDate = new Date; + var newDate = Date.now(); this._update = this._update || {}; if (createdAt) { if (this._update[createdAt]) { @@ -121,7 +121,7 @@ function timestampsPlugin(schema, options) { if(!schema.methods.hasOwnProperty('touch') && updatedAt) schema.methods.touch = function(callback){ - this[updatedAt] = new Date; + this[updatedAt] = Date.now(); this.save(callback); } diff --git a/test/custom_names_only_test.js b/test/custom_names_only_test.js index 1e6eab0..76c3dab 100644 --- a/test/custom_names_only_test.js +++ b/test/custom_names_only_test.js @@ -33,7 +33,7 @@ describe('timestamps custom names only', function() { done(); }); }); - + it('should have the createdAt field named "' + opts.createdAt + '" with type "Date"', function(done) { var customCop = new CustomizedNameOnlyTimeCop({email: 'example@example.com'}); customCop.save(function (err) { @@ -42,15 +42,15 @@ describe('timestamps custom names only', function() { done(); }); }); - + it('should be set to the same value on creation', function(done) { var cop = new CustomizedNameOnlyTimeCop({ email: 'brian@brian.com' }); cop.save( function (err) { - cop[opts.createdAt].should.equal(cop[opts.updatedAt]); + cop[opts.createdAt].should.eql(cop[opts.updatedAt]); done(); }); }); - + it('should have updatedAt greater than createdAt upon updating', function(done) { CustomizedNameOnlyTimeCop.findOne({email: 'brian@brian.com'}, function (err, found) { found.email = 'jeanclaude@vandamme.com'; @@ -62,7 +62,7 @@ describe('timestamps custom names only', function() { }, 1000); }); }); - + after(function() { mongoose.close(); }); diff --git a/test/index_test.js b/test/index_test.js index 7e69740..9bf2ae1 100644 --- a/test/index_test.js +++ b/test/index_test.js @@ -26,11 +26,11 @@ describe('timestamps', function() { it('should be set to the same value on creation', function(done) { var cop = new TimeCop({ email: 'brian@brian.com' }); cop.save( function (err) { - cop.createdAt.should.equal(cop.updatedAt); + cop.createdAt.should.eql(cop.updatedAt); done(); }); }) - + it('should have updatedAt greater than createdAt upon updating', function(done) { TimeCop.findOne({email: 'brian@brian.com'}, function (err, found) { found.email = 'jeanclaude@vandamme.com';