Skip to content

Instantly share code, notes, and snippets.

@astrotars
Created January 31, 2013 22:37
Show Gist options
  • Select an option

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

Select an option

Save astrotars/4687310 to your computer and use it in GitHub Desktop.
full sweepstakes model
/**
* Require Dependencies
*/
var Submission = require('./submission')
, mongooseQuery = require('mongoose-api-query')
, mongooseCreatedModified = require('mongoose-createdmodified').createdModifiedPlugin;
/**
* 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: {}
}
},
facebook: {
page: {
id: {
type: Number
},
name: {
type: String,
trim: true
},
username: {
type: String,
trim: true
}
},
tab: {
id: {
type: Number
},
position: {
type: Number
},
custom_name: {
type: String,
trim: true
},
custom_image_url: {
type: String,
trim: true
}
}
},
enabled: {
type: Boolean,
default: false
},
schedule: {
start: {
type: Date,
default: Date.now
},
end: {
type: Date,
default: Date.now
}
},
submissions: {
type: Number,
default: 0
},
terms_url: {
type: String,
trim: true
}
}, { minimize: false });
/**
* 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