You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: DOC.md
+7-1Lines changed: 7 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -275,7 +275,7 @@ Registers a named configuration with associated schema. This will load, validate
275
275
276
276
## Modular Configuration
277
277
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.
279
279
280
280
First, initialize your global configuration. This will establish the common location and options for loading configuration.
281
281
@@ -291,6 +291,7 @@ const Config = Record({
291
291
})
292
292
})
293
293
294
+
// exporting this so modules can register their configuration
294
295
export const tconf = initialize({
295
296
path: path.join(__dirname, '..', 'config),
296
297
schema: Config
@@ -325,6 +326,11 @@ crypto: # <-- module config
325
326
key: 6K0CjNioiXER0qlXRDrOozWgbFZ9LmG/nnOjl0s4NqM=
326
327
```
327
328
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
+
328
334
## Environment Variable Mapping
329
335
330
336
In some situations, it's preferred to specify deployment related configuration based on environment variables.
Copy file name to clipboardExpand all lines: README.md
+43-2Lines changed: 43 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -125,9 +125,9 @@ This is also optional. _tconf_ natively supports [configuration mapping](./DOC.m
125
125
126
126
```typescript
127
127
// src/config.ts
128
-
import loadConfig from 'tconf'
128
+
import { initialize } from 'tconf'
129
129
130
-
export default loadConfig({
130
+
const tconf = initialize({
131
131
// directories containing configuration files
132
132
path: path.join(__dirname, '..', 'config'),
133
133
// the runtypes Config object (optional)
@@ -136,6 +136,7 @@ export default loadConfig({
136
136
// default.yaml, ${NODE_ENV}.yaml, and env.yaml
137
137
sources: ['default', 'NODE_ENV', 'env'],
138
138
})
139
+
export default tconf.get();
139
140
140
141
```
141
142
_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'
149
150
const conn =awaitdbConnect(config.database);
150
151
```
151
152
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
+
exportconst tconf =initialize({ // <-- export the instance
162
+
// ...
163
+
})
164
+
exportdefaulttconf.get();
165
+
```
166
+
167
+
Then in your module, register your configuration schema and provide access to your module.
0 commit comments