Skip to content

Instantly share code, notes, and snippets.

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

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

Select an option

Save astrotars/4552176 to your computer and use it in GitHub Desktop.
sweepstakes schema
/**
* Require Dependencies
*/
var Submission = require('./submission');
/**
* Define Variables
*/
var Schema = mongoose.Schema,
ObjectId = Schema.ObjectId;
/**
* Schema Definition
*/
var sweepstakesSchema = new Schema({
client_id: {
type: Schema.Types.ObjectId,
ref: 'Client',
index: true,
required: true
},
name: {
type: String,
trim: true,
default: 'Sweepstakes',
},
design: {
images: {
type: [],
default: []
},
elements: {
type: [],
default: []
}
},
enabled: {
type: Boolean,
default: false
},
schedule: {
start: {
type: Date,
default: Date.now
},
end: {
type: Date,
default: Date.now
}
},
submissions: {
type: Number,
default: 0
}
});
/**
* Custom Middleware
*/
// invoked on call to the remove();
sweepstakesSchema.pre('remove', function(next) {
// clean up dependencies
Submission.remove({ sweepstakes_id: this._id }).exec();
next();
});
/**
* Third Party Plugins
*/
sweepstakesSchema.plugin(mongooseQuery);
sweepstakesSchema.plugin(mongooseCreatedModified, {index: true});
/**
* Export Model for Use Within Routes
*/
module.exports = mongoose.model('Sweepstakes', sweepstakesSchema);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment