From 4d9364d5d97ec61595151e3f3696a32ef0767e2a Mon Sep 17 00:00:00 2001 From: Erno Aapa Date: Sun, 27 Dec 2015 10:17:33 +0200 Subject: [PATCH 1/2] Keep hierarchy when updating store with `use()` function Previously, when store were updated with `Provider.use()` function it were moved as last in configuration hierarchy. This caused that usually some other configuration were overriding updated configurations and the updates never get applied. Updated `use()` function to not remove/add the store, but just re-define it so the store key keep the same position in the `this.stores` variable. --- lib/nconf/provider.js | 8 +++----- test/provider-test.js | 10 +++++++++- 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/lib/nconf/provider.js b/lib/nconf/provider.js index 5e39e382..f0e85edf 100644 --- a/lib/nconf/provider.js +++ b/lib/nconf/provider.js @@ -102,11 +102,9 @@ Provider.prototype.use = function (name, options) { var store = this.stores[name], update = store && !sameOptions(store); - if (!store || update) { - if (update) { - this.remove(name); - } - + if (!store) { + this.add(name, options); + } else if (store && update) { this.add(name, options); } diff --git a/test/provider-test.js b/test/provider-test.js index ac4c5fdc..d26703ce 100644 --- a/test/provider-test.js +++ b/test/provider-test.js @@ -71,7 +71,15 @@ vows.describe('nconf/provider').addBatch({ "when 'env' is set to true with a nested separator": helpers.assertSystemConf({ script: path.join(fixturesDir, 'scripts', 'nconf-nested-env.js'), env: { SOME_THING: 'foobar' } - }) + }), + "calling the use() method after adding other stores": { + topic: new nconf.Provider().use('file', { file: files[0] }).defaults({candy: {something: 'should never get this'}}), + "should use a new instance of the store type": function (provider) { + provider.use('file', { file: files[1] }); + + assert.equal(provider.get('candy:something'), 'file2'); + } + }, } } }).addBatch({ From bb07f1d5103388174cd91cd1c3a09818365df65a Mon Sep 17 00:00:00 2001 From: indexzero Date: Wed, 9 Aug 2017 10:33:07 -0700 Subject: [PATCH 2/2] [fix] Do not change lexigraphic ordering of keys in this.stores. --- lib/nconf/provider.js | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/lib/nconf/provider.js b/lib/nconf/provider.js index f0e85edf..4e9c297b 100644 --- a/lib/nconf/provider.js +++ b/lib/nconf/provider.js @@ -102,9 +102,11 @@ Provider.prototype.use = function (name, options) { var store = this.stores[name], update = store && !sameOptions(store); - if (!store) { - this.add(name, options); - } else if (store && update) { + if (!store || update) { + if (update) { + this.remove(name); + } + this.add(name, options); } @@ -146,7 +148,7 @@ Provider.prototype.add = function (name, options, usage) { // this was used in the call to `.add()`. // Provider.prototype.remove = function (name) { - delete this.stores[name]; + this.stores[name] = null; return this; };