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

@akirix/convict-model

v0.1.0

Published

Model style decorators for your convict config.

Downloads

7

Readme

Convict Model

Build Status NPM version

GitHub forks GitHub stars

Annotate a class to define and validate your configs using convict just like you do with an ORM. If you like annotating models classes, then this package will tickle your fancy. The code style and patterns are based on Typeorm because they know what's up. If your using a IOC/DI system, ConvictModel will fit in real nice.

Requirements

Quick Links

Contributing | Changelog | Convict | |---|---|---|

Installation

  1. Install the package

npm install @akirix/convict-model --save

  1. Install reflect-metadata if you have not already so the annotations work.

npm install reflect-metadata --save

then import it in global scope aka main file, i.e. app.ts or index.ts

import "reflect-metadata";

  1. Now we install convict and its types so you can control the version

npm install convict --save
npm install @types/convict --save-dev

  1. Make sure annotations are enabled in tsconfig.json
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
  1. (optional) Install JS Yaml if you like yaml over json for configs.

npm install js-yaml --save

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
│   ├── schema           // place where your config entities will go
│   │   └── MyConfig.ts  // sample entity
│   ├── types.d.ts       // place to put your interfaces  
│   └── index.ts         // start point of your application
├── .gitignore           // standard gitignore file
├── config.json          // Your apps config file
├── package.json         // node module dependencies
├── README.md            // a readme file
└── tsconfig.json        // TypeScript compiler options

Take note of the src/schema directory, here we will put our config schema classes which will be annotated with convict schema definitions. This directory can be called whatever you like by the way. Technically you don't even need the folder, it's just a good idea to put similar classes together for organization.

Getting Started

Now we can start building up a model for our config schema.

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 you use convict competitor or no validation on config at all.

src/types.d.ts

declare namespace config {
    export interface MyConfig {
        name: string;
    }
}

2. Define a Schema Class

Now we can define a schema class and decorate it like Christmas. 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/schema/MyConfig.ts

import { Property } from '@akirix/convict-model';

export default class MyConfig implements config.MyConfig {
    
    @Property({
        doc: 'The name of the thing',
        default: 'Convict',
        env: 'MY_CONFIG_NAME'
    })
    public name: 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.json

{
    "name": "Cool App"
}

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 { ConvictModel } from '@akirix/convict-model';

//get your config file however you do it
const myRawConfig = getMyConfigData();

//initialize the ConvictModel with a list of paths or entities to load as the schema
const convictModel: ConvictModel = new ConvictModel(['src/schema/**/*.*s']);

//get your validated config object as a serialized class
const myConfig: config.MyConfig = convictModel.create<config.MyConfig>('MyConfig',myRawConfig);