Skip to content

Instantly share code, notes, and snippets.

@iworkforthem
Created December 4, 2016 17:52
Show Gist options
  • Save iworkforthem/fd6ed60a78149195edda86f44f69032c to your computer and use it in GitHub Desktop.
Save iworkforthem/fd6ed60a78149195edda86f44f69032c to your computer and use it in GitHub Desktop.
reusable mongoose schema.
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
mongoose.Promise = Promise;
mongoose.connect('mongodb://127.0.0.1:27017/local');
var clickSchema = new Schema({
ip: { type: String, default: '' },
hostname: { type: String, default: '' },
country: { type: String, default: '' },
timestamp: { type: Number, default: 0 },
createDate: { type: Date, default: Date.now },
updateDate: { type: Date, default: Date.now }
});
clickSchema.pre('save', function(next) {
now = new Date();
this.updateDate = now;
if (!this.createDate) {
this.createDate = now;
}
next();
});
module.exports = mongoose.model('Click', clickSchema);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment