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 = require('koa')
const Koaw = require('koaw')
const Waterline = require('waterline')
const Stores = require('./collections/stores')
let waterline = new Waterline()
waterline.loadCollection(Stores)
let server = new Koa()
let controller = new Koaw({
orm: waterline,
model: 'store'
})
controller
.methods('get post put delete')
.before('post', function *(next) { yield next })
.after('post', function *(next) { yield next })
.before('post put', function *(next) { yield next })
.after('post put', function *(next) { yield next })
.after('put', function *(next) { yield next })
.register(server)
waterline.initialize({}, function (err) {
server.listen(4000)
})
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