Created
November 1, 2011 15:27
-
-
Save leesolutions/1330798 to your computer and use it in GitHub Desktop.
mongoose types useTimestamps plugin w/ if block removed
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var mongoose = require('mongoose') | |
, ObjectID = mongoose.ObjectID | |
, BinaryParser = mongoose.mongo.BinaryParser; | |
exports.useTimestamps = function (schema, options) { | |
schema.add({ | |
createdAt: Date | |
, updatedAt: Date | |
}); | |
schema.pre('save', function (next) { | |
if (!this.createdAt) { | |
this.createdAt = this.updatedAt = new Date; | |
} else { | |
this.updatedAt = new Date; | |
} | |
next(); | |
}); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment