@beonica/bn.core.config-helper-tool
v1.3.2
Published
Config Helper Tool
Downloads
46
Readme
bn.core.config-helper-tool
Installation
Copy the env files
cp .env.example .env
Install NPM
npm install
Run
npm run start
Run tests
npm run test
Install plugin in a service
Add this plugin and class-validator to the project with NPM
npm i @beonica/bn.core.config-helper-tool
npm i class-validator class-validator-jsonschema
Create a class for each scope with all configs of the service using the validations and types described in the class-validator plugin. Additionally, add JSON Schema definitions to better describe your data models.
class TenantConfig {
@IsString()
@Length(10, 20)
@JSONSchema({
description: 'The title attribute',
format: 'text',
})
title: string;
@IsString()
@Contains('a')
@Length(10, 20)
text: string;
@MaxLength(20, {
each: true,
})
tags: string[];
}
In the project main file, call the plugin passing the service name, a config class and the Redis String Connection.
const configs = new ConfigHelperTool('SERVICE_NAME', new TenantConfig(), {
redisConnectionString: REDIS_CONNECTION_STRING,
});
General
In the set() and get() method, always use a dot notation.
await configs.get('default','tags.0');
await configs.set('tenant2','param1','A');
The methods getAll() always return a object formatted in dot notation.
The methods that handle sets relates directly to Redis defautl set commands, except for the action commands, like sadd, srem, which response is translated to booleans.
Avoid using dot notation on set names.
Methods
describe()
Converts data model definition into OpenAPI-compatible JSON Schema. Usefull to describe model definitions to APIs and frontend apps.
const configs = new ConfigHelperTool('SERVICE_NAME', new TenantConfig(), {
redisConnectionString: REDIS_CONNECTION_STRING,
});
config.describe();
setAll(scope:string, values: object)
In this case, the service have to fill the "ServiceConfig" class and pass to this function.
let defaultConfig = new TenantConfig();
defaultConfig.title = 'My Service Title';
defaultConfig.text = 'Used to make something';
defaultConfig.tags = ['a','b','c']
await configs.setAll('default',defaultConfig);
getAll(scope:string)
This method return all configs setted in dot notation for the scope
await configs.getAll('default');
//Response
{
'title': 'My Service Title',
'text': 'Used to make something',
'tags[0]': 'a',
'tags[1]': 'b',
'tags[2]': 'c'
}
set(scope:string, key:string, value:string)
This method set only one property for the scope
await configs.set('default','text','Changing Text Property');
await configs.set('tenant0','text','A+A');
get(scope:string, key:string)
This method return one config in dot notation
await configs.get('default','title');
//Response
'My Service Title'
await configs.get('tenant0','text');
//Response
'A'
Dealing with sets
A Redis Set is a list of items, on wich we could store, move, delete and check if given is a member.
All sets must exists under an unique key, the set name. Unlike the get () and set () methods, where you specify a scope, a parameter name and a value, when dealing with sets you specify a scope, the name of the set and the name of the item - which must be a string.
push(scope:string, set:string, item:string)
This method adds an item to set, then return true.
await configs.push('default', 'colors', 'blue');
await configs.push('default', 'colors', 'red');
await configs.push('default', 'colors', 'yellow');
//Response
true
true
true
remove(scope:string, set:string, item:string)
This method removes an item to set, then return true.
await configs.remove('default', 'colors', 'blue');
//Response
true
members(scope:string, set:string)
This method returns an array containing all set members.
await configs.members('default', 'colors');
//Response
['red', 'yellow]
exists(scope:string, set:string, item:string)
This method check if given item is present on set. Returns boolean.
await configs.exists('default', 'colors', 'blue');
//Response
false
await configs.exists('default', 'colors', 'red');
//Response
true
length(scope:string, set:string)
This method returns set cardinality - its length.
await configs.length('default', 'colors');
//Response
2
Publish in NPM
Always change de package version in package.json
npm login
npm publish --access=public