mongoconfig

Store your application configuration in MongoDB

MongoConfig

MongoConfig is a tiny library to store and get configuration documents from MongoDB. It uses the mongoose driver.

var mongoConfig = require('mongoconfig');
var mongoose = require('mongoose');
mongoose.connect('mongodb://myserver:27017/mydb');

var config = {
  'hostname': 'smtp.example.com',
  'port': 25,
  'tls': true
};

// store config in the 'configuration' collection,
// the document _id will be 'smtp'
mongoConfig('smtp').store(config, function(err) {
  if ( !err ) {
    console.log('Configuration stored');
  }
});
var mongoConfig = require('mongoconfig');
var mongoose = require('mongoose');
mongoose.connect('mongodb://myserver:27017/mydb');
    
// add the "antispam" key in the 'smtp' configuration document 
// of the 'configuration' collection
mongoConfig('smtp').set('antispam', true, function(err) {
  if ( !err ) {
    console.log('Configuration updated');
  }
});


// also supports the dot notation
mongoConfig('smtp').set('options.antivirus', true, function(err) {
  if ( !err ) {
    console.log('Configuration updated');
  }
});
// get config from the 'configuration' collection,
mongoConfig('smtp').get(function(err, cfg) {
  if ( !err ) {
    console.log('Got configuration: ', cfg);
  }
});
// get the content of the "hostname" key
mongoConfig('smtp').get('hostname', function(err, cfg) {
  if ( !err ) {
    console.log('Configuration stored');
  }
});

// also support dot notation
mongoConfig('smtp').get('options.antivirus', function(err, cfg) {
  if ( !err ) {
    console.log('options.antivirus', cfg);
  }
});

The MongoConfig module only needs one argument: the namespace of the ocnfiguration document

var mongoConfig = require('mongoconfig')('smtp');

The second argument, if set, is the MongoDB collection name where documents will be stored/fetched. The default collection name is 'configuration'.

var mongoConfig = require('mongoconfig')('smtp', 'myConfigurationCollection');

The third argument is an option hash. For now it only supports one key: 'mongoose', that gives the mongoose instance to use. If it's not set, mongoConfig uses "require('mongoose')" to get mongoose.

var mongoConfig = require('mongoconfig')('smtp', null, {mongoose: myMongoose});

You can configure the module, to set the default collection name:

var mongoConfig = require('mongoconfig');
mongoConfig.setDefaultCollectionName('myOwnCollectionName');

You can configure the module, to set the default mongoose instance:

var mongoConfig = require('mongoconfig');
mongoConfig.setDefaultMongoose(myMongoose);

Use the command:

grunt

to launch the test suite. For it to work, you'll need gjslint, and a Mongodb server listening on localhost:27017.

GNU GPLv3

This was coded with love by Linagora.

Use, share, fork, send pull requests !