Created
January 12, 2016 18:15
-
-
Save villelahdenvuo/4425e010ebbdccf55b1b to your computer and use it in GitHub Desktop.
Koa & TypeScript Example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
import Bookshelf from '../../db/database'; | |
class Test extends Bookshelf.Model<Test> { | |
get tableName() { return 'test'; } | |
} | |
export default Test; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
If I do
static *index() { ... }
TypeScript will complain aboutthis
, because it thinks it refers to the TestController class, but setting them as anonymous generators works without any errors.