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

ejoy-oplog

v3.0.0

Published

Watch mongodb oplog in a simple way

Downloads

10

Readme

Installation

$ npm install ejoy-oplog

Configure MongoDB with replica set

You need to configure your MongoDB instance (local instance) to have access to the oplog, here are some quick steps on how to do so:

  1. Shutdown your existing mongo instance if its running.

  2. Restart the instance. Use the --replSet option to specify the name of the replica set.

$ sudo mongod --replSet rs0
  1. Connect to the mongo instance by executing mongo in your terminal:
$ mongo
  1. In the mongo shell run rs.initiate() to initiate the new replica set:
> rs.initiate()

Once it is initiated then you are ready to start using ejoy-oplog.

And here is the official MongoDB documentation if you need additional help on MongoDB replica set.

Usage

import MongoOplog from 'ejoy-oplog'
const oplog = MongoOplog('mongodb://127.0.0.1:27017/local', { ns: 'test.posts' })

oplog.tail();

oplog.on('op', data => {
  console.log(data);
});

oplog.on('insert', doc => {
  console.log(doc);
});

oplog.on('update', doc => {
  console.log(doc);
});

oplog.on('delete', doc => {
  console.log(doc.o._id);
});

oplog.on('error', error => {
  console.log(error);
});

oplog.on('end', () => {
  console.log('Stream ended');
});

oplog.stop(() => {
  console.log('server stopped');
});

API

MongoOplog(uri, [options])

  • uri: Valid MongoDB uri or a MongoDB server instance.
  • options MongoDB connection options.

oplog.tail([fn])

Start tailing. This method support both Promise and callback.

oplog.tail().then(() => {
  console.log('tailing started')
}).catch(err => console.error(err))

// or with async/await
async function tail() {
  try {
    await oplog.tail()
    console.log('tailing started')
  } catch (err) {
    console.log(err)
  }
}

oplog.stop([fn])

Stop tailing and disconnect from server. This method support both Promise and callback.

oplog.stop().then(() => {
  console.log('tailing stopped')
}).catch(err => console.error(err))

// or with async/await
async function stop() {
  try {
    await oplog.stop()
    console.log('tailing stopped')
  } catch (err) {
    console.log(err)
  }
}

oplog.destroy([fn])

Destroy the mongo-oplog object by stop tailing and disconnecting from server. This method support both Promise and callback.

oplog.destroy.then(() => {
  console.log('destroyed')
}).catch(err => console.error(err))

// or with async/await
async function destroy() {
  try {
    await oplog.destroy()
    console.log('destroyed')
  } catch (err) {
    console.log(err)
  }
}

oplog.ignore

Pause and resume oplog events.

oplog.ignore = true; // to pause
oplog.ignore = false // to resume

oplog.filter(ns)

Create and return a filter object.

const filter = oplog.filter('*.posts')
filter.on('op', fn)
oplog.tail()

filter.destroy()

Destroy filter object.

filter.destroy()

filter.ignore

Pause and resume filter events.

filter.ignore = true; // to pause
filter.ignore = false // to resume

events

Events supported by oplog and filter;

  • op: All bellow operations (oplog/filter).
  • insert: Document insert (oplog/filter).
  • update: Document update (oplog/filter).
  • delete: Document delete (oplog/filter).
  • end: Cursor stream ended (oplog).
  • error: Error (oplog).

Run tests

Configure MongoDB for active oplog, once this is done then you can run the test:

$ npm install
$ npm run test