Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Using Config Utilities

Mark Stosberg edited this pageJan 7, 2025 ·23 revisions

Node-config comes with a handy set of utilities necessary for implementing node-config. These utilities are exposed in theconfig.util object for use in your own applications.

Below is a list of these utilities, in order of general usefulness - your mileage may vary.

extendDeep(mergeInto, mergeFrom..., depth)

Extend an object (and any object it contains) with one or more objects (and objects contained in them).

This does not replace deep objects as other extend functions do,but dives into them extending individual elements instead. If more than oneof the extending objects define the same key, then:

  • if all the respective values are objects, they are merged recursively by calling the same function.
  • if none of the respective values are objects, then the latest one is respected.
  • avoid mixing objects and non-objects for the same key as it can lead to unexpected behaviour.
paramtypedescription
mergeIntoobjectThe object to merge into
mergeFrom...objectAny number of objects to merge from
depthintegerAn optional depth to prevent recursion. Default: 20.
(return)objectThe altered mergeInto object is returned

The following example merges the contents of two objects into a new object.

varnewObject=config.util.extendDeep({},baseObject,anotherObject);

cloneDeep(copyFrom, depth)

Return a deep copy of the specified object.

This returns a new object with all elements copied from the specifiedobject. Deep copies are made of objects and arrays so you can do anythingwith the returned object without affecting the input object.

paramtypedescription
copyFromobjectThe original object to copy from
depthintegerAn optional depth to prevent recursion. Default: 20.
(return)objectA new object with the elements copied from the copyFrom object

Example:

varcopy=config.util.cloneDeep(fromObject);

equalsDeep(object1, object2, depth)

Return true if two objects have equal contents.

paramtypedescription
object1objectThe object to compare from
object2objectThe object to compare with
depthintegerAn optional depth to prevent recursion. Default: 20.
(return)booleantrue if both objects have equivalent contents

Example:

varcustomerCopy=config.util.cloneDeep(myCustomer);varsame=config.util.equalsDeep(myCustomer,customerCopy);// <-- truecustomerCopy.creditLimit=7000;varsame=config.util.equalsDeep(myCustomer,customerCopy);// <-- false

diffDeep(object1, object2, depth)

Returns an object containing all elements that differ between two objects.

It works best when object2 originated by deep copying object1, thenchanges were made to object2, and you want an object that would give youthe changes made to object1 which resulted in object2.

paramtypedescription
object1objectThe object to compare from
object2objectThe object to compare with
depthintegerAn optional depth to prevent recursion. Default: 20.
(return)objectA differential object, which if extended onto object1 would result in object2.

makeImmutable(object, propertyName, propertyValue)

Make a javascript object property immutable (assuring it cannot be changed from the current value).

If the propertyName isn't supplied, all properties of the object are made immutable.

Properties which themselves are objects are called recursively, making sub-objects immutable.

New properties can be added to this (and sub) objects, and those properties will not be immutable unless this method is called after adding the properties.

This operation cannot be undone.

paramtypedescription
objectobjectThe object containing the properties to make immutable
propertyNamestring | [string](optional) Property name (or array of names) to make immutable. If not specified, all properties of the object are made immutable
propertyValuemixed(optional) Property value to set the property to before making immutable. Retained for backward compatibility, and for use only when propertyName is not an array.
(return)objectThe original object, with the newly immutable attributes

Example:

vara={hello:'world'};config.util.makeImmutable(a);a.hello='there';console.log('Sorry, hello is still '+a.hello);

makeHidden(object, propertyName, propertyValue)

Make an object property hidden so it doesn't appear when enumerating elements of the object.

The property still exists and can be read from and written to, but it won't show up infor ... in loops,Object.keys(), orJSON.stringify() type methods.

paramtypedescription
objectobjectThe object containing the property to make hidden
propertyNamestringName of the property to make hidden
propertyValuestring(optional) Set the property to this value
(return)objectThe original object, with the newly hidden property

Example:

vara={hello:'world'};console.log('Before hiding: '+JSON.stringify(a));config.util.makeHidden(a,'hello');console.log('After hiding: '+JSON.stringify(a));console.log('Hidden, but still there: '+a.hello);

getEnv(varName)

Get the current value of a config environment variable

This method returns the value of the specified config environment variable,including any defaults or overrides.

Environment variables that you can inspect includeNODE_ENV,NODE_CONFIG_DIR,NODE_CONFIG,HOSTNAME,NODE_APP_INSTANCE, and others listed in theenvironment variables wiki page.

paramtypedescription
varNamestringThe environment variable name
(return)stringThe value of the environment variable

Example:

console.log('Configuration directory: '+config.util.getEnv('NODE_CONFIG_DIR'));

loadFileConfigs(directory, options)

Reads the given directory using the same conventions as the main config directory (including environment variable reading, except forNODE_CONFIG) and returns an object with the result.

If no directory is given, reads the standard config directory and parsesNODE_CONFIG.

This is useful for using with submodules, e.g.

importconfigfrom'config'constourConfigDir=path.join(__dirname,'config')constbaseConfig=config.util.loadFileConfigs(ourConfigDir)config.util.setModuleDefaults('MyModule',baseConfig)

By default, all sources parsed as part of this operations are added to the global list of configuration Sources, despite not being added to the global configuration.

This may, in turn, increase the memory footprint of your application if the utility is called repeatedly. You can disable this behaviour by passing an options object with theskipConfigSources flag set totrue

importconfigfrom'config'constourConfigDir=path.join(__dirname,'config')constoptions={skipConfigSources:true}constbaseConfig=config.util.loadFileConfigs(ourConfigDir,options)config.util.setModuleDefaults('MyModule',baseConfig)

toObject(config)

Returns a raw version of the current config object, or any part of the config if provided, deeply copied without theget,has andutil functions.

If config is not provided (undefined), the current config object is dumped in its entirety.

paramtypedescription
configobjectThe part of the config to copy and serialize. Omit this argument to return the entire config.
(return)objectThe cloned config or part of the config
varresult=config.util.toObject(config.get('db'));

Review this page

<>

Clone this wiki locally

[8]ページ先頭

©2009-2025 Movatter.jp