Skip to content

Instantly share code, notes, and snippets.

@villelahdenvuo
Created January 12, 2016 18:15
Show Gist options
  • Save villelahdenvuo/4425e010ebbdccf55b1b to your computer and use it in GitHub Desktop.
Save villelahdenvuo/4425e010ebbdccf55b1b to your computer and use it in GitHub Desktop.
Koa & TypeScript Example
'use strict';
import Test from './model';
import route from '../../lib/decorator-router';
class TestController {
/**
* List models
*/
@route.get('/')
static index = function *() {
this.body = yield new Test().fetchAll();
}
/**
* Get a single model
*/
@route.get('/:id')
static show = function *() {
this.body = yield new Test({ id: this.params.id }).fetch();
}
/**
* Update model
*/
@route.put('/')
static update = function *() {
this.body = yield new Test(this.request.body).save(null, { method: 'update' });
}
/**
* Create model
*/
@route.post('/')
static create = function *() {
this.body = yield new Test(this.request.body).save(null, { method: 'insert' });
}
/**
* Delete model
*/
@route.delete('/:id')
static delete = function *() {
yield new Test({ id: this.params.id }).destroy();
this.status = 204;
}
};
export default TestController;
'use strict';
import Bookshelf from '../../db/database';
class Test extends Bookshelf.Model<Test> {
get tableName() { return 'test'; }
}
export default Test;
@villelahdenvuo
Copy link
Author

If I do static *index() { ... } TypeScript will complain about this, because it thinks it refers to the TestController class, but setting them as anonymous generators works without any errors.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment