Skip to content

Commit

Permalink
Merge pull request #22 from lethalbrains/master
Browse files Browse the repository at this point in the history
 Added 'touch' method to the plugin
  • Loading branch information
drudge committed Jan 27, 2015
2 parents 008aeef + edfdc1e commit 0ef540b
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 0 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ mongoose.plugin(timestamps, {
});
```

Any model's updatedAt attribute can be updated to the current time using `touch()`.

## License

(The MIT License)
Expand Down
7 changes: 7 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,13 @@ function timestampsPlugin(schema, options) {
next();
});
}

if(!schema.methods.hasOwnProperty('touch'))
schema.methods.touch = function(callback){
this[updatedAt] = new Date;
this.save(callback)
}

}

module.exports = timestampsPlugin;
47 changes: 47 additions & 0 deletions test/touch_test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@

/**
* @list dependencies
**/

var mocha = require('mocha');
var should = require('should');
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var timestamps = require('../');

mongoose.connect('mongodb://localhost/mongoose_timestamps')
mongoose.connection.on('error', function (err) {
console.error('MongoDB error: ' + err.message);
console.error('Make sure a mongoDB server is running and accessible by this application')
});

var UserSchema = new Schema({
email: String
})

UserSchema.plugin(timestamps)
var User = mongoose.model('User', UserSchema)

describe('User Schema', function(){
it('should have the method touch', function(){
UserSchema.methods.hasOwnProperty('touch').should.equal(true)
})
})

describe('timestamp',function(){
it('should be updated on calling the method touch', function(done){
var user = new User({email: "[email protected]"})
var past = undefined
user.save(function (err) {
past = user.updatedAt
});
setTimeout(function(){
user.touch(function(){
user.updatedAt.should.above(past)
done()
})
},1500)
})
})


0 comments on commit 0ef540b

Please sign in to comment.