[data-reveal]{opacity:1!important;transform:none!important}
RethinkDB

Interface RethinkDB Documentation

Overview

Interface RethinkDB provides query execution and access to the RethinkDB query builder through a clean, promise-based API. The package exports two main items: the RunQuery function for executing queries against a managed connection, and the r query builder re-exported from rethinkdb-ts for constructing queries.

Getting Started

Install the package in your project:

npm install @antelopejs/interface-rethinkdb

r (Query Builder)

The r object is the RethinkDB query builder, re-exported directly from the rethinkdb-ts package. Use it to construct queries that you then execute with RunQuery.

import { r } from "@antelopejs/interface-rethinkdb";

// Build a query (does not execute it)
const query = r.table("users").filter({ active: true });

RunQuery

The RunQuery function executes a RethinkDB query against the managed connection. It accepts a query built with r and optional run options.

import { RunQuery, r } from "@antelopejs/interface-rethinkdb";

const result = await RunQuery(r.table("users").get("123"));
ParameterTypeDescription
queryRQueryA RethinkDB query built using the r query builder
optionsRunOptionsOptional execution settings (e.g., readMode, durability)

The function returns the result type that matches the query's run() return type.

CRUD Operations

Insert documents

import { RunQuery, r } from "@antelopejs/interface-rethinkdb";

async function createUser(name: string, email: string) {
  const result = await RunQuery(
    r.table("users").insert({ name, email, createdAt: r.now() }),
  );
  return result.generated_keys[0];
}

Retrieve documents

import { RunQuery, r } from "@antelopejs/interface-rethinkdb";

async function getUser(id: string) {
  return RunQuery(r.table("users").get(id));
}

async function getActiveUsers() {
  return RunQuery(r.table("users").filter({ active: true }));
}

Update documents

import { RunQuery, r } from "@antelopejs/interface-rethinkdb";

async function updateEmail(userId: string, newEmail: string) {
  await RunQuery(
    r.table("users").get(userId).update({ email: newEmail }),
  );
}

Delete documents

import { RunQuery, r } from "@antelopejs/interface-rethinkdb";

async function deleteUser(userId: string) {
  await RunQuery(r.table("users").get(userId).delete());
}

Query Options

Pass RunOptions as the second argument to control query execution behavior.

import { RunQuery, r } from "@antelopejs/interface-rethinkdb";

// Use outdated reads for better performance when strong consistency is not needed
const users = await RunQuery(
  r.table("users").filter({ role: "admin" }),
  { readMode: "outdated" },
);

Advanced Queries

Joins

import { RunQuery, r } from "@antelopejs/interface-rethinkdb";

async function getUsersWithPosts() {
  return RunQuery(
    r.table("users").eqJoin("id", r.table("posts"), { index: "authorId" }),
  );
}

Aggregations

import { RunQuery, r } from "@antelopejs/interface-rethinkdb";

async function countUsersByRole() {
  return RunQuery(
    r.table("users").group("role").count(),
  );
}

For the complete query builder API, refer to the RethinkDB documentation and the rethinkdb-ts package.