0.1.4 • Published 2 years ago

alpha-config v0.1.4

Weekly downloads
-
License
-
Repository
-
Last release
2 years ago

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

0.1.4

2 years ago

0.1.3

2 years ago

0.1.2

2 years ago

0.1.1

2 years ago

0.1.0

2 years ago

0.0.9

2 years ago

0.0.8

2 years ago

0.0.7

2 years ago

0.0.6

2 years ago

0.0.5

2 years ago

0.0.4

2 years ago

0.0.3

2 years ago

0.0.2

2 years ago

0.0.1

2 years ago