SkillAgentSearch skills...

Rethinkdbdash

An advanced Node.js driver for RethinkDB with a connection pool, support for streams etc.

Install / Use

/learn @neumino/Rethinkdbdash
About this skill

Quality Score

0/100

Supported Platforms

Universal

README

rethinkdbdash

<a href="https://app.wercker.com/project/bykey/10e69719c2031f4995798ddb9221c398"><img alt="Wercker status" src="https://app.wercker.com/status/10e69719c2031f4995798ddb9221c398/m/master" align="right" /></a>

A Node.js driver for RethinkDB with more advanced features.

Install

npm install rethinkdbdash

Note: The rethinkdbdash-unstable package is a relic from the past (rethinkdb < 1.13).

Quick start

Rethinkdbdash uses almost the same API as the official driver. Please refer to the official driver's documentation for all the ReQL methods (the methods used to build the query).

The main differences are:

  • You need to execute the module when you import it:
var r = require('rethinkdbdash')();
// With the official driver:
// var r = require('rethinkdb');
  • Connections are managed by the driver with an efficient connection pool. Once you have imported the driver, you can immediately run queries, you don't need to call r.connect, or pass a connection to run.
var r = require('rethinkdbdash')();
r.table('users').get('orphee@gmail.com').run().then(function(user) {
  // ...
}).error(handleError)
  • Cursors are coerced to arrays by default
var r = require('rethinkdbdash')();
r.table('data').run().then(function(result) {
  assert(Array.isArray(result)) // true
  // With the official driver you need to call
  // result.toArray().then(function(result2) {
  //   assert(Array.isArray(result2))
  // })
});

Drop in

You can replace the official driver with rethinkdbdash by just replacing

var r = require('rethinkdb');

With:

var r = require('rethinkdbdash')({
  pool: false,
  cursor: true
});

If you want to take advantage of the connection pool, refer to the next section.

From the official driver

To switch from the official driver to rethinkdbdash and get the most of it, here are the few things to do:

  1. Change the way to import the driver.
var r = require('rethinkdb');

To:

var r = require('rethinkdbdash')();
// Or if you do not connect to the default local instance:
// var r = require('rethinkdbdash')({servers: [{host: ..., port: ...}]});
  1. Remove everything related to a connection:
r.connect({host: ..., port: ...}).then(function(connection) {
  connection.on('error', handleError);
  query.run(connection).then(function(result) {
    // console.log(result);
    connection.close();
  });
});

Becomes:

query.run().then(function(result) {
  // console.log(result);
});
  1. Remove the methods related to the cursor. This typically involves removing toArray:
r.table('data').run(connection).then(function(cursor) {
  cursor.toArray().then(function(result) {
    // console.log(result):
  });
});

Becomes

r.table('data').run().then(function(result) {
  // console.log(result);
});

Using TLS Connections

Note: Support for a TLS proxy is experimental.

RethinkDB does not support TLS connections to the server yet, but in case you want to run it over an untrusted network and need encryption, you can easily run a TLS proxy on your server with:

var tls = require('tls');
var net = require('net');
var tlsOpts = {
  key: '', // You private key
  cert: '' // Public certificate
};
tls.createServer(tlsOpts, function (encryptedConnection) {
  var rethinkdbConn = net.connect({
    host: 'localhost',
    port: 28015
  });
  encryptedConnection.pipe(rethinkdbConn).pipe(encryptedConnection);
}).listen(29015);

And then safely connect to it with the tls option:

var r = require('rethinkdbdash')({
  port: 29015,
  host: 'place-with-no-firewall.com',
  ssl: true
});

ssl may also be an object that will be passed as the options argument to tls.connect.

New features and differences

Rethinkdbdash ships with a few interesting features.

Importing the driver

When you import the driver, as soon as you execute the module, you will create a default connection pool (except if you pass {pool: false}. The options you can pass are:

  • db: <string> - The default database to use if none is mentioned.
  • user: <string> - The RethinkDB user, default value is admin.
  • password: <string> - The password for the user, default value is an empty string.
  • discovery: <boolean> - When true, the driver will regularly pull data from the table server_status to keep a list of updated hosts, default false
  • pool: <boolean> - Set it to false, if you do not want to use a connection pool.
  • buffer: <number> - Minimum number of connections available in the pool, default 50
  • max: <number> - Maximum number of connections available in the pool, default 1000
  • timeout: <number> - The number of seconds for a connection to be opened, default 20
  • pingInterval: <number> - if > 0, the connection will be pinged every pingInterval seconds, default -1
  • timeoutError: <number> - Wait time before reconnecting in case of an error (in ms), default 1000
  • timeoutGb: <number> - How long the pool keep a connection that hasn't been used (in ms), default 60*60*1000
  • maxExponent: <number> - The maximum timeout before trying to reconnect is 2^maxExponent x timeoutError, default 6 (~60 seconds for the longest wait)
  • silent: <boolean> - console.error errors, default false
  • servers: an array of objects {host: <string>, port: <number>} representing RethinkDB nodes to connect to
  • optionalRun: <boolean> - if false, yielding a query will not run it, default true
  • log: <function> - will be called with the log events by the pool master

In case of a single instance, you can directly pass host and port in the top level parameters.

Examples:

// connect to localhost:8080, and let the driver find other instances
var r = require('rethinkdbdash')({
    discovery: true
});

// connect to and only to localhost:8080
var r = require('rethinkdbdash')();

// Do not create a connection pool
var r = require('rethinkdbdash')({pool: false});

// Connect to a cluster seeding from `192.168.0.100`, `192.168.0.101`, `192.168.0.102`
var r = require('rethinkdbdash')({
    servers: [
        {host: '192.168.0.100', port: 28015},
        {host: '192.168.0.101', port: 28015},
        {host: '192.168.0.102', port: 28015},
    ]
});

// Connect to a cluster containing `192.168.0.100`, `192.168.0.100`, `192.168.0.102` and
// use a maximum of 3000 connections and try to keep 300 connections available at all time.
var r = require('rethinkdbdash')({
    servers: [
        {host: '192.168.0.100', port: 28015},
        {host: '192.168.0.101', port: 28015},
        {host: '192.168.0.102', port: 28015},
    ],
    buffer: 300,
    max: 3000
});

You can also pass {cursor: true} if you want to retrieve RethinkDB streams as cursors and not arrays by default.

Note: The option {stream: true} that asynchronously returns a stream is deprecated. Use toStream instead.

Note: The option {optionalRun: false} will disable the optional run for all instances of the driver.

Note: Connections are created with TCP keep alive turned on, but some routers seem to ignore this setting. To make sure that your connections are kept alive, set the pingInterval to the interval in seconds you want the driver to ping the connection.

Note: The error __rethinkdbdash_ping__ is used for internal purposes (ping). Do not use it.

Connection pool

As mentioned before, rethinkdbdash has a connection pool and manage all the connections itself. The connection pool is initialized as soon as you execute the module.

You should never have to worry about connections in rethinkdbdash. Connections are created as they are needed, and in case of a host failure, the pool will try to open connections with an exponential back off algorithm.

The driver execute one query per connection. Now that rethinkdb/rethinkdb#3296 is solved, this behavior may be changed in the future.

Because the connection pool will keep some connections available, a script will not terminate. If you have finished executing your queries and want your Node.js script to exit, you need to drain the pool with:

r.getPoolMaster().drain();

The pool master by default will log all errors/new states on stderr. If you do not want to pollute stderr, pass silent: true when you import the driver and provide your own log method.

r = require('rethinkdbdash')({
  silent: true,
  log: function(message) {
    console.log(message);
  }
});
Advanced details about the pool

The pool is composed of a PoolMaster that retrieve connections for n pools where n is the number of servers the driver is connected to. Each pool is connected to a unique host.

To access the pool master, you can call the method r.getPoolMaster().

The pool emits a few events:

  • draining: when drain is called
  • queueing: when a query is added/removed from the queue (queries waiting for a connection), the size of the queue is provided
  • size: when the number of connections changes, the number of connections is provided
  • available-size: when the number of available connections changes, the number of available connections is provided

You can get the number of connections (opened or being opened).

r.getPoolMaster().getLength();

You can also get the number of available connections (idle connections, without a query running on it).

r.getPoolMaster().getAvailableLength();

You can also drain the pool as mentionned earlier with;

r.getPoolMaster().drain();

You can access all the pools with:

r.getPoolMaster().getPools();

The pool master emits the healthy when its state change.

View on GitHub
GitHub Stars842
CategoryCustomer
Updated5mo ago
Forks107

Languages

JavaScript

Security Score

92/100

Audited on Oct 25, 2025

No findings