Skip to content

Commit

Permalink
[add] merge strategy: deep merge or rewrite
Browse files Browse the repository at this point in the history
  • Loading branch information
Stan Gumeniuk committed Feb 19, 2016
1 parent 6b0f0d9 commit 353e37b
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 4 deletions.
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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();
```

11 changes: 10 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

"use strict";

var merger = require('extend');

class Config {

constructor(pathToConfigDir, availableConfigs) {
Expand All @@ -16,13 +18,19 @@ class Config {
this.availableConfigs = availableConfigs;// || ['production', 'rc', 'develop'];
this.configHierarchy = ['production'];
this.pathToConfigDir = pathToConfigDir;
this.deepMerge = true;
}

setEnvName(envName) {
this.envName = envName;
return this;
}

setDeepMerge(deep) {
this.deepMerge = deep;
return this;
}


getConfig() {
var env = this.getEnvironmentFromGlobalEnv();
Expand Down Expand Up @@ -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;
Expand All @@ -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;
});
Expand Down
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
34 changes: 32 additions & 2 deletions test/mconf.js
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand All @@ -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"
Expand All @@ -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);
});
})

});

0 comments on commit 353e37b

Please sign in to comment.