Skip to content

Instantly share code, notes, and snippets.

@leesolutions
Created November 1, 2011 15:27
Show Gist options
  • Save leesolutions/1330798 to your computer and use it in GitHub Desktop.
Save leesolutions/1330798 to your computer and use it in GitHub Desktop.
mongoose types useTimestamps plugin w/ if block removed
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