From 353e37bf56e821bbfcfaea409558907d67796cb6 Mon Sep 17 00:00:00 2001 From: Stan Gumeniuk Date: Fri, 19 Feb 2016 22:46:00 +0300 Subject: [PATCH] [add] merge strategy: deep merge or rewrite --- README.md | 11 +++++++++++ index.js | 11 ++++++++++- package.json | 4 +++- test/mconf.js | 34 ++++++++++++++++++++++++++++++++-- 4 files changed, 56 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 919a322..bf4aef8 100644 --- a/README.md +++ b/README.md @@ -35,3 +35,14 @@ By default using `NODE_ENV`, but if you need to use another environment name: .setEnv('YOUR_ENV_NAME') .getConfig(); ``` +# Merge config strategy + +By default using `deep merge` strategy. But if you want non deep merge ( rewrite), you should use: + + ``` + var config = new Config(__dirname,['production', 'develop']); + module.exports = config + .setDeepMerge(false) + .getConfig(); + ``` + diff --git a/index.js b/index.js index 760aa05..0db97fa 100644 --- a/index.js +++ b/index.js @@ -5,6 +5,8 @@ "use strict"; +var merger = require('extend'); + class Config { constructor(pathToConfigDir, availableConfigs) { @@ -16,6 +18,7 @@ class Config { this.availableConfigs = availableConfigs;// || ['production', 'rc', 'develop']; this.configHierarchy = ['production']; this.pathToConfigDir = pathToConfigDir; + this.deepMerge = true; } setEnvName(envName) { @@ -23,6 +26,11 @@ class Config { return this; } + setDeepMerge(deep) { + this.deepMerge = deep; + return this; + } + getConfig() { var env = this.getEnvironmentFromGlobalEnv(); @@ -82,6 +90,7 @@ class Config { _initConfig() { var result = {}; var pathToConfigDir = this.pathToConfigDir; + var self = this; this.configHierarchy .forEach(function (configName) { var path = pathToConfigDir + '/' + configName; @@ -91,7 +100,7 @@ class Config { throw new Error('Config: config "'+configName+'" not found'); } - Object.assign(result, config); + merger(self.deepMerge?true:false,result, config); result.environment = configName; }); diff --git a/package.json b/package.json index 948ecf5..741ad1a 100644 --- a/package.json +++ b/package.json @@ -31,7 +31,9 @@ "url": "https://github.com/ViGo5190/mconf/issues" }, "homepage": "https://github.com/ViGo5190/mconf#readme", - "dependencies": {}, + "dependencies": { + "extend": "^3.0.0" + }, "devDependencies": { "chai": "^3.5.0", "coveralls": "^2.11.6", diff --git a/test/mconf.js b/test/mconf.js index 69e7ecd..fd6a9be 100644 --- a/test/mconf.js +++ b/test/mconf.js @@ -163,7 +163,7 @@ describe('services/cache', function () { }); - it('return really union config', function() { + it('return really union config with deep merge', function() { mockery.registerMock('../config/production', { foo: "bar", first: { @@ -182,7 +182,8 @@ describe('services/cache', function () { var expectedConfig = { foo: "bar", first: { - name: 'new value' + name: 'new value', + bar: 'foo' }, bar: "foo", "environment": "develop" @@ -191,6 +192,35 @@ describe('services/cache', function () { var config = new Config(default_path,default_available_config); assert.deepEqual(config.getConfig(), expectedConfig); }); + + it('return really union config with undeep merge', function() { + mockery.registerMock('../config/production', { + foo: "bar", + first: { + name: 'value', + bar: 'foo' + } + }); + + mockery.registerMock('../config/develop', { + bar: "foo", + first: { + name: 'new value' + } + }); + + var expectedConfig = { + foo: "bar", + first: { + name: 'new value' + }, + bar: "foo", + "environment": "develop" + + }; + var config = new Config(default_path,default_available_config); + assert.deepEqual(config.setDeepMerge(false).getConfig(), expectedConfig); + }); }) }); \ No newline at end of file