node-config-store v0.0.3
Config Store
Easily manage the runtime setup of an app wide config store.
Features
- Populate initial state via object, files and functions
- Schema validation
- Static value lookup and derived lookups based on the current state
- Immutability Option
Usage
createConfigStore(options) => ConfigStore
Create the ConfigStore
options
initialState
- {Object} The initial state for the storeinitialStateFiles
- {Array} Paths to thedotenv
compatible files that should be used to populate the initial store.- If used,
dotenv
is a required peer-dependency. - The values pulled from the files are merged into the
ConfigStore
in the order they are encountered.
- If used,
initialStateLoaders
- {Array<Function(currentState) => ValueSet} Array of sync/async functions that return a set of values to merge into the current state at the time of execution.- The loaders are executed non-parellel since the new, combined state is passed to each subsequent loader.
initialState
andinitialStateFile
are applied first- Any values returned by any
loader
overwrite the current state
derivedValues
- {Object} Object of functions that are accessible and derived at runtime. Should accept a single parameter,currentState
.schema
- {Object} Ajoi
schema object that defines the required structure for the config state.- If used,
joi
is a required peer-dependency.
- If used,
immutable
- {Boolean} Whether or not the store should be immutable after intialization.- If
true
,initialState
orinitialStateFile
must be provided
- If
async loadState() => boolean
Execute the set of initialStateLoaders
.
- Only required when
initialStateLoaders
are provided - Must be executed before usage of the
ConfigStore
set(key: String, value: StoreValue) => boolean
Set the value for a key in the ConfigStore
.
- Cannot be used if
immutable
istrue
get(key: String | keySet String[]) => StoreValue
| StoreValueMap
| undefined
Get the value(s) for a specific key(set) in the ConfigStore
- If passed an array of keys, the keys will be returned in a set
- This first checks for the existence of a
derivedValue
function that matches the provided key and will return that, otherwise return the value for the absolute key (if exists)
State Construction Order
The creation of the ConfigStore
state is deterministic and is the result of merge operation for the different methods of initial value creation:
- The
initialState
object is applied - Any
initialStateFiles
are merged agaisnt the state in the order in which they are listed - Any
initialStateLoaders
are merged agaisnt the state in the order in which they are listed
Validation
If a schema
object is provided, the state is validated agaisnt the schema after all initialState*
operations are performed
- If
initialStateLoaders
are provided, and areasync
, the validation will only occur after those operations resolve. Don'tget
values from theConfigStore
until everything is loaded