Restify Model
A surprisingly useful model/collection adapter that builds routes and handles CRUD operations (Create, Read, Update and Delete). Works with any custom database adapter (Postgres, MySQL, MongoDB, etc.).
Installation
npm install restify-model
Example Usage
var restify = ;var server = restify;var Model = server; var Person = Model; var tim = name: 'Tim' occupation: 'Enchanter' id: 1; var roger = name: 'Roger' occupation: 'Shrubber' id: 2; Person; // GET /people// => [{// "name": "Tim",// "occupation": "Enchanter",// "id": 1// }, {// "name": "Roger",// "occupation": "Shrubber",// "id": 2// }] server;
Nested Routes & Collection Relationships
var Equipment = Model; Equipment; Person; // GET, POST to /people/:person_id/equipment// GET, PUT, DELETE to /people/:person_id/equipment/:id
CRUD Operations
GET
/people
"name": "Tim" "occupation": "Enchanter" "id": 1 "name": "Roger" "occupation": "Shrubber" "id": 2
GET
/people/1
"name": "Tim" "occupation": "Enchanter" "id": 1
POST
{ name: 'Brave Sir Robin', occupation: 'Knight', id: 3 }
/people
"name": "Brave Sir Robin" "occupation": "Knight" "id": 3
GET
/people
"name": "Tim" "occupation": "Enchanter" "id": 1 "name": "Roger" "occupation": "Shrubber" "id": 2 "name": "Brave Sir Robin" "occupation": "Knight" "id": 3
PUT
{ name: 'Brave Sir Robin', occupation: 'Knight of the Round Table' }
/people/3
"name": "Brave Sir Robin" "occupation": "Knight of the Round Table" "id": 3
Specifying CRUD Operations
// CREATE, READ, UPDATEvar CRUModel = Model;
Using a Database/Persistence Adapter
var MyModel = Model; var Enchanter = MyModel; var tim = id: 1; tim;// => Created 1
Middleware
Restify Model comes with the following middleware
findById
Finds related model, assigns it toreq.model
, callsnext()
detail
Getsreq.model
and sendsmodel.toJSON()
, ends requestlist
Getsreq.collection
and sendscollection.toJSON()
, ends requestcreate
Creates a new instance, assigns it toreq.model
and callsmodel.save()
, callsnext()
(middleware.detail
)update
Updatesreq.model
with body and callsmodel.save()
, callsnext()
(middleware.detail
)remove
Removesreq.model
, callsnext()
(middleware.detail
)
Customizing middleware
var CustomModel = Model;
Sample Database Adapter
This is an example of a simple database adapter. Any database ORM can be used here (Postgres, MySQL, MongoDB, etc.). Pseudo coding MongoDB as an example.
var mongoose = ; var DBModel = Model; var Person = DBModel; var tim = name: 'Tim' occupation: 'Enchanter' ; tim;// => Created 54b9e08ed983b41d432473e4// POST /people { name: 'Roger', occupation: 'Shrubber' }// => Created 54bcacb4c812ec812382b6b2// GET /people// => [{// "name": "Tim",// "occupation": "Enchanter",// "_id": "54b9e08ed983b41d432473e4"// }, {// "name": "Roger",// "occupation": "Shrubber",// "_id": "54bcacb4c812ec812382b6b2"// }]
collection.load()
on every request
Running var Persist = Person;
Customizing Routes
var Custom = Model;
Model as an Action (and disabling CRUD)
var Action = Model;
Even more middleware functionality
// You can specify which middleware to run on a specific namespace { if reqmodel ; else res; } { ; ;} var customNamespace = Person; var CustomModel = Model; // Or you can explicitly call namespace method (get, put, post, delete, etc)// GET /person/:id/foocustomNamespace;