Modules@robinpath/config
config

@robinpath/config

0.1.1Node.jsPublic

Multi-source configuration management with deep merge, dot-path access, env loading, and validation

Config

Multi-source configuration management with deep merge, dot-path access, env loading, and validation

Package: @robinpath/config | Category: Other | Type: Utility

Authentication

No authentication required. All functions are available immediately.

Use Cases

Use the config module when you need to:

  • Create config with defaults -- Use config.create to perform this operation
  • Load config from file (.json, .env) -- Use config.load to perform this operation
  • Load from environment variables -- Use config.loadEnv to perform this operation
  • Get config value by dot path -- Use config.get to perform this operation
  • Get entire config -- Use config.getAll to perform this operation

Quick Reference

FunctionDescriptionReturns
createCreate config with defaultsConfig object
loadLoad config from file (.json, .env)Merged config
loadEnvLoad from environment variablesMerged config
getGet config value by dot pathConfig value
setSet config value by dot pathtrue
getAllGet entire configFull config
mergeDeep merge into configMerged config
hasCheck if path existstrue if exists
removeRemove config keytrue if removed
clearClear entire configtrue
listList all config namesConfig names
validateValidate required keys exist{valid, missing}
freezeFreeze config (immutable)true
toEnvConvert config to env formatEnv-format string

Functions

create

Create config with defaults

Module: config | Returns: object -- Config object

config.create "app" {"port": 3000, "debug": false}
ParameterTypeRequiredDescription
namestringNoConfig name
defaultsobjectNoDefault values

load

Load config from file (.json, .env)

Module: config | Returns: object -- Merged config

config.load "./config.json"
ParameterTypeRequiredDescription
filePathstringYesConfig file path
namestringNoConfig name

loadEnv

Load from environment variables

Module: config | Returns: object -- Merged config

config.loadEnv "APP_"
ParameterTypeRequiredDescription
prefixstringNoEnv var prefix filter
namestringNoConfig name

get

Get config value by dot path

Module: config | Returns: any -- Config value

config.get "database.host" "localhost"
ParameterTypeRequiredDescription
pathstringYesDot-separated path
defaultanyNoDefault value
namestringNoConfig name

set

Set config value by dot path

Module: config | Returns: boolean -- true

config.set "database.port" 5432
ParameterTypeRequiredDescription
pathstringYesDot-separated path
valueanyYesValue
namestringNoConfig name

getAll

Get entire config

Module: config | Returns: object -- Full config

config.getAll
ParameterTypeRequiredDescription
namestringNoConfig name

merge

Deep merge into config

Module: config | Returns: object -- Merged config

config.merge {"database": {"port": 5432}}
ParameterTypeRequiredDescription
dataobjectYesData to merge
namestringNoConfig name

has

Check if path exists

Module: config | Returns: boolean -- true if exists

config.has "database.host"
ParameterTypeRequiredDescription
pathstringYesDot-separated path
namestringNoConfig name

remove

Remove config key

Module: config | Returns: boolean -- true if removed

config.remove "debug"
ParameterTypeRequiredDescription
pathstringYesDot-separated path
namestringNoConfig name

clear

Clear entire config

Module: config | Returns: boolean -- true

config.clear
ParameterTypeRequiredDescription
namestringNoConfig name

list

List all config names

Module: config | Returns: array -- Config names

config.list
ParameterTypeRequiredDescription
(none)NoCall with no arguments

validate

Validate required keys exist

Module: config | Returns: object -- {valid, missing}

config.validate ["database.host", "database.port"]
ParameterTypeRequiredDescription
requiredarrayYesRequired dot paths
namestringNoConfig name

freeze

Freeze config (immutable)

Module: config | Returns: boolean -- true

config.freeze
ParameterTypeRequiredDescription
namestringNoConfig name

toEnv

Convert config to env format

Module: config | Returns: string -- Env-format string

config.toEnv "app" "APP_"
ParameterTypeRequiredDescription
namestringNoConfig name
prefixstringNoKey prefix

Error Handling

All functions throw on failure. Common errors:

ErrorCause
(standard errors)Check function parameters and authentication
@desc "Create and validate result"
do
  set $result as config.create "app" {"port": 3000, "debug": false}
  if $result != null
    print "Success"
  else
    print "No result"
  end
enddo

Recipes

1. List and iterate

Retrieve all items and loop through them.

@desc "Get and iterate results"
do
  set $result as config.get "database.host" "localhost"
  each $item in $result
    print $item
  end
enddo

2. Create a new item with create

Create a new resource and capture the result.

set $result as config.create "app" {"port": 3000, "debug": false}
print "Created: " + $result

3. Check before creating

List existing items and only create if needed.

@desc "Get and create"
do
  set $existing as config.get "database.host" "localhost"
  if $existing == null
    config.create "app" {"port": 3000, "debug": false}
    print "Item created"
  else
    print "Item already exists"
  end
enddo

4. Multi-step Config workflow

Chain multiple config operations together.

@desc "Create, load, and more"
do
  set $r_create as config.create "app" {"port": 3000, "debug": false}
  set $r_load as config.load "./config.json"
  set $r_loadEnv as config.loadEnv "APP_"
  print "All operations complete"
enddo

5. Safe create with validation

Check results before proceeding.

@desc "Create and validate result"
do
  set $result as config.create "app" {"port": 3000, "debug": false}
  if $result != null
    print "Success: " + $result
  else
    print "Operation returned no data"
  end
enddo

Related Modules

  • json -- JSON module for complementary functionality

Versions (1)

VersionTagPublished
0.1.1latest1 months ago
Install
$ robinpath add @robinpath/config

Collaborators

Dumitru Balaban
Dumitru Balaban
@dumitru
View all @robinpath modules
Version0.1.1
LicenseMIT
Unpacked Size5.5 KB
Versions1
Weekly Downloads26
Total Downloads26
Stars0
Last Publish1 months ago
Created1 months ago

Keywords

Category

utilities