Skip to content

Instantly share code, notes, and snippets.

@anhhh11
Forked from zaim/README.md
Created February 15, 2016 15:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anhhh11/250dea5ceb0a998a69a7 to your computer and use it in GitHub Desktop.
Save anhhh11/250dea5ceb0a998a69a7 to your computer and use it in GitHub Desktop.
Thunkify Rethinkdb

rethunkdb

Thunkify rethinkdb

To be used with co and/or koa.

So that we can do something like:

var r = require('rethunkdb')  // not a typo, might use other name if too close :)
  , koa = require('koa')
  , app = koa();

app.use(function *(next) {
  if (!this.app.rdb) this.app.rdb = yield r.connect();
  yield next;
});

app.use(function *() {
  this.body = yield r.run(this.app.rdb, r.table('animals').get('cat'));
});

app.listen(3000);

NOT YET TESTED

/**
* Thunkify `r.connect` and add the `r.run` thunk function.
*/
var r = require('rethinkdb')
, connect = r.connect;
r.connect = function (config) {
return function (callback) {
connect.call(r, config, callback);
};
};
r.run = function (conn, query) {
return function (callback) {
query.run(conn, callback);
};
};
module.exports = r;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment