Package Exports
- koapi
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (koapi) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
Koapi
RESTful API framework based on koa and bookshelf
Wriging a RESTful API have never been so easy!
Install
npm install koapiWrite your APIs in just 1 minute
Assume you have database below
Table posts
| id | title | contents | created_at | updated_at |
|---|---|---|---|---|
| 1 | Title | Contents | 2016-8-1 | 2016-8-1 |
Table comments
| id | post_id | title | contents | created_at | updated_at |
|---|---|---|---|---|---|
| 1 | 1 | Title | Comment | 2016-8-1 | 2016-8-1 |
Here we go!
app.js
import Koapi, {ResourceRouter, Model} from 'koapi';
const app = new Koapi();
/****************** Connect to database ******************/
Model.init({
client: 'pg',
connection: {
host : '127.0.0.1',
user : 'your_database_user',
password : 'your_database_password',
database : 'myapp_test'
}
});
/****************** Model definition ******************/
const Comment = Model.extend({
tableName: 'comments',
hasTimestamps: true,
});
const Post = Model.extend({
tableName: 'posts',
hasTimestamps: true,
comments(){
return this.hasMany(Comment);
}
});
/****************** Implement Routers ******************/
const posts = new ResourceRouter(Post.collection());
// POST /posts
// GET /posts
// GET /posts/:id
// PATCH /posts/:id
// DELETE /posts/:id
// are ALL ready!
// YES! IT'S DEAD SIMPLE! RIGHT?
posts.crud();
const comments = new ResourceRouter(ctx => ctx.state.post.comments());
// get post instance in context
comments.use(async (ctx, next) => {
ctx.state.post = await Post.findById(ctx.params.post_id, {require:true});
await next();
});
// method "crud" is a shortcut for create, read, update, destroy
// YOU CAN ALSO USE MIDDLEWARE in create, read, update, destroy
// AWESOME! RIGHT?!!!
comments.create(async(ctx, next) => {
// you can do anything before create
await next();
// you can do anything after create
});
comments.read(/* if you want, you can place any middlewares here */{
filterable: ['created_at'], // filterable field
sortable: ['created_at'], // sortable field
});
// comment may not have update
// comments.update();
comments.destroy();
/****************** Start server ******************/
app.bodyparser();
app.routers([
posts,
// YES! Thanks to koa-router, nested router is supported
posts.use('/posts/:post_id/comments', comments)
]);
app.listen(3000);run
babel-node app.jsYou have done your RESTful APIs in 1 minute
Your API is more complex?
Checkout Koapi Boilerplate for your situation.