rethinkdb / rethinkdb-example-nodejs

137 stars 98 forks source link

koa 2.x #18

Open kodermax opened 8 years ago

kodermax commented 8 years ago

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

danielmewes commented 8 years ago

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

deontologician commented 8 years ago

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.

segphault commented 8 years ago

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.

kodermax commented 8 years ago

sadly :(

danneu commented 8 years ago

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

ralyodio commented 8 years ago

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

danneu commented 8 years ago

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. :)

ralyodio commented 8 years ago

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);
});
QasimQureshi commented 7 years ago

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

ralyodio commented 7 years ago

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'});
  }