npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@turanga/config

v0.3.0

Published

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.

Downloads

1

Readme

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 
}