Code Monkey home page Code Monkey logo

ts-convict's Introduction

TS Convict

NPM version Build Status Coverage Status

GitHub forks GitHub stars

Annotate a class to define and validate your configs using convict just like you do with an ORM. Brings true serialized class types to your config when loaded. If you like annotating models classes with Typescript, then this package will work well. If your using a IOC/DI system, TSConvict will fit in real nice. The idea is inspired by projects like Typeorm and Inversify.

Quick Links

Contributing Changelog Convict

Requirements

Features

  • all the power and then some from convict
  • define convict schemas with decorators
  • get your config as serialized classes
  • extremely simple and intuitive implementation
  • very pretty code for defining your apps config

Installation

  1. Install the package and dependencies.

npm install ts-convict convict reflect-metadata --save
npm install @types/convict --save-dev

Optionally install a parser of your choice if your config is not JSON. For example you can also use YAML.

npm install js-yaml --save

  1. Make sure reflect-metadata Is configured correctly for Typescript.

2.1 tsconfig.json

"emitDecoratorMetadata": true,
"experimentalDecorators": true,

2.2 Import in main file
index.ts

import "reflect-metadata";

or import with node command
node -r reflect-metadata

Project Setup

First we need a proper project setup like the one below with a folder to hold our config schema classes. This is a very simple Typescript folder structure.

MyProject
├── src                  // place of your TypeScript code
│   ├── config           // place where your config entities will go
│   │   ├── MyConfig.ts  // the main config
│   │   ├── Database.ts  // sample database config
│   │   └── SubConfig.ts // a nested config
│   ├── types.d.ts       // place to put your interfaces  
│   └── index.ts         // start point of your application
├── .gitignore           // standard gitignore file
├── config.yml           // Your apps config file
├── db.json              // Your apps other db config as json
├── package.json         // node module dependencies
├── README.md            // a readme file
└── tsconfig.json        // TypeScript compiler options

Take note of the src/config directory, here we will put our convict schema classes.
The classes will be annotated with convict schema definitions. This directory can be called whatever you like.

Useage

1. Define an Interface

It's a good idea to define an interface so your experience can be agile and include all the fancy IDE features. Interfaces also open an opportunity to have more than one implementation of your config, i.e. maybe youswitch to a convict competitor or maybe just have no validation on config at all, i.e. require('config.json').

src/types.d.ts

declare namespace config {
    export interface MyConfig {
        name: string;
        subConfig: SubConfig;
        db: Database;
    }
    export interface SubConfig {
        bar: number;
    }
    export interface Database {
        host: string;
        port: number;
        database: string;
        user: string;
        password: string;
    }
}

2. Define a Schema Class

Now we can define a schema class and decorate it. The parameter for @Property decorator is simply a convict SchemaObj like in normal convict. You can read all about the possible options in convicts documentation.

src/config/MyConfig.ts

import { Property, Config } from 'ts-convict';
import SubConfig from './SubConfig';
import Database from './Database';
import * as yaml from 'js-yaml';
import { url, ipaddress } from "convict-format-with-validator";

@Config({
    
    // optional default file to load, no errors if it doesn't exist
    file: 'config.yml',// relative to NODE_PATH or cwd()

    // optional parameter. Defaults to 'strict', can also be 'warn' or 'skip'
    validationMethod: 'strict',

    // optionally add parsers like yaml or toml
    parser: { 
        extension: ['yml', 'yaml'], 
        parse: yaml.safeLoad
    },

    //optional extra formats to use in validation
    formats: {
        url,
        ipaddress,
    }
    
})
export class MyConfig implements config.MyConfig {
    
    // ts-convict will use the Typescript type if no format given
    @Property({
        doc: 'The name of the thing',
        default: 'Convict',
        env: 'MY_CONFIG_NAME'
    })
    public name: string;

    @Property(SubConfig)
    public subConfig: config.SubConfig;

    @Property(Database)
    public db: config.Database;

}

src/config/SubConfig.ts

import { Property } from 'ts-convict';

export class SubConfig implements config.SubConfig {
    @Property({
        doc: 'A sub prop',
        default: 3,
        env: 'SUB_CONFIG_BAR',
        format: 'int'
    })
    public bar: number;

    public message: string = "I am an unmanaged config property";
}

Database.ts

import { Property } from 'ts-convict';

export class Database implements config.Database {
    @Property({
        doc: "The database host",
        default: "localhost",
        format: "url",
        env: "DATABASE_HOST"
    })
    public host: string;

    @Property({
        doc: "The database port",
        default: 5432,
        format: "port",
        env: "DATABASE_PORT"
    })
    public port: number;

    @Property({
        doc: "The database db",
        default: "my_db",
        env: "DATABASE_DB"
    })
    public database: string;

    @Property({
        doc: "The database user",
        default: "magik",
        env: "DATABASE_USER"
    })
    public user: string;

    @Property({
        doc: "The database pass",
        default: "secretpassword",
        sensitive: true,
        env: "DATABASE_PASS"
    })
    public password: string;
}

3. Make a Configuration

Now we can make our configuration for our app. This can be a hardcoded Object in your code, a json file, a yaml file, or however you do it. In the end it's up to you how you type out and load the data.

config.yml

name: Cool App
subConfig: 
    bar: 5
db:
    user: devuser
    password: devpassword

db.json

{
    "user": "someuser",
    "password": "somepassword",
    "host": "somedb.com"
}

4. Load it up

We have a couple of ways to load it up so you can choose what works for your unique situation. The example below is the simplest way in the spirit of TL;DR.

src/index.ts

import { TSConvict } from 'ts-convict';
import { MyConfig } from "./config/MyConfig";
import { Database } from "./config/Database";
import { SubConfig } from "./config/SubConfig";

// example loading default file defined in @Config
const myConfigLoader = new TSConvict<MyConfig>(MyConfig);
const myConfig: MyConfig = myConfigLoader.load();

// example loading with file passed to load
const dbLoader = new TSConvict<Database>(Database);
const dbConfig: Database = dbLoader.load('db.json');

// example loading an ad hoc config class with raw data
const rawSub: config.SubConfig = {
    bar: 22
};
const subLoader = new TSConvict<SubConfig>(SubConfig);
const subConfig = subLoader.load(rawSub);

ts-convict's People

Contributors

abmania avatar kferrone avatar shijiezhou1 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

ts-convict's Issues

Inheritance and Reusing Parent Classes

So it seems one class can inherit another class so long as the base class is never reused in any other instance of TSConvict. Not sure if it is even possible to have more than one class extend a base class and then have the two used in the same config. Seems like the base class will inherit the metadata of the extending class.

Their is a test in the test folder called inheritance and it has the tests to make sure this is possible.

Allow Skipping Validation

I have a use-case where I'm wrapping with my own library that integrates ts-convict and NestJS together to provide the foundation of config management for the rest of my NestJS libraries. So far we've been able to do this quite well with the existing implementation of ts-convict, but we are running into some interesting scenarios with exposing multiple configs based off the same config file but distributed independently in each library.

Let me explain a bit further with this example. Imagine our app is broken up into several modules (possibly in different, independently published NPM packages):

app.config.ts # Root app config
/logging
 - logging.config.ts # Expects to be configured under a "logging" sub-key
/auth
 - auth.config.ts # Expects to be configured under an "auth" sub-key
/metrics
 - metrics.config.ts # Expects to be configured under a "metrics" sub-key

We'd like to be able to have each of these libraries load its own subkey from the main config yml file which may look something like this:

appName: my-app
db:
  connectionString: someConnectionString

# Configures logging library
logging:
  logLevel: DEBUG

# Configures auth library
auth:
  issuer: ...

metrics:
  timeSeriesDbUri: ...

This works for us right now as long as we use the validationMethod: 'warn' option, but of course we get a bunch of warnings in our logs about the config file having fields out of sync with the config class since each sub config gets initialized independently. What are your thoughts on introducing a value to validationMethod that will allow it to skip validation altogether?

Update some packages

A lot of the dependencies are quite old. See what can be done about updating them without breaking too much.

Load More Configs Successively

Ability to load a config, and then load another one later. This would overlay the new values from the new config over the existing values which were already loaded.

config.load('somefile.yaml'); // first time
config.load({ foo: 'bar' }); // load again to overlay new values

Move validation level (i.e. strict/warn) to the config decorator

Currently it makes more sense to expand the interface to allow a validationLevel to be passed in as an optional parameter directly into the config decorator rather than an additional argument when the schema is loaded. It's much cleaner.

Here instead:

@Config({
    file: 'config/config.json',
    parser: {
        extension: ['json'],
        parse: JSON.parse
    },
    validationLevel: 'warn'
})
export default class myConfigClass implements config.myInterface {
...

New version of reflect-metadata results in TypeError

Tried to deploy a project without any change to package,json and was greeted with this error:

TypeError: Cannot read properties of undefined (reading 'prototype')
    at Reflector.isConstructor (...\node_modules\ts-convict\dist\Reflector.js:12:26)
    at TSConvict.getSchemaFor (...\node_modules\ts-convict\dist\TSConvict.js:79:37)
    at new TSConvict (...\node_modules\ts-convict\dist\TSConvict.js:28:28)
    ...

Looking at the generated JavaScript code, the error was occurring for all __decorate calls except first one

__decorate([
    (0, ts_convict_1.Property)({
        doc: 'Current node environment',
        format: 'String',
        default: '',
        env: 'NODE_ENV'
    }),
    __metadata("design:type", String)
], Config.prototype, "nodeEnv", void 0);
__decorate([
    (0, ts_convict_1.Property)({
        doc: 'Whether framework should output debug logs or not',
        default: true,
        format: 'Boolean',
        env: 'DEBUG_ENABLED'
    }),
    __metadata("design:type", Boolean)
], Config.prototype, "debug", void 0);

In the example above, debug and anything below resulted in error. If I removed nodeEnv then debug worked fine but anything below - error again.

The problem happens because [email protected] and [email protected] seem to be incompatible. The version specified in this module is "reflect-metadata": "^0.1.13" which results in installing latest version. I am not sure if the problem is in reflect-metadata. For me the solution was to specify a fixed version of reflect-metadata in my project's package.json:

"reflect-metadata": "0.1.13"

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.