1.1.0 • Published 8 months ago

couchdb-changes-stream v1.1.0

Weekly downloads
-
License
MIT
Repository
github
Last release
8 months ago

CouchDBChangesStream Documentation


Description

CouchDBChangesStream is a TypeScript class for handling real-time changes in a CouchDB database through the _changes API. It supports normal, longpoll, continuous, and eventsource modes, with filtering via selector and automatic reconnection in live mode.


Installation

Install the package via npm:

npm install couchdb-changes-stream

Usage

Example

import { CouchDBChangesStream } from "couchdb-changes-stream";

const changesStream = new CouchDBChangesStream(
  "http://localhost:5984/mydb",
  {
    feed: "continuous",
    include_docs: true,
    since: "now",
    heartbeat: 10000,
    selector: {
      type: "message",
    },
  },
);

(async () => {
  try {
    for await (const change of changesStream) {
      console.log("Change:", change);

      // Process data
      if (change.doc) {
        console.log("Document:", change.doc);
      }
    }
  } catch (error) {
    console.error("Error in changes stream:", error);
  }
})();

Parameters

Constructor

new CouchDBChangesStream<T>(
  dbUrl: string,
  options: CouchDBChangesOptions | Readonly<CouchDBChangesOptions>,
)

Constructor Parameters

ParameterTypeDescription
dbUrlstringThe CouchDB database URL (e.g., http://localhost:5984/mydb).
optionsCouchDBChangesOptionsThe query options. Supports the same parameters as CouchDB _changes API.

Fields in CouchDBChangesOptions

FieldTypeDescription
sincestring \| numberThe sequence to start from (now or a number).
filterstringName of the filter (e.g., _selector).
doc_idsstring[] \| readonly string[]List of document IDs to filter.
selectorRecord<string, unknown>A JSON object for selecting documents (works only with POST).
feed"normal" \| "longpoll" \| "continuous" \| "eventsource"Feed mode.
include_docsbooleanInclude document body in changes.
heartbeatboolean \| numberFrequency of keep-alive messages in milliseconds.
livebooleanEnable live mode with automatic reconnection.
timeoutnumberTimeout for waiting for changes in milliseconds.

Features

  • Support for all modes: normal, longpoll, continuous, eventsource.
  • Filtering via selector: Uses the POST method when selector is present.
  • Automatic reconnection: Enabled in live mode.
  • Heartbeat support: Keeps connections alive.
  • Scalable: Asynchronous stream for handling large data volumes.

Methods

stop

public stop(): void

Stops the current changes stream.

Example:

changesStream.stop();

Errors

  • HTTP error: 400: Invalid request parameters (e.g., incorrect selector).
  • HTTP error: 401: Authentication error.
  • Error parsing change: Failed to parse data from the stream.

Usage Tips

  1. Use heartbeat to keep connections alive.
  2. Add exception handling to properly manage network errors.
  3. Prefer selector over filter for more flexible document selection.
  4. Call stop when done to terminate the stream gracefully.

License

This project is distributed under the MIT License.


For questions or issues, feel free to reach out! 🚀

1.1.0

8 months ago

1.0.1

8 months ago

1.0.0

8 months ago