alpha-config
TypeScript icon, indicating that this package has built-in type declarations

0.1.4 • Public • Published

Alpha-Config

This nestjs module is just a wrapper over the config npm package, the main feature it provides, the ability to store config in db and change from over-there, which means changing config can be done via http request without the need to re-build the whole application and trigger ci/cd

note: currently we only support sequelize, more on the way (^_^)

So with the result of that, we need to provide a connection to db and a table name to the AlphaConfigModule

Let's dive into it:

First, you need to create a database service to inject the connection as following:

@Global() // this required unless it's provided in a global module
@Injectable()
export class DbService {
    constructor(@InjectConnection('default') private connection: Sequelize) {}
    getSequelizeConnection() {
        return this.connection;
    }
}

note that the @InjectConnection takes a token string to get the connection to database, if you've not defined any connection, the 'default' token will be used by @nestjs/sequelize

Now you can import the AlphaConfigModule as following:

AlphaConfigModule.registerAsync({
    extraProviders: [DbService],
    useFactory: (dbService: DbService) => {
        const sequelizeConnection = dbService.getSequelizeConnection();
        return {
            sequelizeInstance: sequelizeConnection,
            valueAttributeName: 'value',
            schema: {},
            configTableName: 'Configuration' | ModelType<ModelAttributes, ModelCreateionAttributes>,
            configDir: 'config'
        }
    },
    inject: [DbService]
})

Provided functionality

@Injectable()
export class AnyService {
    constructor(
        private readonly alphaConfigService: AlphaConfigService
    ) {
        alphaConfigService.storeItemInDatabase({whatever: 'whateever'}); // this will store the provided object in 'Configuration' table
        alphaConfigService.getItem('key'); // It will look for the key in db, if it's not found it will try to load it from env files, if it does not exist it will throw an error
    }
}

Reference

Parameter Type Description
sequelizeInstance Sequelize Sequelize instance loaded via @InjectConnection in DbService.
valueAttributeName string this attribute is used whenever you call getItem on the database table, so if 'Configuration' table in the database stores the configuration in 'some_string' attribute you can provide it here, or 'value' will be used by default
schema any This schema will be used to validate against the configuration loaded from files using joi library.
configTableName string the allocated table's name in db used to store configuration
configDir string by default config library loads env files from config folder stored in root directory, you can change this behavior using this attribute

Readme

Keywords

none

Package Sidebar

Install

npm i alpha-config

Weekly Downloads

1

Version

0.1.4

License

none

Unpacked Size

135 kB

Total Files

27

Last publish

Collaborators

  • abdulrahman-falyoun
  • zain_khatib