# @beonica/bn.core.config-helper-tool

> Config Helper Tool

Latest version **1.3.2** (published 2021-03-17) · ISC license · 0 weekly downloads

## Install

```sh
npm install @beonica/bn.core.config-helper-tool
pnpm add @beonica/bn.core.config-helper-tool
yarn add @beonica/bn.core.config-helper-tool
bun add @beonica/bn.core.config-helper-tool
```

## Health

**Score 25/100 (F)** — status: abandoned.

Positive: has types; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.3.2 |
| Published | 2021-03-17 |
| First published | 2021-03-12 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 7 |
| Unpacked size | 37.4 KB |
| Known vulnerabilities | 0 (+1 in 1 direct dependencies) |
| Install scripts | no |
| Author | https://github.com/adrianobnu |
| Maintainers | pssdn, gusprado, adrianopt |

## Links

- npm: https://www.npmjs.com/package/@beonica/bn.core.config-helper-tool
- Repository: https://github.com/beonica/bn.core.config-helper-tool
- Homepage: https://github.com/beonica/bn.core.config-helper-tool#readme
- Issues: https://github.com/beonica/bn.core.config-helper-tool/issues
- npm.io page: https://npm.io/package/@beonica/bn.core.config-helper-tool

## Dependencies (7)

- [redis](https://npm.io/package/redis.md) ^3.0.2
- [dotenv](https://npm.io/package/dotenv.md) ^8.2.0
- [dot-object](https://npm.io/package/dot-object.md) ^2.1.4
- [class-validator](https://npm.io/package/class-validator.md) ^0.13.1
- [reflect-metadata](https://npm.io/package/reflect-metadata.md) ^0.1.13
- [class-transformer](https://npm.io/package/class-transformer.md) ^0.3.1
- [class-validator-jsonschema](https://npm.io/package/class-validator-jsonschema.md) ^2.2.0

## Recent versions

- 1.3.2 (latest) — 2021-03-17
- 1.3.1 — 2021-03-17
- 1.3.0 — 2021-03-17
- 1.2.2 — 2021-03-16
- 1.2.1 — 2021-03-16
- 1.0.1 — 2021-03-12

## 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](https://github.com/typestack/class-validator#readme). Additionally, add [JSON Schema](https://www.npmjs.com/package/class-validator-jsonschema) 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](https://github.com/rhalff/dot-object).

```
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
```

---
_Source: https://npm.io/package/@beonica/bn.core.config-helper-tool · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
