0.3.0 • Published 1 year ago

@turanga/config v0.3.0

Weekly downloads
-
License
MIT
Repository
-
Last release
1 year ago

Config

The purpose of this package is to provide access for configuration options in various topic related config files like for application, database, caching, etc. Serving configuration options in a nested manner makes them even more readable and maintainable. While application configuration attributes remain the same over different environments, their values are not.

This is where a combination of existing concepts come into place. While dotenv provides access to env files, they are supposed to be splitted by environment, not application domain specific and don't provide nested configuration options. A package like node-config has the ability to split configurations per domain topics but doesn't provide environment specific overrides.

The package is built on top of dotenv, while providing more flexibility in regard to nested configuration files.

Installation

npm install js-turanga/config

Configuration Files

Configuration files have to be located in a config folder underneath the application root folder

|- application					application root folder
	|- config 					configuration folder
		| - config files 		configuration files

This package is limited to configuration files in json5 format. Json5 provides the ability to include comments and therefore better readablility. Configuration files are made available if they have an extension of .js, .json, .json5 or even if extension is left empty. In any case, the files are parsed in json5 format.

/config/database.json
{
	"host": "localhost",
	"port": 1433,
	"database": "contoso",

	"accounts": {
		"username": "dbuser",
		"password": "secret"
	}
}

While it seems obvious, that these configuration attributes are required in any environment, it's values are not. Also, sensitive values like passwords are not supposed to be stored in configuration files which are commited in public repositories. There is where .env files and dotenv comes into play

/config/database.json
{
	"host": ["DB_HOST", "localhost"],
	"port": ["DB_PORT", 1433],
	"database": ["DB_NAME", "contoso"],

	/* 
	 * some multiline comments
	 */
	"charset": "utf8",

	// some comments //
	"accounts": {
		"username": ["DB_USER", "dbuser"],
		"password": ["DB_PASS", ""]
	}
}

A configuration value provides in array syntax provide for extended flexibility. Taken the example above, if DB_HOST is available in the .env file, it's value will be set for the database host. If the .env hasn't a property and value set for DB_HOST then the second array item will be provides as default value for the database host. This concept allows for environment specific settings and overrides of default values. Think of a deployments in development or testing environments with a ready-to-use approach. Such application blueprints or templates are in need of default configurations which apply even without a need for a developer or user to change the defaults.

Usage

Require and initialise package in your project like:

const config = require( "config.js" );

Access configuration values. It's supposed that the first segment in dotted notation is the filename, while the following notation is a dotted notation to access the nested configuration object

// access the application name, residing in the application configuration file, provided by the attribute name
const value = config.get('application.name')

/config/application.json
{
	"name": "Contoso" 
}


// access the database port, residing in the database configuration file, provided by the nested attribute server.port
const value = config.get('database.server.port')

/config/database.json
{
	"server": {
		"port": 5432
	} 
}

If configuration files or its properties are not available, then the resolver returns null

// usage of an invalid path
const value = config.get('database.server.port')
> value = null

/config/database.json
{
	"host": "localhost",
	"port": 5432 
}