node-config-store
v0.0.3
Published
Runtime Configuration Utility for Node JS
Readme
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 thedotenvcompatible files that should be used to populate the initial store.- If used,
dotenvis a required peer-dependency. - The values pulled from the files are merged into the
ConfigStorein 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.
initialStateandinitialStateFileare applied first- Any values returned by any
loaderoverwrite the current state
derivedValues- {Object} Object of functions that are accessible and derived at runtime. Should accept a single parameter,currentState.schema- {Object} Ajoischema object that defines the required structure for the config state.- If used,
joiis a required peer-dependency.
- If used,
immutable- {Boolean} Whether or not the store should be immutable after intialization.- If
true,initialStateorinitialStateFilemust be provided
- If
async loadState() => boolean
Execute the set of initialStateLoaders.
- Only required when
initialStateLoadersare 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
immutableistrue
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
derivedValuefunction 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
initialStateobject is applied - Any
initialStateFilesare merged agaisnt the state in the order in which they are listed - Any
initialStateLoadersare 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
initialStateLoadersare provided, and areasync, the validation will only occur after those operations resolve. Don'tgetvalues from theConfigStoreuntil everything is loaded
