Skip to content

Instantly share code, notes, and snippets.

View astrotars's full-sized avatar
⛰️

Nick Parsons astrotars

⛰️
View GitHub Profile
@astrotars
astrotars / gist:4503140
Created January 10, 2013 15:55
submission.js
var Schema = mongoose.Schema,
ObjectId = Schema.ObjectId;
var submissionSchema = new Schema({
sweepstakes_id: {
type: Schema.Types.ObjectId,
ref: 'Sweepstakes'
},
email: {
type: String,
@astrotars
astrotars / gist:4504221
Created January 10, 2013 17:52
REST Structure
/**
* Clients
*/
app.post('/clients', routes.clients.create);
app.get('/clients', routes.clients.list);
app.get('/clients/:client_id', routes.clients.get);
app.get('/clients/:client_id/sweepstakes', routes.sweepstakes.get);
app.put('/clients/:client_id', routes.clients.update);
@astrotars
astrotars / gist:4512985
Created January 11, 2013 18:50
API routes. Pending to change.
/**
* Require Routes
*/
var requireDir = require('require-dir'),
routes = requireDir('.');
/**
* Clients
*/
var Schema = mongoose.Schema,
ObjectId = Schema.ObjectId;
var userSchema = new Schema({
email: {
type: String,
index: true,
unique: true
},
password: String,
{
"design": {
"images": [
{
"enabled": true,
"url": "https://sweeps-dev.s3.amazonaws.com/a9b0d9a0ac9af495c88ada6695152ef4.png",
"height": 1000,
"width": 810
}
],
@astrotars
astrotars / gist:4542814
Created January 15, 2013 22:35
Update w/ Mongoose [casts values to appropriate types but does not run validators, middleware, defaults, or setters]
// assign update client to var
var updateclient = req.body;
remove default to avoid mongo errors (like Mod on _id not allowed)
delete updateclient._id;
Client.update({_id: req.params.client_id}, updateclient, {}, function(err, client) {
if (err)
return next(new restify.InternalError(err));
domain.js:66
throw er;
^
TypeError: Object function model(doc, fields, skipId) {
if (!(this instanceof model))
return new model(doc, fields, skipId);
Model.call(this, doc, fields, skipId);
} has no method 'pre'
at Promise.exports.destroy (/Applications/MAMP/htdocs/apps/labs/sweeps-api/routes/clients.js:82:10)
at Promise.addBack (/Applications/MAMP/htdocs/apps/labs/sweeps-api/node_modules/mongoose/lib/promise.js:128:8)
@astrotars
astrotars / schema.js
Created January 16, 2013 17:57
extract id from clients array
clientSchema.pre('remove', function(next) {
// clean up dependencies
Sweepstakes.remove({client_id: this._id}).exec();
Submission.remove({client_id: this._id}).exec();
// remove client id from users
User.update({clients: {$in: [this._id]}}, {$pull: {clients: this._id}}, function(err, numAffected) {
if (err)
@astrotars
astrotars / gist:4552169
Created January 16, 2013 23:56
submissions schema
/**
* Require Dependencies
*/
var Sweepstakes = require('./sweepstakes');
/**
* Define Variables
*/
@astrotars
astrotars / gist:4552176
Created January 16, 2013 23:57
sweepstakes schema
/**
* Require Dependencies
*/
var Submission = require('./submission');
/**
* Define Variables
*/