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: README.md
+68Lines changed: 68 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -488,6 +488,74 @@ TypedConfigModule.forRoot({
488
488
});
489
489
```
490
490
491
+
## Handling Environment Variable Key and Value Transforms (Best Practice)
492
+
493
+
When using multiple loaders (such as local YAML files for development and environment variables for production), it is common to need to map environment variables like `DATABASE__USER_NAME` to camelCase config properties like `userName`. Here is the recommended approach:
494
+
495
+
1.**Use `keyTransformer` to unify key style**
496
+
497
+
The `keyTransformer` option of `dotenvLoader` allows you to automatically convert environment variable names to camelCase. For example:
With this, `DATABASE__USER_NAME=testuser` will be correctly mapped to the `userName` property.
516
+
517
+
2.**Use `@Type` decorator to ensure value type conversion**
518
+
519
+
Since environment variables are always loaded as strings, it is recommended to use the `@Type` decorator on config class properties to ensure correct type conversion:
> - Adjust the `keyTransformer` function to match your naming conventions as needed.
555
+
> - It is recommended to document this usage in your codebase for team clarity and maintainability.
556
+
557
+
---
558
+
491
559
### Using custom loader
492
560
493
561
If native loaders provided by `nest-typed-config` can't meet your needs, you can implement a custom loader. This can be achieved by providing a function which returns the configuration object synchronously or asynchronously through the `load` option. For example:
0 commit comments