about
uenv
is a zero dependency configuration manager for nodejs, its design goals is to be very fast, allow finely grained access to properties via references, have an easy and straight-forward plugin system, allow different routines within the system to set up different parts of the configuration without having to be aware of the entire tree structure
installation
npm install --save uenv
by example
kitchen sink
const path = const uenv = // import enviorment variables into configurationuenv // assign an object to the root of the configuraiton treeuenv uenv // creates a new child with the literal pluginconst child = uenv // sets a value referenced as 'child.childfoo'child // assign multiple values to childchild // assign json contents to configuraiton root// assuming json1.json contents is { "jsonProperty": { "jsonA": 1, "jsonB": 2 } }uenv // assign json contents to { jsonData: {} }uenv // seal a property against changesuenvuenv uenv // baruenv // trueuenv // { object: { a: 1, b: 2 } }uenv // { a: 1, b: 2 }uenv // 1uenv // { childfoo: 'childbar', c: 9, d: 10 })uenv // { jsonA: 1, jsonB: 2 }uenv // { jsonA: 1, jsonB: 2 })uenv // { b: 2 }uenv // { a: 2 }uenv // 2// get entire configuration as plain javascript objectuenv
Useless plugin
const uenv = { // methods are { set, get, has, assign, toJSON } // and would behave depending on the plugin mounted position this__methods = methods this__arg1 = arg1} MyPluginprototype { this__methods} uenv const useless = uenv useless uenv // addthis-bar
plugins
built-in
- literal - a simple get/set/assign storage for objects
- env - loads the environment variables into the configuration as-is
- json - allows reading configuration from a json file, can also be used to store the file
external plugins
- (
uenv-s3-plugin
)[https://github.com/oiime/uenv-s3-plugin] - stores and retrieves configuration from AWS S3, allows encryption before storage
API
uenv.assign(obj)
Assigns an entire object to the configuration
- obj = plain javascript object to assign
uenv.set(key, value, options = {})
Assigns an entire object to the configuration
- key - key to set, using a separator (eg: 'foo.bar') would set references for the entire hierarchy
- value - just... the value
- options - optional parameters
- references (bool) - if set to false, there would be no references set for any properties of the value (if the value is an object), this can be useful if you dont want to waste memory and need to store large objects
uenv.get(key)
Returns a property value
uenv.has (key)
Checks if key exists, returns a boolean
uenv.toJSON ()
Returns a plain javascript object of all properties
uenv.plugin (name, Plugin)
registers a new plugin, Plugin would be constructed whenever use
or child
are called with its name
uenv.use (name, ...args)
Initiates a plugin that'll have access to the properties, any arguments after the plugin name would be passed as arguments to the plugin itself
this is accessible via the shorthand method uenv.[plugin name]
eg uenv.json()
uenv.child (key, name, ...args)
Initiates a plugin that'll have access to the properties at a specific position, any arguments after the plugin name would be passed as arguments to the plugin itself
Key can be dot notated to get a child at a deeper part of the tree, any preceding keys would be created with references, this is accessible via the shorthand method [plugin name]Child
uenv.equals (key, value)
Checks if a key is equal to a value, returns a boolean
uenv.seal (key)
Seals a key, any subsequent attempts to write to this key would not change the stored value
uenv.any (keys = [])
returns the first trueish key in the array
uenv.pick (key, properties = [])
if key is a plain javascript object, only the properties listed would be returned in a new object
uenv.omit (key, properties = [])
if key is a plain javascript object, only the properties not listed would be returned in a new object
uenv.setOptions (options = {})
Allows changes internal options within uenv
- options
- separator - change the default separator (.) to something else, this needs to be set before any property is set as that'll screw up the references
uenvuenvuenv
uenv.instance ()
Gets a standalone instance of uenv that does not store its properties at the module itself
jsonPlugin methods
uenv.json (filename, { required = true })
- filename - required argument if required is set to true (default), filename to load json from
- options
- required - if set to false the plugin would ignore any errors trying to load the json file
uenv.jsonChild (key, filename, { required = true })
- key to place object in
- filename - required argument if required is set to true (default), filename to load json from
- options
- required - if set to false the plugin would ignore any errors trying to load the json file
[jsonChild].save (filename)
saves current configuration to json file, if filename is not provided it'll save to the same filename set up during construction
License: MIT