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-global-changes

v2.0.4

Published

Event emitter for _changes to _all_dbs of a couchdb instance, including newly added dbs

Downloads

53

Readme

couchdb-global-changes

Does what it says on the tin: emit a db-change event for each document change in each db of a couchdb instance. Can also persist its progress (sequence id-wise) to a file or in the monitored databases.

Installation

npm install couchdb-global-changes

Simple usage

var couchdb_changes = require('couchdb-global-changes')
var feed = couchdb_changes('http://user:[email protected]:5984')

feed.on('db-change', function(details) {
    console.log('document changed: %s / %s', details.db_name, details.change.id)
})
feed.on('error', function(err) {
    console.error('something is wrong:', err)
    process.exit()
})

Advanced usage

var couchdb_changes = require('couchdb-global-changes')
var twilio = require('twilio')('ACCOUNT_SID', 'AUTH_TOKEN')

var options = 
    {   couch: 'http://user:[email protected]:5984'
    ,   include: '^userdb-'
    ,   persist: '_local'
    ,   namespace: 'sms-daemon'
    ,   include_docs: true
    }

var feed = couchdb_changes(options)

feed.on('db-change', function(details) {

    var doc = details.change.doc
    if(!doc.type == 'sms-message')
        return

    twilio.sendMessage(
            {   to: doc.recipient
            ,   from: doc.sender
            ,   body: doc.message
            }
        ,   function(err, responseData) {
                if (!err)
                    console.log('Successfully sent an sms from %s to %s', doc.sender, doc.recipient)
                else
                    console.error('Could not send sms from %s to %s:\n', doc.sender, doc.recipient, err)
        }
    )
})

feed.on('db-persist', function(details) {
    // after this, we can be sure the update sequence has been saved.
    // when this script is started again, it will pick up just where
    // it left off and not process any document a second time.
    console.log("%s processed all documents in the database %s (up to update-sequence %s)", feed.namespace, details.db_name, details.persist.seq)
})

Details

Options

  • if options is a string, it is interpreted as the url to a couchdb instance

options.couch

  • the root url of the couchdb instance
  • defaults to http://127.0.0.1:5984

options.include

  • if provided, will be evaluated as a regular expression
  • each db's name must then match that regular expression

options.exclude

  • if provided, will be evaluated as a regular expression
  • each db's name must then not match that regular expression

options.since can be one of

  • 'now' or an integer sequence id: all feeds will use this same value
  • { db1: 'now', db2: 1234 }: provide a value for each db's name individually (good for managing resuming operations)
  • defaults to 0 for every feed

options.include_docs can be one of

  • true or false: all feeds will use this same value
  • { db1: true, db2: false }: provide a value for each db's name individually
  • defaults to false for every feed

options.persist can be

  • a string containg the path to a JSON file, where the db's sequences can be stored and later retrieved
  • '_local', which will persist the db's sequence ids to {db}/_local/couchdb-global-changes:{namespace}
  • nothing else right now

options.persist_debounce

  • to avoid hammering the filesystem or the db with write queries, the actual call to persist sequence ids is debounced by this amount of milliseconds
  • defaults to 200

options.namespace (mandatory if options.persist is provided)

  • this allows the persistence layer to save sequence ids individually for every script that invokes the couchdb-global-changes module.

Functions

  • feed.total_dbs() returns the number of dbs that are being followed
  • feed.caught_up_dbs() returns the number of dbs have emitted the catchup event

Events

progress | function(details)

  • calculated global progress during the catchup-phase.
  • details.progress goes from 0 to 1
  • details.caught_up_dbs the number of caught up dbs
  • details.total_dbs the number of dbs being followed

catchup | function()

  • all followed dbs have caught up

db-catchup | function(details)

  • the db details.db_name has caught up to sequence id details.seq (which is an alias to details.catchup)

db-progress | function(details)

  • calculated progress of a single db during the catchup-phase.
  • details.progress goes from 0 to 1
  • details.db_name the name of the db

db-removed | function(details)

  • the db details.db_name has been removed from the feed, probably due to deletion of the db

db-* | function(details)

  • each of iriscouch's follow events is forwarded with a db- prefix
  • the originating db's name is passed as details.db_name
  • arguments are passed as details.<eventname>
  • Example: the db-change event gets passed { db_name: 'foo', change: <change-obj> }

Tests

The tests are quite incomplete, as this still is a work in progress.

# *nix
export COUCH=http://admin:[email protected]:5984
# windows
set COUCH=http://admin:[email protected]:5984

# the tests will create a few dbs prefixed with 'cgc-tests-'
# these can be removed afterwards

cd node_modules/couchdb-global-changes
npm install
npm test

Changes

3.0.0

  • removed caught_up_dbs and total_dbs info from the db-catchup event
  • extended the progress event's parameter to an object with progress and caught_up_dbs and total_dbs

2.0.0

  • renamed options.filter to options.include (and add accordingly options.exclude)
  • added "_local" as an option to options.persist
  • added options.namespace (needed for namespacing in the persistence layer)
  • added event db-persist which gets emitted after the current sequence id has been persisted to disk/db.
  • added options.persist_debounce
  • added caught_up_dbs and total_dbs info to the db-catchup event's details
  • (internal) persistence layers are now async