rethinkdb-example-nodejs icon indicating copy to clipboard operation
rethinkdb-example-nodejs copied to clipboard

koa 2.x

Open kodermax opened this issue 9 years ago • 10 comments

Give me please example for koa 2.x and koa-router 7.x

kodermax avatar Jun 07 '16 06:06 kodermax

@deontologician Do you know what it will take to update this example?

danielmewes avatar Jun 09 '16 18:06 danielmewes

Not really. I think @segphault was saying koa 2.0 requires async/await, which aren't available in node yet. So you'd need to precompile with babel or something.

deontologician avatar Jun 09 '16 21:06 deontologician

Koa 2 is still technically unreleased, pending the native availability of async/await in Node. More details about the status of Koa 2 here: https://github.com/koajs/koa/issues/533

The status of async/await support in v8 is here: https://bugs.chromium.org/p/v8/issues/detail?id=4483

I don't think we want to replace our current Koa example with one that's based on 2.x until 2.x is official released, but I'm personally looking forward to building some demos with it myself when that eventually happens.

segphault avatar Jun 09 '16 21:06 segphault

sadly :(

kodermax avatar Jun 10 '16 06:06 kodermax

I upgraded the koa example from koa 0.x to koa 1.x: https://github.com/rethinkdb/rethinkdb-example-nodejs/pull/19. Baby steps!

danneu avatar Jun 10 '16 22:06 danneu

you can use koa2 with babel now. I'm wondering if the rethinkdb npm module will work with async/await

here's a simple example app -- if someone can show how to use async/await with rethinkdb that would be cool :) https://github.com/geekplux/koa2-boilerplate

ralyodio avatar Jun 18 '16 02:06 ralyodio

The move from koa1 to koa2 is so mechanically straightforward that koa2 is basically koa1 with build step boilerplate.

If you're wondering how to use rethinkdb with koa2, you could start with my koa1 example rewrite, introduce the build step from your linked boilerplate, change the old function * (next) signatures to async function (ctx, next), and bump the deps to ensure they are koa2 friendly: https://github.com/danneu/rethinkdb-example-nodejs/blob/7ac4ae718237db3e246faf719f5d41eed0fac568/todo-angular-koa/app.js.

If you're just suggesting that there be a koa2 example, then ignore me. Just trying to help. :)

danneu avatar Jun 18 '16 21:06 danneu

I figured it out last night. what you posted is helpful too.

router.get('/', async (ctx, next) => {
  ctx.body = await r.table('things').coerceTo('array').run(ctx.rdbConn);
});

ralyodio avatar Jun 18 '16 22:06 ralyodio

@chovy Thanks! One quick question, did you change anything with the way rdbConn gets defined?

QasimQureshi avatar May 06 '17 11:05 QasimQureshi

I have something like this...but I am not sure if its the best way:

class Base {
  constructor(opts) {
    this.opts = opts;
    this.queries = [];
    this.r = r;
    this.conn = null;
  }

  async db() {
    this.conn = await this.r.connect({host: 'localhost', port: 28015, db: 'mydb'});
  }

ralyodio avatar May 06 '17 21:05 ralyodio