Giter Club home page Giter Club logo

metaconfiguration's Introduction

Metarhia Configuration Loader

ci status snyk npm version npm downloads/month npm downloads license

Installation

  • npm install metaconfiguration --save
  • const { Config } = require('metaconfiguration');

Usage

Load configuration with asynchronous constructor:

const { Config } = require('metaconfiguration');
const config = await new Config('./configDirectory');
console.log(config);
// Output example:
// {
//   logger: {
//     enabled: true,
//     keepDays: 100,
//     writeInterval: 3000,
//     writeBuffer: 65536,
//     toStdout: [ 'system', 'fatal', 'error' ]
//   },
//   server: {
//     transport: 'http',
//     address: '127.0.0.1',
//     ports: 80
//   }
// }

or factory:

const { readConfig } = require('metaconfiguration');
const config = await readConfig('./configDirectory');

Specify certain configuration sections to load:

const { Config } = require('metaconfiguration');
const options = { names: ['application', 'gateway'] };
const config = await new Config('./configDirectory', options);

Loag configuration in specified mode:

const { Config } = require('metaconfiguration');
const options = { mode: 'test' };
const config = await new Config('./configDirectory', options);

Specify sections and mode:

const { Config } = require('metaconfiguration');
const options = { mode: 'test', names: ['application', 'gateway'] };
const config = await new Config('./configDirectory', options);

Use custom context (sandbox) to execute configuration js file in it:

const vm = require('node:vm');
const metautil = require('metautil');
const { Config } = require('metaconfiguration');
const context = { duration: metautil.duration };
vm.createContext(context);
const options = { context };
const config = await new Config('./configDirectory', options);

License & Contributors

Copyright (c) 2019-2023 Metarhia contributors. Metaconfiguration is MIT licensed.
Metaconfiguration is a part of Metarhia technology stack.

metaconfiguration's People

Contributors

bob-chemist avatar darkwinngduck avatar dependabot[bot] avatar habiiev avatar imnetcat avatar maksgovor avatar siobhanofzhytomyr avatar tshemsedinov avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

metaconfiguration's Issues

Return promise with sections from constructor

Now we use:

const config = await new Config(path);
const value = config.sections.<name>.<field>;

We can't just use await new Config(path).sections because await will be applied to sections, not to the value returned from constrictor. But if we will return promise with sections from constructor instead of promise with config instance, we can do following:

const sections = await new Config(path);
const value = sections.<name>.<field>;

Rename options sandbox to context

We need to rename for naming consistency between metavm, metarhia/config, 'impress' and node.js API.

Change signature await new Config(dirPath, { sandbox }) to await new Config(dirPath, { context }) but we need to support both names sandbox and context for version 2.x. In version 3.x option sandbox will be deprecated.

Load just specified sections

For example: await new Config(path, ['sectionName'])
By default all sections will be loaded.
Or with options: await new Config(path, { mode: 'debug' }, ['sectionName'])

Add unit tests

  • Now we have just test stubs (it's just examples), need to rewrite it using metatests
  • Tests should exit with code 1 to fail (now unhanded promise rejection exit with 0)
  • Need more cases including negative tests (which should fail and we will check it)

Add the ability to describe all configs in several files

Is your feature request related to a problem? Please describe.

In my opinion, there are many files in config directory, it would be great to have possibility, to describe env variables in few files.

Describe the solution you'd like

Map by specific file names, set order priorities and highest priority for files as it was before.

Describe alternatives you've considered

It realized in node-config file-load-order

Additional context

development.js

({
   server: { host: '0.0.0.0' },
   database: { 
     host: process.env.DB_HOST, 
     port: process.env.DB_PORT,
     schema: process.env.DB_SCHEMA
   },
   authSecret: process.env.AUTH_SECRET,
   //...
})

test.js

({
   server: { host: '127.0.0.1' },
   database: { 
     host: '127.0.0.1', 
     port: 5432,
     schema: 'test_db'
   },
   authSecret: 'test_auth_secret',
   //...
})

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.