# @totalsoft/tenant-configuration

> Tenant configuration provider

Latest version **1.0.0** (published 2022-07-04) · MIT license · 0 weekly downloads

## Install

```sh
npm install @totalsoft/tenant-configuration
pnpm add @totalsoft/tenant-configuration
yarn add @totalsoft/tenant-configuration
bun add @totalsoft/tenant-configuration
```

## Health

**Score 40/100 (D)** — status: abandoned.

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

Warnings: low downloads; no esm support.

Negative: abandoned.

## Facts

| | |
|---|---|
| Version | 1.0.0 |
| Published | 2022-07-04 |
| First published | 2022-07-04 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 4 |
| Unpacked size | 46.4 KB |
| Known vulnerabilities | 0 (+1 in 1 direct dependencies) |
| Install scripts | no |
| GitHub stars | 26 |
| Author | TotalSoft |
| Maintainers | osstotalsoft |
| Keywords | tenant, configuration |

## Links

- npm: https://www.npmjs.com/package/@totalsoft/tenant-configuration
- Repository: https://github.com/osstotalsoft/rocket-toolkit
- Homepage: https://github.com/osstotalsoft/rocket-toolkit#readme
- Issues: https://github.com/osstotalsoft/rocket-toolkit/issues
- npm.io page: https://npm.io/package/@totalsoft/tenant-configuration

## Dependencies (4)

- [humps](https://npm.io/package/humps.md) ^2.0.1
- [debounce](https://npm.io/package/debounce.md) ^1.2.1
- [deepmerge](https://npm.io/package/deepmerge.md) ^4.2.2
- [object-path](https://npm.io/package/object-path.md) ^0.11.8

## Alternatives

- [jsforce](https://npm.io/package/jsforce.md) — 851.2K weekly downloads
- [react-native-qrcode-svg](https://npm.io/package/react-native-qrcode-svg.md) — 693.5K weekly downloads
- [@salesforce/plugin-data](https://npm.io/package/@salesforce/plugin-data.md) — 394.9K weekly downloads
- [@backstage/plugin-search-common](https://npm.io/package/@backstage/plugin-search-common.md) — 308.5K weekly downloads
- [@chain-registry/types](https://npm.io/package/@chain-registry/types.md) — 38.4K weekly downloads

## Recent versions

- 1.0.0 (latest) — 2022-07-04

## README

# tenant-configuration
This package allows loading tenant configuration from environment variables.


## Installation

```javascript
npm i @totalsoft/tenant-configuration
```

or

```javascript
yarn add @totalsoft/tenant-configuration
```
## Activation
The following environment variable should be set to 'true' 

```
IS_MULTITENANT: 'true'
```

## Environment variables format

Tenant speciffic environment variables should follow the format `MultiTenancy__Tenants__Tenant1__TenantProp` where:
-  `MultiTenancy__Tenants__` is a fixed prefix
-  `Tenant1` is the tenant code
-  `TenantProp` is a tenant speciffic setting 
-  `__` is the separator for the nested structure

An environment variable with the tenant id is mandatory for each tenant (eg:  `MultiTenancy__Tenants__Tenant1__TenantId`)

Nested properties can be used (eg: `MultiTenancy__Tenants__Tenant1__ConnectionStrings__MyDatabase__Password`)

For settings that are shared by multiple tenants, the folowing format should be used `MultiTenancy__Defaults__DefaultProp` where:
- `MultiTenancy__Defaults__` is a fixed prefix
- `DefaultProp` is a shared setting for all tenants
- `__` is the separator for the nested structure

The default settings are merged with the tenant speciffic settings, and in case of conflicts the tenant speciffic ones have precedence. 


## Usage
### tenantConfiguration
The `getValue` function reads values or complex objects from environment variables:

```javascript
const { tenantConfiguration } = require("@totalsoft/tenant-configuration")

// process.env = {
//    IS_MULTITENANT: 'true',
//    MultiTenancy__Tenants__Tenant1__TenantId: '3c841325-eccc-4670-a577-09546df7b1fc',
//    MultiTenancy__Tenants__Tenant1__TenantProp: 'tenantPropValue'
//}
const tenantSpecifficValue = tenantConfiguration.getValue('3c841325-eccc-4670-a577-09546df7b1fc', "tenantProp")
```
The `getConnectionInfo` extension reads the configuration to return a connection information object:
```javascript
const { tenantConfiguration } = require("@totalsoft/tenant-configuration")

// process.env = {
//    IS_MULTITENANT: 'true',
//    MultiTenancy__Tenants__Tenant1__TenantId: tenantId,
//    MultiTenancy__Defaults__ConnectionStrings__MyDatabase__Server: server,
//    MultiTenancy__Tenants__Tenant1__ConnectionStrings__MyDatabase__Database: db,
//    MultiTenancy__Tenants__Tenant1__ConnectionStrings__MyDatabase__UserName: user,
//    MultiTenancy__Tenants__Tenant1__ConnectionStrings__MyDatabase__Password: pass,
//    MultiTenancy__Defaults__ConnectionStrings__MyDatabase__OtherParams: otherParams,
}
const connectionInfo = tenantConfiguration.getConnectionInfo(tenantId, "myDatabase")
```
The returned `ConnectionInfo` object has the following structure:
```typescript
export interface ConnectionInfo {
    server: string, // Can include instance name and port
    database: string, 
    userName: string, 
    password: string, 
    otherParams?: string
}
```

### tenantService
The tenant service relies on the tenant configuration mechanism described above to read obtain tenant attributes.

The `getTenantFromId` function returns tenant information based on a tenant id.

```javascript
const { tenantService } = require("@totalsoft/tenant-configuration")

// process.env = {
//     IS_MULTITENANT: 'true',
//     MultiTenancy__Tenants__Tenant1__TenantId: tenantId,
//     MultiTenancy__Tenants__Tenant1__Name: 'Tenant 1 name'
}

const res = await tenantService.getTenantFromId(tenantId)
```

The returned `Tenant` object has the following structure:
```typescript
export interface Tenant {
    id: string,
    code: string,
    name?: string
}
```
The service throws an exception when the tenant with the specified id is not found.

---
_Source: https://npm.io/package/@totalsoft/tenant-configuration · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
