npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

couchdb-changes-stream

v1.1.0

Published

Stream CouchDB changes feed with support for pause, resume, and reconnection in TypeScript.

Downloads

228

Readme

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

| Parameter | Type | Description | |--------------------|----------------------------------------------|-----------------------------------------------------------------------------| | dbUrl | string | The CouchDB database URL (e.g., http://localhost:5984/mydb). | | options | CouchDBChangesOptions | The query options. Supports the same parameters as CouchDB _changes API. |

Fields in CouchDBChangesOptions

| Field | Type | Description | |--------------------|----------------------------------------------|-----------------------------------------------------------------------------| | since | string \| number | The sequence to start from (now or a number). | | filter | string | Name of the filter (e.g., _selector). | | doc_ids | string[] \| readonly string[] | List of document IDs to filter. | | selector | Record<string, unknown> | A JSON object for selecting documents (works only with POST). | | feed | "normal" \| "longpoll" \| "continuous" \| "eventsource" | Feed mode. | | include_docs | boolean | Include document body in changes. | | heartbeat | boolean \| number | Frequency of keep-alive messages in milliseconds. | | live | boolean | Enable live mode with automatic reconnection. | | timeout | number | Timeout 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! 🚀