Skip to content

Instantly share code, notes, and snippets.

@wavded
Created March 15, 2011 17:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wavded/871039 to your computer and use it in GitHub Desktop.
Save wavded/871039 to your computer and use it in GitHub Desktop.
Arc.js
/**
* 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