Hapi plugin that automatically generates RESTful API for CRUD
Go to file
2016-06-29 21:30:27 -07:00
src Fix: crud: GET id's can be strings or numbers 2016-06-29 21:30:27 -07:00
.babelrc feat(simple): simple CRUD REST API (no associations) 2016-01-18 18:08:43 +03:30
.gitignore chore: ignore build directory 2016-01-19 10:07:10 +03:30
Gruntfile.js feat(simple): simple CRUD REST API (no associations) 2016-01-18 18:08:43 +03:30
package.json Build: install boom 2016-06-29 21:27:44 -07:00
README.md chore(README, defaultConfig): add defaultConfig to README 2016-06-05 21:37:58 +04:30

hapi-sequelize-crud

Automatically generate a RESTful API for your models and associations

This plugin depends on hapi-sequelize.

npm install -S hapi-sequelize-crud

##Configure

// First, register hapi-sequelize
await register({
  register: require('hapi-sequelize'),
  options: { ... }
});

// Then, define your associations
let db = server.plugins['hapi-sequelize'].db;
let models = db.sequelize.models;
associations(models); // pretend this function defines our associations

// Now, register hapi-sequelize-crud
await register({
  register: require('hapi-sequelize-crud'),
  options: {
    prefix: '/v1',
    defaultConfig: { ... } // passed as `config` to all routes created
  }
});

Please note that you should register hapi-sequelize-crud after defining your associations.

##What do I get

Let's say you have a many-to-many association like this:

Team.belongsToMany(Role, { through: 'TeamRoles' });
Role.belongsToMany(Team, { through: 'TeamRoles' });

You get these:

# get an array of records
GET /team/{id}/roles
GET /role/{id}/teams
# might also append query parameters to search for
GET /role/{id}/teams?members=5

# you might also use scopes
GET /teams/{scope}/roles/{scope}
GET /team/{id}/roles/{scope}
GET /roles/{scope}/teams/{scope}
GET /roles/{id}/teams/{scope}

# get a single record
GET /team/{id}/role/{id}
GET /role/{id}/team/{id}

# create
POST /team/{id}/role
POST /role/{id}/team

# update
PUT /team/{id}/role/{id}
PUT /role/{id}/team/{id}

# delete
DELETE /team/{id}/roles #search and destroy
DELETE /role/{id}/teams?members=5

DELETE /team/{id}/role/{id}
DELETE /role/{id}/team/{id}

# include
# include nested associations (you can specify an array if includes)
GET /team/{id}/role/{id}?include=SomeRoleAssociation

# you also get routes to associate objects with each other
GET /associate/role/{id}/employee/{id} # associates role {id} with employee {id}

# you can specify a prefix to change the URLs like this:
GET /v1/team/{id}/roles