Created
January 16, 2013 23:56
-
-
Save astrotars/4552169 to your computer and use it in GitHub Desktop.
submissions schema
This file contains hidden or 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
| /** | |
| * 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