Dollar config
Dollar config lets you keep dynamic settings in a declarative way and query them with runtime params.
Installation
npm install dollar-config
Setup
const DollarConfig = ; const config = ;
Usage
config;
Examples
Static setting
No magic here.
"foo": 1
config;> 1
$default
Provides a fallback for an absent setting.
"foo": 1 "$default": 0
config;> 0
$param
Looks up a param and returns its value.
"foo": "$param": "bar"
config;> 1
Use an array to provide a default value:
"foo": "$param": "bar" 0
config;> 0
$template
Replaces ${paramName}
with param value.
"greeting": "$template": "Hello, ${name}!"
config;> Hello John!
$guard
Picks the first truthy param and returns correspondent setting.
Falls back to an optional $default
if none of the params is truthy.
N.B. $default
, if present, must be the last item for lookup performance.
"foo": "$guard": "bar" "no luck" "baz" "close, but no cigar" "qux" "you are the champion" "$default" "oops"
config;> you are the champion config> oops
$switch
Matches param value to a list of cases and picks correspondent setting.
Falls back to an optional $default
if no match is found.
N.B. $default
, if present, must be the last item for lookup performance.
"meal": "$switch": "dayOfTime" "morning" "breakfast" "midday" "lunch" "evening" "dinner" "$default" "fridge"
config;> lunch config;> fridge
$function
Calls the referenced function and returns it's value.
Functions are provided as an option to config constructor.
Each function recevies params
as a single argument.
"expectedSalary": "$function": "double"
const config = functions: paramscurrentSalary * 2 ; config;> 1000
Nested settings/params/functions
Deep properties are accessible with dot-notation:
"foo": "bar": "$param": "baz.qux"
config;> 1
Nested $-keywords
You can mix and match $-keywords to get the desired effect:
"foo": "$switch": "bar" "baz" "$guard": "qux" "$param": "xyz"
config;> 1
Binding
The .bind()
method clones your config, converting all dynamic keys to getters, so that you can use them as a normal object:
const config = foo: 1 bar: $param: 'baz' ; config;> foo: 1 bar: 2
Because all dynamic keys are evaluated lazily, you can even make self-references:
const config = foo: $param: 'baz' bar: $param: 'config.foo' ; const params = baz: 1 ;paramsconfig = config; paramsconfig> foo: 1 bar: 1
After the first invocation getters replace themselves with evaluated values (a.k.a memoization):
let i = 1;const config = foo: $function: 'bar' functions: i++ ;const boundConfig = config; boundConfigfoo> 1 boundConfigfoo> 1
Express middleware
Dollar config express middleware binds provided config to the express req
and puts the result into req.config
:
"foo": "$param": "query.bar"
const dollarConfigMiddleware = ; app
The middleware accepts dollar config instances as well:
const dollarConfigMiddleware = ; const config = ; app;
Validation
You can use special ajv plugin to validate dollar-configs against JSON schemas.
const ajv = ;const validate = ajv;
Or, with ajv-cli:
ajv validate -d config.json -s schema.json -c dollar-config/ajv
The plugin introduces a custom dynamic
keyword which accepts any valid JSON schema inside it:
"type": "object" "properties": "foo": "dynamic": "type": "number"
;> true ;> true
The plugin checks that leaf values of $-keywords match original schema:
;> true ;> false expected number got string ;> false expected number got string
Using $template
implies that the original schema is string:
;> false expected original schema to be string got number
Building
Sometimes you want to have separate configs for different environments. Dollar config lets you keep all the settings in one source config and generate per-environment configs. dollar-config/build
can be used to inline predefined params. Other dynamic params are returned as-is.
"backendUrl": "$switch": "environment" "prestable" "https://prestable-backend/" "$default" "https://production-backend/" "backendQuery": "foo": "$param": "bar"
const buildConfig = ; const config = ; ;> backendUrl: "https://prestable-backend/" backendQuery: "foo": "$param": "bar" ;> backendUrl: "https://production-backend/" backendQuery: "foo": "$param": "bar"