Skip to content

Commit c35e84f

Browse files
committed
Update docs.
1 parent 89e243a commit c35e84f

2 files changed

Lines changed: 50 additions & 3 deletions

File tree

DOC.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,7 @@ Registers a named configuration with associated schema. This will load, validate
275275

276276
## Modular Configuration
277277

278-
Application modules may register local configuration with tconf while maintaining the same configuration sources.
278+
Application modules may register configuration with tconf while using the same configuration sources.
279279

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

@@ -291,6 +291,7 @@ const Config = Record({
291291
})
292292
})
293293
294+
// exporting this so modules can register their configuration
294295
export const tconf = initialize({
295296
path: path.join(__dirname, '..', 'config),
296297
schema: Config
@@ -325,6 +326,11 @@ crypto: # <-- module config
325326
key: 6K0CjNioiXER0qlXRDrOozWgbFZ9LmG/nnOjl0s4NqM=
326327
```
327328

329+
{% note %}
330+
**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.
331+
{% endnote %}
332+
333+
328334
## Environment Variable Mapping
329335

330336
In some situations, it's preferred to specify deployment related configuration based on environment variables.

README.md

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -125,9 +125,9 @@ This is also optional. _tconf_ natively supports [configuration mapping](./DOC.m
125125
126126
```typescript
127127
// src/config.ts
128-
import loadConfig from 'tconf'
128+
import { initialize } from 'tconf'
129129

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

140141
```
141142
_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).
@@ -149,6 +150,46 @@ import dbConnect from './db'
149150
const conn = await dbConnect(config.database);
150151
```
151152

153+
### 6. use in isolated modules
154+
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.
155+
156+
First, expose your initialized Tconf instance:
157+
```typescript
158+
// src/config.ts
159+
import { initialize } from 'tconf'
160+
161+
export const tconf = initialize({ // <-- export the instance
162+
// ...
163+
})
164+
export default tconf.get();
165+
```
166+
167+
Then in your module, register your configuration schema and provide access to your module.
168+
169+
```typescript
170+
// src/modules/db/config.ts
171+
import {tconf} from '../../config'
172+
173+
const Config = Record({
174+
uri: String
175+
})
176+
177+
const config = tconf.register('database', Config); // Static<typeof Config>
178+
179+
export default config
180+
181+
```
182+
183+
The configuration will be sourced the same way, but you'll need to add your configuration under the registered name.
184+
```yaml
185+
# config/default.yaml
186+
api:
187+
# //...
188+
189+
database:
190+
uri: postgresql://host.com:5432/appdb
191+
```
192+
152193
153194
## Documentation
154195

0 commit comments

Comments
 (0)