Giter Site home page Giter Site logo

rethinkdb-watch's Introduction

rethinkdb-watch npm version

Consume RethinkDB Changefeeds as Async Iterables

Installation

npm install rethinkdb-watch

API

watch

import {Connection, RSelection} from 'rethinkdb-ts';

function watch<T>(
  selection: RSelection<T>,
  connection?: Connection,
  options?: WatchOptions<T>,
): AsyncIterable<ReadonlyMap<string, WatchUpdate<T>>>;

type WatchUpdate<T> =
  | {type: 'add'; oldVal?: undefined; newVal: T}
  | {type: 'change'; oldVal: T; newVal: T}
  | {type: 'remove'; oldVal: T; newVal?: undefined};

Watch for real-time updates of query results.

Emits updates in batches represented as Map, where key is JSON-stringified primary key, and value is the update event.

The first emission represents the full initial query result.

Subsequent updates are buffered in time window specified by options.bufferTimeMs.

Supports querying the whole table, getAll, between and filter queries. orderBy queries are not supported and will lead to incorrect results. Can be used with pluck or map as long as options.key function is provided.

Supported options:

type WatchOptions<T> = {
  /**
   * Signal that aborts the watch. When aborted, the iterator throws
   * `AbortError`.
   */
  signal?: AbortSignal;
  /**
   * The number of changes the server will buffer between client reads before it
   * starts dropping changes and generates an error. Default is 100000.
   */
  changefeedQueueSize?: number;
  /**
   * The time window to buffer updates for. Default is 10.
   */
  bufferTimeMs?: number;
  /**
   * A function that must return the unique key of a value. Defaults to
   * JSON-stringified primary key.
   */
  key?: (value: T) => string;
};

Examples

Async generator that emits live query states:

import {r} from 'rethinkdb-ts';
import {watch} from 'rethinkdb-watch';
import {AbortSignal} from 'node-abort-controller';

const connection = await r.connect({host, port});

const query = r
  .db('test')
  .table<Doc>('test')
  .filter(row => row('test'));

async function* watchQueryState(signal: AbortSignal): AsyncGenerator<Doc[]> {
  const state = new Map<string, Doc>();

  for await (const updates of watch(query, connection, {signal})) {
    for (const [key, update] of updates) {
      if (update.newVal != null) {
        state.set(key, update.newVal);
      } else {
        state.delete(key);
      }
    }

    yield [...state.values()];
  }
}

consumeFeed

function consumeFeed<T>(
  feed: RFeed<T>,
  connection?: Connection,
  signal?: AbortSignal,
): AsyncIterable<T>;

A simple wrapper around RethinkDB .changes() that turns it it into an Async Iterable.

Examples

Async generator that emits live point query states:

import {r} from 'rethinkdb-ts';
import {consumeFeed} from 'rethinkdb-watch';
import {AbortSignal} from 'node-abort-controller';

const connection = await r.connect({host, port});

const query = r.db('test').table<Doc>('test').get('test-id');

const feed = query.changes({
  squash: true,
  includeInitial: true,
});

async function* watchDocState(signal: AbortSignal): AsyncGenerator<Doc | null> {
  for await (const changes of consumeFeed(feed, connection, signal)) {
    if (changes.new_val != null) {
      yield changes.new_val;
    } else {
      yield null;
    }
  }
}

rethinkdb-watch's People

Contributors

aikoven avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.