/index.js Secret
Created
February 20, 2012 16:29
This file contains 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
/** | |
* Client - CRUD/filter/ | |
* | |
*/ | |
module.exports = flow = { | |
'moduleName':'client', | |
'obj': null, | |
'model': null, | |
'db' : function(){ | |
// The contact embedded doc allows multiple | |
// contact details | |
var contact = new this.obj.Schema({ | |
key: { type: String, required: true }, // Form of contact | |
value: { type: String, required: true } // Contact detail | |
},{strict: true}); | |
// The assistant embedded doc allows multiple | |
// assistants and multiple contact details | |
// per assistant | |
var assistant = new this.obj.Schema({ | |
name: { type: String, required: true }, // Name of assistant | |
key: { type: String, required: true }, // Form of contact | |
value: { type: String, required: true } // Contact detail | |
},{strict: true}); | |
var clientSchema = new this.obj.Schema({ | |
firstname: { type: String, required: true, trim: true }, // The first name of the client | |
lastname: { type: String, required: true, trim: true }, // The second name of the client | |
password: { type: String, trim: true, index: true, required: true }, // The password of the client | |
tier: { type: Number, required: true, index: true, trim: true }, // The tier of the client | |
level: [{ type: Number, trim: true }], // Array of levelIDs associated with client | |
contact: [contact], // The contact details associated with the client | |
assistant: [assistant], // The details of the client's assistant | |
jobTitle: { type: String, trim: true }, // The client's job title | |
accountIds: [{ type: Number, trim: true, index: true}], // The account IDs (usually 1) associated with the client | |
approved: { type: Boolean, index: true, default:'0' }, // The approval details associated with the client | |
approvedOn: { type: Number, trim: true }, | |
isActive: { type: Number, required: true, index: true, 'default': 1 } // Whether the client is visible or not | |
},{strict: true}); | |
clientSchema.plugin(this.obj.useTimestamps); | |
clientSchema.pre('save',function(next){ | |
this.timeApproved = new Date(this.timeApproved*1000).toISOString(); | |
console.log(this); | |
next(); | |
}); | |
flow.model = this.obj.mongoose.model(this.moduleName,clientSchema); | |
}, | |
'routes' : function(){ | |
// Standard Controllers | |
var standard = Object.create(this.obj.standardControllers).init(), | |
// Middleware that modifies what's exposed | |
// to the Controller. | |
middleware = this.obj.routing; | |
// GET / | |
this.obj.express.get('/'+this.moduleName,middleware.getParams,standard.list); | |
// GET /:id | |
this.obj.express.get('/'+this.moduleName+'/:id',middleware.getParams,standard.show); | |
// POST / | |
this.obj.express.post('/'+this.moduleName,standard.create); | |
// POST /search | |
this.obj.express.post('/'+this.moduleName+'/search',middleware.getParams,standard.search); | |
// POST /:id OR PUT /:id | |
this.obj.express.post('/'+this.moduleName+'/:id',standard.update); | |
this.obj.express.put('/'+this.moduleName+'/:id',standard.update); | |
// DELETE /:id | |
this.obj.express.del('/'+this.moduleName+'/:id',standard.remove); | |
this.obj.express.post('/'+this.moduleName+'/delete/:id',standard.remove); | |
return this; | |
}, | |
'init': function(obj){ | |
this.obj = obj; | |
this.routes(); | |
this.db(); | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment