Skip to content

Instantly share code, notes, and snippets.

@astrotars
Created January 16, 2013 23:56
Show Gist options
  • Select an option

  • Save astrotars/4552169 to your computer and use it in GitHub Desktop.

Select an option

Save astrotars/4552169 to your computer and use it in GitHub Desktop.
submissions schema
/**
* Require Dependencies
*/
var Sweepstakes = require('./sweepstakes');
/**
* Define Variables
*/
var Schema = mongoose.Schema,
ObjectId = Schema.ObjectId;
/**
* Schema Definition
*/
var submissionSchema = new Schema({
client_id: {
type: Schema.Types.ObjectId,
ref: 'Client',
index: true,
required: true
},
sweepstakes_id: {
type: Schema.Types.ObjectId,
ref: 'Sweepstakes',
index: true,
required: true
},
email: {
type: String,
index: true,
trim: true,
lowercase: true,
required: true
},
data: {
type: Schema.Types.Mixed,
default: []
}
});
/**
* Compound Uniques
*/
// letting application layer take care of validation.
// that way we can eventually allow for adding weight to an entry (multiple entries)
// submissionSchema.index({email: 1, sweepstakes_id: 1}, {unique: true});
/**
* Custom Middleware
*/
// invoked on call to save();
submissionSchema.pre('save', function(next) {
// increment submissions count for sweepstakes
Sweepstakes.update({ _id: this.sweepstakes_id }, { $inc: { submissions: 1 } }).exec();
next();
});
// invoked on call to remove();
// submissionSchema.pre('remove', function(next) {
// // decrement submissions count for sweepstakes
// Sweepstakes.update({ _id: this.sweepstakes_id }, { $inc: { submissions: -1 } }).exec();
// next();
// });
/**
* Third Party Plugins
*/
submissionSchema.plugin(mongooseQuery);
submissionSchema.plugin(mongooseCreatedModified, {index: true});
/**
* Export Model for Use Within Routes
*/
module.exports = mongoose.model('Submission', submissionSchema);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment