Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to transform/merge config files in node.js / nconf?

I have node.js app and conf.json file with application settings, i.e.:

{
  "settings": [
    {
      "name": "setting1",
      "connectionString": {
        "host": "mongodb://127.0.0.1:27017/db",
        "collection": "collection1"
      }
    }, 
    {
      "name": "setting2",
      "file": "/path/file",
      "token": "development token"
    }
  ]
}

Is there any way in nconf or another tool to have analog of .NET's configuration transformation so I may have production config file with overrides, i.e. conf.production.json:

{
  "settings": [
    {
      "name": "setting2",
      "token": "production token"
    }
  ]
}

Expected value of "setting2" in production mode is "production token" and rest from default config file. I tried to load base files with nconf, but it does not work:

nconf.file(process.env.NODE_ENV, './conf.' + process.env.NODE_ENV + '.json');
nconf.file('./conf.json');
like image 883
asa Avatar asked Jan 08 '14 11:01

asa


1 Answers

The relevant part of my config script that I personally use to handle this:

nconf
    .argv    ()
    .env     ()
    .defaults( require( './_config.js' ) )
    .use     ( 'memory' )
    ;

...

nconf.set( 'env'     , nconf.get( 'environments:' + env ) );
nconf.set( 'env:name', env );

var includes = nconf.get( 'env:includes' );

if( {}.toString.call( includes ) === '[object Array]' ){
    var incs = {};

    includes.forEach( function( val ){
        incs[ val ] = val;
    } );

    includes = incs;
}

Object.keys( includes ).forEach( function( incName ){
    var incPath = includes[ incName ]
      , incData = nconf.get( 'environments:' + incPath );

    Object.keys( incData ).forEach( function( key ){
        var keyNm = ( incName.indexOf( '__local' === 0 ) )
            ? 'env:%s'   .sprintf( key )
            : 'env:%s:%s'.sprintf( incName, key )
            ;

        if( nconf.get( keyNm ) == null ){
            nconf.set( keyNm, incData[ key ] );
        }
    });
} );

I think the only dependency in there is sprintf, which you can either install with npm or just slightly re-factor the code.

Basically, I pass in an env param, and set .env to that object. I then walk an includes object, and pull in any relevant JSON objects from the same config object.

I then access everything via config.get( 'env:<param>' )

One of the simplier config.js (I don't like .json since it can't have comments) files I use:

module.exports =

{
    environments : {
        libraries : {
            jQuery : '//ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js'
        }

        , localLibraries : {
            jQuery : '/js/jquery-2.1.0.js'
        }

        , web_server : {
                      task : 'web_server'
            ,     includes : [ 'cdn', 'libraries' ]

            ,         port : 12567
            , csrfMaxAgeMs : 1000 * 60 * 60 * 24 * 30 // 30 days
            ,  csrfHashAlg : 'sha256'

            ,  apiHostName : '(^|\\.)api.*'

            ,     frontend : '/frontend'

            ,   jsonSpaces : 0
        }

        , local_web_server : {
                     task : 'web_server'
            ,    includes : { __local: 'web_server', libraries: 'localLibraries' }
            ,  jsonSpaces : 4
            ,         dev : true
        }
    }
};

You either include a list of objects (['cdn', 'libraries']), in which case they get mapped to env.cdn.param, for example. Or you can include a mapping, and if the key is __local, it maps to the the current object.

like image 114
Nobody Avatar answered Sep 28 '22 16:09

Nobody