mongoconfig

1.1.0 • Public • Published

MongoConfig

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

Quick usage

Storing a configuration document in MongoDB

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');
  }
});

Updating a configuration document

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');
  }
});

Getting a complete configuration document

// get config from the 'configuration' collection,
mongoConfig('smtp').get(function(err, cfg) {
  if ( !err ) {
    console.log('Got configuration: ', cfg);
  }
});

Getting a specific key from a configuration document

// 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);
  }
});

Detailed Usage

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);

Tests

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.

Licence

GNU GPLv3

Others

This was coded with love by Linagora.

Use, share, fork, send pull requests !

Versions

Current Tags

  • Version
    Downloads (Last 7 Days)
    • Tag
  • 1.1.0
    0
    • latest

Version History

  • Version
    Downloads (Last 7 Days)
    • Published
  • 1.1.0
    0
  • 1.0.0
    0

Package Sidebar

Install

npm i mongoconfig

Weekly Downloads

0

Version

1.1.0

License

MIT

Last publish

Collaborators