File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -47,6 +47,8 @@ mongoose.plugin(timestamps, {
4747});
4848```
4949
50+ Any model's updatedAt attribute can be updated to the current time using ` touch() ` .
51+
5052## License
5153
5254(The MIT License)
Original file line number Diff line number Diff line change @@ -55,6 +55,13 @@ function timestampsPlugin(schema, options) {
5555 next ( ) ;
5656 } ) ;
5757 }
58+
59+ if ( ! schema . methods . hasOwnProperty ( 'touch' ) )
60+ schema . methods . touch = function ( callback ) {
61+ this [ updatedAt ] = new Date ;
62+ this . save ( callback )
63+ }
64+
5865}
5966
6067module . exports = timestampsPlugin ;
Original file line number Diff line number Diff line change 1+
2+ /**
3+ * @list dependencies
4+ **/
5+
6+ var mocha = require ( 'mocha' ) ;
7+ var should = require ( 'should' ) ;
8+ var mongoose = require ( 'mongoose' ) ;
9+ var Schema = mongoose . Schema ;
10+ var timestamps = require ( '../' ) ;
11+
12+ mongoose . connect ( 'mongodb://localhost/mongoose_timestamps' )
13+ mongoose . connection . on ( 'error' , function ( err ) {
14+ console . error ( 'MongoDB error: ' + err . message ) ;
15+ console . error ( 'Make sure a mongoDB server is running and accessible by this application' )
16+ } ) ;
17+
18+ var UserSchema = new Schema ( {
19+ email : String
20+ } )
21+
22+ UserSchema . plugin ( timestamps )
23+ var User = mongoose . model ( 'User' , UserSchema )
24+
25+ describe ( 'User Schema' , function ( ) {
26+ it ( 'should have the method touch' , function ( ) {
27+ UserSchema . methods . hasOwnProperty ( 'touch' ) . should . equal ( true )
28+ } )
29+ } )
30+
31+ describe ( 'timestamp' , function ( ) {
32+ it ( 'should be updated on calling the method touch' , function ( done ) {
33+ var user = new User ( { email : "tyrion.lannister@westeros.com" } )
34+ var past = undefined
35+ user . save ( function ( err ) {
36+ past = user . updatedAt
37+ } ) ;
38+ setTimeout ( function ( ) {
39+ user . touch ( function ( ) {
40+ user . updatedAt . should . above ( past )
41+ done ( )
42+ } )
43+ } , 1500 )
44+ } )
45+ } )
46+
47+
You can’t perform that action at this time.
0 commit comments