Crudify


API generation using NodeJS and MongoDB


@wearefractal


Consulting, training, products, professional services

contact@wearefractal.com

Open source is at github.com/wearefractal






A naive overview...











REST

  • Everything has an ID
  • Things can be linked together
  • Everything is a "resource"
  • Standard methods
    • GET = find
    • POST = create
    • PUT = replace
    • PATCH = merge
    • DELETE = remove


GET "/employees/1234" /* Get employee by ID */
GET "/employees?name=Todd&limit=10" /* Get 10 matching employees */

MongoDB

  • Everything has an ID
  • Things can be linked together (DBRefs)
  • Everything is a "document"
  • "No Schema" - Great for rapid development


db.employees.find({_id: ObjectId("1234")}) // Get employee by ID
db.employees.find({name: "Todd"}).limit(10) // Get 10 matching employees

NodeJS - The Quick Sell


  • Excels at asynchronous I/O
  • Powered by Chrome's V8 engine



You get the point...

MongooseJS

  • Schemas
  • Validation
  • Clean querying syntax
  • Population of DBRefs
  • Middleware
  • Connection management
  • Plugin interface

Mongoose Model


// 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);

Crudify

  • Takes your models and turns them into an API
  • Does all of the hard stuff you don't want to
    • Authorization
    • Relationships
    • Permission-based document/field filtering
    • Type checking/validation
    • Middleware
    • Configurable sorting/filtering/population
    • Performance

Example


// Create HTTP servervar 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);

Endpoints generated


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.

WARNING





API IN PROGRESS!

Authorization

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
    };
  }
}

Custom Methods

Collection Level

/employees/searchByName?q=Todd
EmployeeSchema.statics.searchByName = function(req, cb) {
  this.find({name: String(req.params.q)}, cb);
}

Model Level

/employees/1234/similar
EmployeeSchema.methods.similar = function(req, cb) {
  this.find({status: String(this.status)}, cb);
}

Links

Questions?

Crudify

By Eric Schoffstall

Crudify

API generation using NodeJS and MongoDB

  • 11,586
Loading comments...

More from Eric Schoffstall