Consulting, training, products, professional services
contact@wearefractal.comGET "/employees/1234" /* Get employee by ID */
GET "/employees?name=Todd&limit=10" /* Get 10 matching employees */
db.employees.find({_id: ObjectId("1234")}) // Get employee by ID
db.employees.find({name: "Todd"}).limit(10) // Get 10 matching employees
// Create our schema
var EmployeeSchema = new Schema({
name: {
type: String,
required: true
},
status: {
type: String,
enum: ['online','away','offline'],
default: 'offline'
}
});
// Add the model into mongoose
var Employee = mongoose.model('Employee', EmployeeSchema);
// Create HTTP server
var app = express(); app.use(express.bodyParser());
// Expose our models from the database and add it to the HTTP server var api = crudify(db); api.expose('Employee'); api.hook(app); // Listen to port 8080 app.listen(8080);
GET /employees - Gets a list of employees. Can use limit, skip, sort, and where.
POST /employees - Creates a new employee
GET /employees/:id - Gets a specific employee.
PUT /employees/:id - Replaces a specific employee.
PATCH /employees/:id - Modifies a specific employee.
DELETE /employees/:id - Removes a specific employee.
Read/write on a collection
EmployeeSchema.statics.authorize = function(req) {
return {
write: true,
read: true
};
};
Read/modify/delete on a specific document
EmployeeSchema.methods.authorize = function(req) {
return {
write: true,
read: true,
delete: true
};
};
Read/modify on a field of a specific item
name: {
type: String,
authorize: function (req) {
return {
write: true,
read: true
};
}
}
/employees/searchByName?q=Todd
EmployeeSchema.statics.searchByName = function(req, cb) {
this.find({name: String(req.params.q)}, cb);
}
/employees/1234/similar
EmployeeSchema.methods.similar = function(req, cb) {
this.find({status: String(this.status)}, cb);
}