Skip to content

Commit

Permalink
Update docs.
Browse files Browse the repository at this point in the history
  • Loading branch information
codemariner committed May 30, 2023
1 parent 89e243a commit c35e84f
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 3 deletions.
8 changes: 7 additions & 1 deletion DOC.md
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ Registers a named configuration with associated schema. This will load, validate

## Modular Configuration

Application modules may register local configuration with tconf while maintaining the same configuration sources.
Application modules may register configuration with tconf while using the same configuration sources.

First, initialize your global configuration. This will establish the common location and options for loading configuration.

Expand All @@ -291,6 +291,7 @@ const Config = Record({
})
})
// exporting this so modules can register their configuration
export const tconf = initialize({
path: path.join(__dirname, '..', 'config),
schema: Config
Expand Down Expand Up @@ -325,6 +326,11 @@ crypto: # <-- module config
key: 6K0CjNioiXER0qlXRDrOozWgbFZ9LmG/nnOjl0s4NqM=
```

{% note %}
**Note:** Tconf will provide all configuration it finds and does not filter any out when requesting from the top level `tconf.get()`. However, when strictly typing with Runtypes and TypeScript, other module configuration types are not exposed. In this way, your application code can act as if it doesn't exist though it literally does.
{% endnote %}


## Environment Variable Mapping

In some situations, it's preferred to specify deployment related configuration based on environment variables.
Expand Down
45 changes: 43 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -125,9 +125,9 @@ This is also optional. _tconf_ natively supports [configuration mapping](./DOC.m
```typescript
// src/config.ts
import loadConfig from 'tconf'
import { initialize } from 'tconf'

export default loadConfig({
const tconf = initialize({
// directories containing configuration files
path: path.join(__dirname, '..', 'config'),
// the runtypes Config object (optional)
Expand All @@ -136,6 +136,7 @@ export default loadConfig({
// default.yaml, ${NODE_ENV}.yaml, and env.yaml
sources: ['default', 'NODE_ENV', 'env'],
})
export default tconf.get();

```
_tconf_ will import configurations from the defined sources (or a set of defaults) from the [specified directories](./DOC.md#path-required), and merge the values in the order of the [specified sources](./DOC.md#sources-optional).
Expand All @@ -149,6 +150,46 @@ import dbConnect from './db'
const conn = await dbConnect(config.database);
```

### 6. use in isolated modules
Within larger applications, you may want to isolate certain areas of your code into modules (i.e. modular monolith). It may make sense to isolate your configuration to such modules as well.

First, expose your initialized Tconf instance:
```typescript
// src/config.ts
import { initialize } from 'tconf'

export const tconf = initialize({ // <-- export the instance
// ...
})
export default tconf.get();
```

Then in your module, register your configuration schema and provide access to your module.

```typescript
// src/modules/db/config.ts
import {tconf} from '../../config'

const Config = Record({
uri: String
})

const config = tconf.register('database', Config); // Static<typeof Config>

export default config

```

The configuration will be sourced the same way, but you'll need to add your configuration under the registered name.
```yaml
# config/default.yaml
api:
# //...

database:
uri: postgresql://host.com:5432/appdb
```
## Documentation
Expand Down

0 comments on commit c35e84f

Please sign in to comment.