Search code examples
node.jsyamlnode-config

Overriding setting in YAML using a strategy from the default file?


experienceList:
  - dbAnalyst: &dbAnalyst
      posName: 'Database Analyst / Net Tech'
      companyName: 'Choices People Supporting People'
  - webDevXetex: &webDevXetex
      posName: 'Software Engineer'
      companyName: 'Xetex Business Systems'
# ...
experienceStrategies:
  - defaultExperience: &defaultExperience
    - <<: *dbAnalyst
    - <<: *webDevXetex
  - webDevExperience: &webDevExperience
    - <<: *webDevXetex
#...
experience: *defaultExperience

config/default.yml

And this results in experience being output in YAML as:

experience: 
  - dbAnalyst:
      posName: 'Database Analyst / Net Tech'
      companyName: 'Choices People Supporting People'
  - webDevXetex:
      posName: 'Software Engineer'
      companyName: 'Xetex Business Systems'

Now what I'd like to do is, keep the defaults in config/default.yml and override them selecting a different strategy (one of the experienceStrategies: above) in an override file similar to the following:

experience: *webDevExperience

config/override.yml

And hopefully resulting in the following:

experience: 
  - webDevXetex:
      posName: 'Software Engineer'
      companyName: 'Xetex Business Systems'

...

However, instead using the node-config library I end up with an error; is there a way to do this in yaml and node.js using either some other library or by sticking with node-config? I've seen another library called node-config-yaml that allows you to include other yaml files, but not certain if it allows you to override settings in the way that you can with node-config.

/home/leeand00/Documents/lifehacker organized/docs/projdir/node_modules/config/lib/config.js:933
    throw new Error("Cannot parse config file: '" + fullFilename + "': " + e3);
    ^

Error: Cannot parse config file: '/home/leeand00/Documents/lifehacker organized/docs/projdir/config/company_job.yml'
: YAMLException: unidentified alias "webDevExperience" at line 14, column 30:
    experience: *webDevExperience
                                 ^
    at util.parseFile (/home/leeand00/Documents/lifehacker organized/docs/projdir/node_modules/config/lib/config.js:
933:11)
    at /home/leeand00/Documents/lifehacker organized/docs/projdir/node_modules/config/lib/config.js:702:28
    at Array.forEach (native)
    at /home/leeand00/Documents/lifehacker organized/docs/projdir/node_modules/config/lib/config.js:698:14
    at Array.forEach (native)
    at util.loadFileConfigs (/home/leeand00/Documents/lifehacker organized/docs/projdir/node_modules/config/lib/conf
ig.js:697:13)
    at new Config (/home/leeand00/Documents/lifehacker organized/docs/projdir/node_modules/config/lib/config.js:122:
27)
    at Object.<anonymous> (/home/leeand00/Documents/lifehacker organized/docs/projdir/node_modules/config/lib/config
.js:1767:31)
    at Module._compile (module.js:409:26)
    at Object.Module._extensions..js (module.js:416:10)

Solution

  • Using the node-config-yml library actually did the trick, Despite it's sparse documentation on the matter it works perfectly.

    It appears to make the configuration into one file in memory from both of them. You can use aliases, and the merge function from node-config and just override as needed.