Performance: Configuration namespaces
This affects the startup time and memory usage of the application by organizing configuration loading and reducing unnecessary config parsing.
ConfigModule.forRoot({ isGlobal: true, load: [() => ({ namespaceA: configA(), namespaceB: configB() })] })
// Then inject only needed namespaces in modulesConfigModule.forRoot({ isGlobal: true })| Pattern | Config Loading | Memory Usage | Startup Time | Verdict |
|---|---|---|---|---|
| Global eager config loading | Loads all config at once | High memory usage | Longer startup | [X] Bad |
| Scoped config namespaces | Loads config per namespace | Lower memory usage | Faster startup | [OK] Good |
registerAs in NestJS configuration?registerAsregisterAs is used to define a configuration namespace, grouping related settings under one name.
It is not used for middleware, modules, or database connections, which are handled differently.
registerAs?registerAs with a name and a function returning an object.export const appConfig = registerAs('app', () => ({ port: 3000, env: 'dev' }));configService.get('app.port') return?configService.get('app.port') accesses the 'port' key inside the 'app' namespace.port: 3000, so the call returns 3000.export const apiConfig = registerAs('api', () => ({ url: 'https://api.example.com' }));
// Later in service
const url = configService.get('api.url');url is undefined?export const dbConfig = registerAs('database', () => ({ host: 'localhost', port: 5432 }));
export const authConfig = registerAs('auth', () => ({ jwtSecret: 'secret' }));ConfigService and ensure type safety?'auth.jwtSecret' to specify the namespace and key.<string> to get to ensure the returned value is typed as string.Pass the test and this pattern moves unknown