W3cubDocs

/RethinkDB JavaScript

ReQL command: connect

Command syntax

r.connect([options, ]callback)
r.connect([host, ]callback)
r.connect([options]) → promise
r.connect([host]) → promise

Description

Create a new connection to the database server. Accepts the following options:

  • host: the host to connect to (default localhost).
  • port: the port to connect on (default 28015).
  • db: the default database (default test).
  • user: the user account to connect as (default admin).
  • password: the password for the user account to connect as (default '', empty).
  • timeout: timeout period in seconds for the connection to be opened (default 20).
  • ssl: a hash of options to support SSL connections (default null). Currently, there is only one option available, and if the ssl option is specified, this key is required:
    • ca: a list of Node.js Buffer objects containing SSL CA certificates.

If the connection cannot be established, a ReqlDriverError will be passed to the callback instead of a connection.

Using SSL with RethinkDB requires proxy software on the server, such as Nginx, HAProxy or an SSL tunnel. RethinkDB will encrypt traffic and verify the CA certification to prevent man-in-the-middle attacks. Consult your proxy’s documentation for more details.

Alternatively, you may use RethinkDB’s built-in TLS support.

Example: Open a connection using the default host and port, specifying the default database.

r.connect({
    db: 'marvel'
}, function(err, conn) {
    // ...
});

If no callback is provided, a promise will be returned.

var promise = r.connect({db: 'marvel'});

Example: Open a new connection to the database.

r.connect({
    host: 'localhost',
    port: 28015,
    db: 'marvel'
}, function(err, conn) {
    // ...
});

Alternatively, you can use promises.

var p = r.connect({
    host: 'localhost',
    port: 28015,
    db: 'marvel'
});
p.then(function(conn) {
    // ...
}).error(function(error) {
    // ...
});

Example: Open a new connection to the database, specifying a user/password combination for authentication.

r.connect({
    host: 'localhost',
    port: 28015,
    db: 'marvel',
    user: 'herofinder',
    password: 'metropolis'
}, function(err, conn) {
    // ...
});

Example: Open a new connection to the database using an SSL proxy.

var fs = require('fs');
fs.readFile('/path/to/cert', function (err, caCert) {
    if (!err) {
        r.connect({
            host: 'localhost',
            port: 28015,
            db: 'marvel',
            authKey: 'hunter2',
            ssl: {
                ca: caCert
            }
        }, function(err, conn) {
            // ...
        });
    } else {
        console.log(err);
    }
});

Related commands

Get more help

Couldn't find what you were looking for?

© RethinkDB contributors
Licensed under the Creative Commons Attribution-ShareAlike 3.0 Unported License.
https://rethinkdb.com/api/javascript/connect/