Skip to content

Instantly share code, notes, and snippets.

@NikolayGalkin
Last active November 23, 2015 11:28
Show Gist options
  • Save NikolayGalkin/811e59bb33c879b8575c to your computer and use it in GitHub Desktop.
Save NikolayGalkin/811e59bb33c879b8575c to your computer and use it in GitHub Desktop.
import route from 'koa-route';
import glob from 'glob';
export default app => {
glob.sync('../controllers/*.js', {cwd: __dirname}).map(file => {
const Item = require(file); // How to import via `import * from file` ?
for (const key in Item.default.routes || {}) {
const [method, path] = key.split(' ');
app.use(route[method](path, Item.default[Item.default.routes[key]]));
}
});
return async (ctx, next) => {
await next();
};
};
// Works correctly!
export default {
routes: {
'get /events': 'list'
},
list: async (ctx) => {
const result = await ctx.db.raw('SELECT * FROM events_list(?, ?)', [ctx.query.page, ctx.query.limit]);
ctx.body = result.rows;
}
};
function list(ctx) {
var _this = this;
return _asyncToGenerator(regeneratorRuntime.mark(function _callee5() {
return regeneratorRuntime.wrap(function _callee5$(_context5) {
while (1) {
switch (_context5.prev = _context5.next) {
case 0:
_context5.next = 2;
return ctx.db.raw('SELECT * FROM events_list(?, ?)', [ctx.query.page, ctx.query.limit]);
case 2:
result = _context5.sent;
ctx.body = result.rows;
case 4:
case 'end':
return _context5.stop();
}
}
}, _callee5, _this);
}))();
}
export default class Events {
static routes = {
'get /events': 'list',
'get /events/count': 'count',
'get /events/:id': 'detail',
'post /events': 'create'
}
static async list(ctx) {
this.result = await ctx.db.raw('SELECT * FROM events_list(?, ?)', [ctx.query.page, ctx.query.limit]);
ctx.body = this.result.rows;
}
static async detail(ctx, id) {
this.result = await ctx.db.raw('SELECT * FROM events_detail(?)', [id]);
ctx.body = this.result.rows[0];
}
static async create(ctx) {
await ctx.db.raw('SELECT * FROM events_create(?, ?)', [ctx.request.body.webhookEvent, ctx.request.body]);
ctx.body = {};
}
static async count(ctx) {
this.result = await ctx.db.raw('SELECT * FROM events_count()');
ctx.body = this.result.rows[0];
}
}
@NikolayGalkin
Copy link
Author

Но без переменной работает:

ctx.body = (await ctx.db.raw('SELECT * FROM events_list(?, ?)', [ctx.query.page, ctx.query.limit])).rows;

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