config
This is useful for when the environment-variables need to be nested and
still be camel cased.
The order of how the config is beeing transformed is:
- 0: defaults
- 1: environment
- 2: config-file
- 3: secrets
So that means that environment-variables
will override defaults
,
and the config-file
will override environment-variables
and so on.
Properties defined in defaults will almost always be returned, but overridden.
For example:
defaults: {
foo: {
bar: 'baz',
}
}
console.log(config.get('foo'))
console.log(config.get('foo:bar'))
console.log(config.get('foo:something'))
Types defined in defaults
will be applied to overridden values:
{
"another": [{
"key": "not hello",
"value": "2",
}]
}
const options = {
file: `${__dirname}/../config.json`,
defaults: {
some: {
nested: {
array: [ 1, 2, 3 ],
}
},
booleanValues: {
zero: false,
one: true
},
another: [{
key: 'hello',
value: 1,
}],
}
}
config.get('some')
config.get('some:nested')
config.get('some:nested:array')
config.get('booleanValues')
config.get('booleanValues:zero')
config.get('booleanValues:one')
config.get('another')
The casing is always ignored as an input, but the values fetched will always be camel-cased.
For example (starting application in shell):
SOME__NESTED__CONFIG__IN_CAMEL_CASE=ok \
some__nested__secondValue=bar \
npm start
... will be be transformed into (result in javascript):
{
some: {
nested: {
config: {
inCamelCase: 'ok',
},
secondValue: 'bar',
}
}
}
simple usage
const config = require('@iteam/config')({
file: `${__dirname}/../config.json`,
defaults: {
foo: {
bar: 'baz',
},
baz: [ 1, 2, 3 ],
}
})
config.defaults = { foo: { bar: 'baz' }, baz: [ 1, 2, 3 ] }
console.log(config.get('foo'))
console.log(config.get('foo:bar'))
console.log(config.get('baz'))
defaults can be passed to the initial function-call
const config = require('@iteam/config')({
defaults: {
foo: {
bar: 'bar'
},
baz: 'results'
}
})
"secrets"
This module got extended with docker-swarm
in mind, and their way of handling
secrects (which is run-time mounted files).
There's a option for the config-module to look into a directory and treat all
files a key/value config.
For example:
The directory /run/secrets
has these file in it:
some_nested__file
some_nested__other_file
When they are provided to the module they will be transformed as such:
{
someNested: {
file: 'contents of "/run/secrets/some_nested__file"',
otherFile: 'contents of "/run/secrets/some_nested__other_file"'
}
}
To enable this, provide secrets
as a argument when calling the module:
const config = require('@iteam/config')({
secrets: {
dir: '/run/secrets/'
}
})
config.secrets = {
dir: '/run/secrets/'
}
arguments:
{
search: boolean
dir: string
file: string
}
-
secrets takes an object
or a string
When providing a string, this should be the directory where secrets are stored.
The options-objects takes two properties:
{
dir: string
separator: string
}
{
separator: string
}
-
file takes an object
or a string
When providing a string, this should point to the full location of the config-file, or the dir
will be de default
{
search: boolean
dir: string
file: string
}