rethinkdb / horizon-docs

Other
24 stars 36 forks source link

Embedding docs incorrectly uses `_reql_conn` as a property of the server module #120

Closed Tryneus closed 7 years ago

Tryneus commented 7 years ago

in docs/embed, the last section Accessing the Rethinkdb 'r' object has the code:

const horizon = require('@horizon/server');
const conn = horizon._reql_conn.connection();

This is wrong, _reql_conn is a property of a Server instance, not of the @horizon/server module. In order to get the connection, you need to instantiate a Server and pass it the information for r.connect, at which point it may become available.

See related issue at rethinkdb/horizon#807

chipotle commented 7 years ago

So at risk of asking a stupid question, would the correct example be more like:

const express = require('express');
const horizon = require('@horizon/server');

const app = express();
const httpServer = app.listen(8181);
const options = {
    project_name: 'myProject',
    auth: { token_secret: 'my_super_secret_secret' }
};
const horizonServer = horizon(httpServer, options);

const conn = horizonServer._reql_conn.connection();
Tryneus commented 7 years ago

@chipotle - not entirely - the connection will not be ready immediately, so the ready() function returns a promise:

...
const horizonServer = horizon(httpServer, options);
const r = horizon.r;

horizonServer._reql_conn.ready().then((reql_conn) => {
  r.expr('foo').run(reql_conn.connection());
});

Also, the header for the section indicates it was to show how the same r object can be used, I though the ReQL connection was just an aside.

Anyway, a lot of this stuff is changing for the plugins feature, though, so I wouldn't put too much work into this section yet.