mongoose-swarm

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

Usage no npm install needed!

<script type="module">
  import mongooseSwarm from 'https://cdn.skypack.dev/mongoose-swarm';
</script>

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.