RethinkDB JavaScript Driver
The RethinkDB JavaScript driver provides programmatic access to RethinkDB, a NoSQL database known for its real-time capabilities via push queries. It is primarily designed for Node.js environments and implements the ReQL query language. The current stable version is 2.4.4. After a period of inactivity from the original creators, the project transitioned to community-driven maintenance under the Linux Foundation, with recent 2.4.x releases (2023-2024) addressing bug fixes and compilation issues. Its key differentiator remains the 'changefeeds' feature, which allows applications to receive real-time updates as data changes. While the official documentation is still available, the release cadence is irregular, reflecting its community-supported status.
Common errors
-
Error: connect ECONNREFUSED 127.0.0.1:28015
cause The RethinkDB server is not running, or the connection details (host/port) are incorrect.fixEnsure the RethinkDB server is running and accessible from your application. Verify the `host` and `port` parameters in your `r.connect()` call match the server's configuration. -
RethinkDBError: Table `db.table_name` does not exist.
cause The specified database or table has not been created in RethinkDB.fixEnsure that the database and table exist before attempting to query them. You can use `r.dbCreate('your_db')` and `r.db('your_db').tableCreate('your_table')` to create them programmatically or via the RethinkDB Data Explorer. -
TypeError: r.table(...).insert is not a function
cause A ReQL query command was called without a `run()` method or with incorrect syntax, or the connection object was not passed.fixAll ReQL queries must be terminated with a `.run(connection, callback)` or `await .run(connection)` call to execute them against the database. Verify your ReQL chain for correct command usage.
Warnings
- breaking The API for the `r.js` ReQL command experienced breaking changes in version 2.4.2. Users relying on this command should review their code.
- breaking RethinkDB 2.4.x servers are not compatible with 2.3.x or earlier versions when mixed in the same cluster. Mixing versions can lead to cluster instability or data inconsistencies.
- gotcha The HTTP connection type provided by the driver is intended only for the RethinkDB web UI and is explicitly stated not to be secure for application use. Using it in production applications is a security risk.
- breaking When upgrading to RethinkDB 2.4.0 from versions 1.16 or earlier, data files will be automatically migrated. However, it is strongly recommended to back up your data files before performing any major version upgrade.
- breaking On 32-bit platforms and Windows (both 32 and 64-bit), RethinkDB 2.3.5 servers should not be mixed with servers running 2.3.3 or older in the same cluster. This can cause server crashes, especially when using the web UI or accessing system tables.
Install
-
npm install rethinkdb -
yarn add rethinkdb -
pnpm add rethinkdb
Imports
- r
const r = require('rethinkdb'); - connect
import { connect } from 'rethinkdb'const r = require('rethinkdb'); r.connect({...}) - RethinkDBError
import { RethinkDBError } from 'rethinkdb'const r = require('rethinkdb'); const { RethinkDBError } = r;
Quickstart
const r = require('rethinkdb');
async function runExample() {
let connection;
try {
connection = await r.connect({
host: process.env.DB_HOST ?? 'localhost',
port: parseInt(process.env.DB_PORT ?? '28015'),
db: process.env.DB_NAME ?? 'test',
user: process.env.DB_USER ?? 'admin',
password: process.env.DB_PASSWORD ?? ''
});
// Create a table if it doesn't exist
const tableList = await r.db('test').tableList().run(connection);
if (!tableList.includes('authors')) {
await r.db('test').tableCreate('authors').run(connection);
console.log('Table "authors" created.');
}
// Insert a document
const insertResult = await r.table('authors').insert({
name: 'John Doe',
posts: 5
}).run(connection);
console.log('Inserted:', insertResult.generated_keys[0]);
// Fetch all documents
const cursor = await r.table('authors').run(connection);
const authors = await cursor.toArray();
console.log('Authors:', authors);
} catch (err) {
console.error('Error:', err.message);
} finally {
if (connection) {
await connection.close();
console.log('Connection closed.');
}
}
}
runExample();