Skip to content

Allows type Number on property definition #39

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});
Expand All @@ -70,20 +70,20 @@ 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();
});
}

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]) {
Expand All @@ -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]) {
Expand All @@ -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);
}

Expand Down
10 changes: 5 additions & 5 deletions test/custom_names_only_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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: '[email protected]'});
customCop.save(function (err) {
Expand All @@ -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: '[email protected]' });
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: '[email protected]'}, function (err, found) {
found.email = '[email protected]';
Expand All @@ -62,7 +62,7 @@ describe('timestamps custom names only', function() {
}, 1000);
});
});

after(function() {
mongoose.close();
});
Expand Down
4 changes: 2 additions & 2 deletions test/index_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,11 @@ describe('timestamps', function() {
it('should be set to the same value on creation', function(done) {
var cop = new TimeCop({ email: '[email protected]' });
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: '[email protected]'}, function (err, found) {
found.email = '[email protected]';
Expand Down