An express router module that adds a JSON api conformant to the JSON API spec for all defined Sequelize models in your application.
I specifically started this project to support the Ember Data adapter ember-json-api but quickly found it has some limitations. I realized this after implementing partial support for JSON API format.
So I now support two transport formats, json-api
and ember-restadapter
. The later supports Ember Data RESTAdapter transport format.
var express = require('express')
, Sequelize = require('sequelize')
, api = require('sequelize-json-api')
var sequelize = new Sequelize();
// define models here
api = api(sequelize,{
endpoint: '/api', // needed for href calculation
})
api.use(function(req,res,next){
//Do some middleware function
next();
});
api.initialize();
app.use('/api', api);
app.listen();
{
endpoint: '/api', // the api endpoint, this is used to build resource URLs
allowed: [], // a list of models to expose on the api, default so all if none specified
allowOrigin: "*", // the value for the Access-Control-Allow-Origin header to support CORS
transport: "json-api", // the transport format to use for the api, json-api or ember-restadapter
idValidator: function(id){return true;} //a method to validate ids, default is `validator.isInt`
}
Method | Route | |
---|---|---|
GET | /:resource |
Returns a resource collection of :resources |
POST | /:resource |
Create a new :resource and returns individual resource |
GET | /:resource/:id |
Returns the individual resource :resource/:id |
PUT | /:resource/:id |
(Pending issues/6) Updates and returns the individual resource :resource/:id |
DELETE | /:resource/:id |
Deletes :resource/:id |
GET | /:resource/:id/:collection |
Returns a resource collection of :collection where associated with :resource/:id |