Created
March 15, 2011 17:00
-
-
Save wavded/871039 to your computer and use it in GitHub Desktop.
Arc.js
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
/** | |
* Arc model | |
* | |
* @module Arc | |
* @requires mongoose (npm install mongoose) | |
* @requires User (models/User.js) | |
* @global mongoose | |
*/ | |
var Schema = mongoose.Schema, | |
User = require('./User.js'); | |
var ArcSchema = new Schema({ | |
userId : { type: String, required: true, index: true }, | |
isoCode : { type: String, match: /ISO-\w{3}-\d{4}/, required: true }, | |
parcelId : { type: String }, | |
lon : { type: Number }, | |
lat : { type: Number } | |
}); | |
ArcSchema.pre('save',function(next){ | |
if(this.isNew){ // check new entries | |
User.findById(this.userId, {userId: 1}, function(res,user){ | |
if(user){ | |
next(); | |
} else { | |
next(new Error('No user exists')); | |
} | |
}); | |
} else { | |
next(); | |
} | |
}); | |
ArcSchema.pre('init',function(next){ | |
if(!this.isoCode) this.isoCode = "ISO-XXX-0000"; // dummy code for now | |
}); | |
mongoose.model('Arc', ArcSchema); | |
module.exports = mongoose.model('Arc'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment