struts-mongo

struts handler for mongodb


License
ISC
Install
npm install struts-mongo@1.0.0

Documentation

struts-mongo

struts handler for mongodb

Provides simple connection using the native mongodb node.js driver, as well as oplog monitoring for specified collections. All events follow the struts hub/spoke model and are emitted on the hub emitter.

Usage

// this represents the possible options and their defaults:
const options = {
  dbhost: "127.0.0.1",
  dbname: "test",
  dbopts: {}, // passed to driver, see http://mongodb.github.io/node-mongodb-native/2.2/api/MongoClient.html
  root: "mongodb" // root of emitted events (e.g. 'mongodb.connected')
}

class Hub extends EventEmitter {
  constructor() {
    this.db = new MongoHandler(this, options);
    this.db.watch("some-collection");
    this.db.start();
    
    // handle events here or in other struts handlers
    this.on("mongodb.connected", (db) => {
      // directly use db object
    });
    
    this.on("mongodb.insert", (data) => {
      // data.ns provides namespace of insert
      // data._id provides _id of new insertion
      // data.o provides the relevant object
    });
    
    this.on("mongodb.update", (data) => {
      // data.ns provides namespace of insert
      // data._id provides _id of new insertion
      // data.o provides the relevant object
    });
    
    this.on("mongodb.delete", (data) => {
      // data.ns provides namespace of insert
      // data._id provides _id of new insertion
      // data.o provides the relevant object
    });
    
  }
}