sails-hook-deepblueprints is sails hook, provide more than two resources layers route which is provided by blueprints in sails native hook
Features
- generate more than two resource layers route like
/deep/company/1/team/2/project/3
- validate association between each layer in route path like: if team id 2 is not belong to company id 1, then will return 400 error
Installation
install in sails project
npm install sails-hook-deepblueprints --save
Usage
-
put config
deepBluePrint : true
into controller which one you want it have deep blueprint routes/*** CompanyController** @description :: Server-side logic for managing companies* @help :: See http://sailsjs.org/#!/documentation/concepts/Controllers*/moduleexports =_config :deepBluePrint : true; -
after sails start, there will auto generate deep-blueprints' routes with the prefix in the path: /deep
Example
here is complete sample
here are 4 models below, and they have One-to-many associations of one and the next one.
/*** Company.js*/moduleexports = attributes: name : "string" team: collection: 'team' ;
/*** Team.js*/moduleexports = attributes: name : "string" project : collection: 'project' ;
/*** Project.js*/ moduleexports = attributes: name : "string" todolist : collection: 'todolist' ;
/*** Todolist.js*/ moduleexports = attributes: name : "string" ;
after sails start, there will generate the deep-blueprints routes(these can see in the sails logs when log level is silly):
silly: Binding RESTful deepblueprint/shadow routes
when the request path match these routes, deep-blueprints will take the data processing job and in it is way, mostly like the blueprints do:
add: post %s/:parentid/%s/:id?
remove: delete %s/:parentid/%s/:id?
update: put %s/:parentid/%s/:id?
populate: get %s/:parentid/%s/:id?
Notice
- the association attribute name in the model should be EXACTLY same as the associate model's name, like :
/*** Company.js*/moduleexports = attributes: name : "string" team: // team is OK, teams is NOT OK collection: 'team' ;
/*** Team.js*/moduleexports = attributes: name : "string" ;
- because deep-blueprints will find the associations and create the route, so avoid circle routes, DON'T DEFINE TWO-WAY associations in two models like:
/*** Company.js*/moduleexports = attributes: name : "string" team: collection: 'team' ;
/*** Team.js*/moduleexports = attributes: name : "string" company : model : 'company' project : collection: 'project' ;
Test
in deep-blueprints sample, clone repo and run npm test