Created
December 4, 2016 17:52
-
-
Save iworkforthem/fd6ed60a78149195edda86f44f69032c to your computer and use it in GitHub Desktop.
reusable mongoose schema.
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'); | |
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