4.3.0 • Published 3 years ago

connor-base-config v4.3.0

Weekly downloads
2
License
ISC
Repository
github
Last release
3 years ago

CircleCI Libraries.io dependency status for GitHub repo GitHub release Dependent repos (via libraries.io)

Connor's Base Config Package

I tend to use this boilerplate package for configuration. It adds a basic config object with some useful data in it, which you can then add to to have a nice global repository store. Designed to work with my logging boilerplate package.

This uses a modified version of convict but completely destroys its core principles by making the schema dynamic. I wanted this so I could stack packages on top of each other and add new schemas for each package.

The config is stored as a global symbol. Every time you require connor-base-config it will be referencing the same config variable. This means that no matter where you load config in your project, it will be available to every module that requires it.

Features

  • Follows Convict's schemas and maintains its API - Should be a drop-in replacement for most.
  • JSON5 by default
  • Stackable schema
  • Stored as a globally consistent singleton
  • Can load config from various sources (env vars, arguments, in-line)
  • Optional validation
  • Optionally load config before loading the schema
  • Proper schema support for arrays
  • Booleans work from env vars now

Install

npm install connor-base-config

Boilerplate snippet

// This can be copied into new projects as boilerplate for config
// config.js

const config = require('connor-base-config');

config.load({
    "environment.level": "production",
    "metadata.stack": {
        MyItem: true
    }
});

//Add additional values to the schema
config.addToSchema({
   test: {
     name: {
      doc: "What's the name of the task we're doing?",
      format: "String",
      default: "None",
      env: "JOB_NAME"
    }
   }
})

//Load additional config to the newly updated schema
config.load({
  test: {
      name: "Testing"
  }
})

//console.log(config.getProperties());
console.log(config.get("test.name"))

module.exports = config;

Or as a shortened example:

const config = require('connor-base-config')
               .addToSchema(require('./job_schema.json5'))
               .addToSchema(require('./task_schema.json5'))
               .set("task.name", "My Awesome Task")

How should I lay out my config?

I haven't enforced any particular layout, but my go-to is to essentially use namespaces. Each package that adds its own schema on top gets its own namespace, so you end up with something like this:

{
    metadata: {...},
    task: { //new "namespace"
        name: "Convert a file",
        format: "csv",
        csv: { //new "namespace"
            delimiter: ",",
            escapecharacter: "\""
        }
    }
}

Array schemas

You can put schemas in arrays now to make sure every item conforms to the schema:

    config.addToSchema({
        sources: {
            doc: 'A collection of data sources.',
            format: 'Array',
            default: [],
            children: {
                type: {
                    doc: 'The source type',
                    format: ['git', 'hg', 'svn'],
                    default: null
                },
                url: {
                    doc: 'The source URL',
                    format: 'String',
                    default: null
                },
                test: {
                    doc: "Test",
                    default: "Test"
                }
            }
        }
    });

    config.load({
        'sources': [
            {
                'type': 'git',
                'url': 'https://github.com/mozilla/node-convict.git'
            },
            {
                'type': 'git',
                'url': 'https://github.com/github/hub.git'
            }
        ]
    })
    
    
    console.log(config.getProperties())
    /*
        Will return:
          sources: [
            {
              type: 'git',
              url: 'https://github.com/mozilla/node-convict.git',
              test: 'Test'
            },
            {
              type: 'git',
              url: 'https://github.com/github/hub.git',
              test: 'Test'
            }
          ]
    
     */

Extra Commands

For the full list of commands and how schemas work, check Convict's repo. This fork adds the following two functions to the config prototype:

addToSchema: Takes a JSON/JSON5 blob and appends it to the current schema, re-applying all config values on top of the new schema. This can also be used to overwrite parts of the old current schema, but that isn't recommended behaviour.

updateSchema: Takes a JSON/JSON5 blob and replaces the entire current schema with it, re-applying all config values on top of the new schema.

addFormat: The same as Convict's one, except it works on an already-initialised config object.

This fork also modifies Convict's getSchema and getSchemaString to no longer transform them, so you'll get back exactly what you put in.

4.3.0

3 years ago

4.2.3

4 years ago

4.2.2

4 years ago

4.2.1

4 years ago

4.2.0

4 years ago

4.1.4

4 years ago

4.1.3

4 years ago

4.1.2

4 years ago

4.1.1

4 years ago

4.1.0

4 years ago

4.0.0

4 years ago

3.2.0

4 years ago

3.1.3

5 years ago

3.1.2

5 years ago

3.1.1

5 years ago

3.1.0

5 years ago

3.0.3

5 years ago

3.0.2

5 years ago

3.0.1

5 years ago

3.0.0

5 years ago

2.2.2

5 years ago

2.2.1

5 years ago

2.2.0

5 years ago

2.1.0

5 years ago

2.0.2

5 years ago

2.0.1

5 years ago

2.0.0

5 years ago

1.1.1

5 years ago

1.1.0

5 years ago

1.0.0

5 years ago