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

mongoose-swarm

v1.1.0

Published

A tiny package for managing connections to multiple Mongo DBs with Mongoose.

Downloads

8

Readme

Mongoose Swarm

A (tiny) library for managing connections to multiple MongoDBs with Mongoose.


Setup

It's really easy! Swarm exposes only one method that takes two non-optional arguments: (configObject, callback). Your configuration object consists of a couple required components:

  • url - A properly formatted mongodb:// prefixed url string
  • schemas - An object where the key is the name of the schema, and the value is a pre-made Mongoose Schema object. Alternatively, Mongoose will attempt to cast a plain object to a Schema upon model instantiation.

Example:

const config = {
  primary: {
    url: 'mongodb://localhost:27017/test1',
    schemas: {
      Test1: new Schema( { foo: { type: String} } ),
      Test2: { bar: { type: Boolean } }
      Test3: PrebuiltSchema
    }
  }
}

But what about my options like replSet and autoIndex? No worries! just pass those in your configObj as opts.

const config = {
  primary: {
    url: 'mongodb://somehost,someotherhost:27017/test',
    schemas: { ... },
    opts: {
      server: {
        socketOptions: {
          keepAlive: 300000,
          connectTimeoutMS: 30000
        }
      },
      replset: {
        rs_name: 'someReplSet',
      },
      db: {
        readPreference: 'secondaryPreferred'
      }
    }
  }
}

So that's all fine and dandy, but not that helpful if you just have one DB to connect to, which is why this doodad will connect to many. Just add multiple DB configurations in your configObj

const config = {

  primary: {
    url: 'mongodb://localhost:27017/my_db',
    schemas: {
      Test1: new Schema( { foo: { type: String} } ),
    }
  },

  secondary: {
    url: 'mongodb://localhost:27017/my_other_db',
    schemas: {
      Test2: new Schema( { bar: { type: String} } ),
    }
  }
}
All together now:
const mongooseSwarm = require('mongoose-swarm');
const config = {

  primary: {
    url: 'mongodb://localhost:27017/my_db',
    schemas: {
      Test1: new Schema( { foo: { type: String} } ),
    }
  },

  secondary: {
    url: 'mongodb://localhost:27017/my_other_db',
    schemas: {
      Test2: new Schema( { bar: { type: String} } ),
    }
  }
};

mongooseSwarm(config, (err, results) => {
  if (err) handleErr(err);

  let myDbs = results.dbs;
  // this is an object of named Mongoose Connection objects,
  // e.g. `results.dbs.primary` would be the connection to
  // mongodb://localhost:27017/my_db
  // You can take these objects and add your own event
  // listeners and do other fun Mongoose-y things.

  let myModels = results.models;
  // this is an object of named Mongoose Model objects,
  // e.g. `results.models.Test1` would now be the model
  // generated from the schema Test1.
  // Models are connection specific in Mongoose, so each
  // model is bound to its connection. Calling
  // `myModels.Test1.create(obj, cb);` will create a new
  // Test1 object and save it in your primary DB.
});

Error Handling

All Mongoose-Swarm specific errors are prefixed with Mongoose-Swarm:

Mongoose-Swarm will throw in two cases:

  • No configObject passed in initial call.
  • No callback pass in initial call.

All other errors are passed to your callback in standard (err, results) fashion.

If you don't pass fully formed Schemas to the initialization function, Mongoose-Swarm will attempt to create it for you in a try/catch block, as Mongoose throws. If an error is caught, it will immediately fire your callback function.


Testing

You can clone it from github to run tests, npm test for Mocha output, npm run cover for Istanbul coverage reporting. CI testing coming soon.