Koaw
Create APIs using Koa and Waterline. Koaw creates auto-generated CRUD routes, using the co approach and the middleware design pattern.
Install
With npm do:
$ npm install koaw --save
Usage
const Koa = const Koaw = const Waterline = const Stores = // Set collections to ORMlet waterline = waterline // Create serverlet server = // Set new controllerlet controller = orm: waterline model: 'store' controller // Set methods to be used // Add middlewares to extend logic using generators // Register controller waterline
The above code registers the following routes:
POST /stores
GET /stores
GET /stores/:id
PUT /stores/:id
DELETE /stores/:id
API
.methods(methods)
Change default methods with an array or whitelisted string. These are default methods: post
put
get
delete
.
.override(methods, handler)
Change default handler of auto-generated routes. The methods should be specified with an array or whitelisted string.
.route(methods, path, fn)
Add custom route to given model. The methods should be specified with an array or whitelisted string. For instance, .route('get', 'custom', fn)
should generate this route: GET /[collection]/custom
.before(methods[, path], fn)
Add a before middleware to one or more methods, both custom routes and auto-generated routes. The methods should be specified with an array or whitelisted string.
.after(methods[, path], fn)
Add an after middleware to one or more methods, both custom routes and auto-generated routes. The methods should be specified with an array or whitelisted string.
.register(server)
Register controller routes to server. .register
must be called at the end of above methods.
Contribute
Todos
- Add support to queries, filters and populating
- Add support to subcontrollers to create nested routes
License
MIT