nails-config

Nails Configuration

Usage no npm install needed!

<script type="module">
  import nailsConfig from 'https://cdn.skypack.dev/nails-config';
</script>

README

Nails Framework

nails-config

This plugin provides configuration utilities for nails-framework.

It adds a node-etc instance to your app configured to load configuration details from a variety of sources.

Usage

This is a core plugin and is automatically included with nails-framework.

Configuration details are read from each source and deep-merged in to a single object. Sources are loaded in order of priority and don't overwrite existing keys. This makes it possible to override values using command line arguments or environment variables.

Sources

The following sources are loaded by default, in order of priority:

1. Command line arguments (argv)

Parsed with node-optimist

2. Environment Variables (env)

Environment variables should be prefixed with the name of your application as set in package.json (eg. $myapp_apikey.) Defaults to $nails_ when the field can't be found.

3. JSON files

Loads all JSON files from config/{$NODE_ENV}/. Files prefixed with config. are deep-merged with the config object and other files are nested under keys based on their filename (with the extension removed.)

By convention plugins look for a config key based on their name so using seperate files does help to stay organised.

Example Directory Structure

myapp/
  |- config/
      |- development/
          |- config.development
          |- config.secrets
          |- pluginName.json
          |- nails-web.json
          |- etc..
      |- production/
          |- config.development
          |- config.secrets
          |- pluginName.json
          |- nails-web.json
          |- etc..
      |- staging/
      |- etc/

Example JSON file config/development/pluginName.json

{
  "name": "Foo",
  "age": 62,
  "hostname": "127.0.0.1",
  "port": 3000,
  "meta": {
    "yes": "no",
    "bing": {
      "bang": true,
      "this": "that"
    }
  }
}

4. Defaults

Default values can be set by plugins as a fallback.

API

app.config.get(key)

Retrieves the given key from the configuration store. Use a : delimiter to fetch specific keys from objects.

Example:

var name = app.config.get('pluginName:name') // String "Foo"
  , age = app.config.get('pluginName:age')   // Number 62 
  , all = app.config.get('pluginName');      // Object { name: 'foo', age: 62, <etc> }

app.config.set(key, value)

Creates or modifies the given key. Primitives will override exsting values whereas Objects and Arrays will merge.

Example:

app.config.set('pluginName:name', 'Bar'); // Overwritten

app.config.set('pluginName', {            // Merged
  name: 'Jim',
  age: 35
});

Refer to the node-etc documentation for additional methods.

Licence

The MIT License (MIT)

Copyright (c) 2013 James Wyse james@jameswyse.net

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.