14

Is there any method in nconf to collect all the keys from all the stores into a single object?

Imagine I've got this little script:

assert = require('assert');
nconf = require('nconf');

nconf.argv().env().defaults({'C': 3});
assert.equal(nconf.get('A'), 1);
assert.equal(nconf.get('B'), 2);
assert.equal(nconf.get('C'), 3);
assert.deepEqual({'A':1, 'B':2, 'C':3}, nconf.X); // <-- What's X?

that I run with

A=1 node script.js -B 2

Is there an nconf.X that will pass the test? I'd even settle for listing all the configured keys.

hurrymaplelad
  • 26,645
  • 10
  • 56
  • 76
  • It appears from the source that such an operation is not implemented and to implement it would require accessing the Provider's properties and building such a object from the Stores. – Dan D. Nov 20 '12 at 08:12

1 Answers1

21

Yes to get the object you can do the following;

nconf.get();

it will fail the test as argv will with the exec string and env will have a lot more variables though.

You could whitelist the env call using the following

nconf.env({whitelist: 'A'});

Also the defaults adds a 'type' with value 'literal' to the resulting output.

For a test that passes you could use this;

var assert = require('assert'),
nconf = require('nconf');

nconf.argv().env({whitelist: ['A']}).defaults({'C': 3});
assert.equal(nconf.get('A'), 1);
assert.equal(nconf.get('B'), 2);
assert.equal(nconf.get('C'), 3);

var object = nconf.get();

delete object.type;
delete object['$0'];
delete object['_'];

assert.deepEqual({'A':1, 'B':2, 'C':3}, object);
Jason Brumwell
  • 3,482
  • 24
  • 16
  • Great, thanks! If configuration is fixed after startup, does it seem crazy to use that `object` to lookup settings using natural js paths instead of `:` delimited nconf paths? – hurrymaplelad Nov 24 '12 at 07:55
  • Your welcome, I would assume its a matter of preference, I export to an object as I find it more convenient. – Jason Brumwell Nov 26 '12 at 01:06
  • 1
    @Jason Brumwell Instead of `nconf.get()`, can I get only the configurations passed in from command line, `process.argv`? Apparently, `nconf.get('argv')` doesn't work. – chepukha Dec 18 '14 at 21:12
  • I would also like to get all the nconf properties – Alexander Mills May 28 '15 at 01:53