nestjs-boilerplate icon indicating copy to clipboard operation
nestjs-boilerplate copied to clipboard

Switch to `nest-typed-config` for configuration

Open Nikaple opened this issue 2 years ago • 0 comments

Nest-typed-config is a module which enables defining config schema like request DTOs, and it supports reading configuration from environment variables, local files or remote configuration center.

In this boilerplate, three types of configurations are included: App, JWT and DB, which can be defined in a local toml file for better organization:

# .env.toml
[App]
env = 'dev'
url = 'http://localhost'

[JWT]
secretKey = 'uAsBw6WxqD'
expirationTime = 3600

[DB]
type = 'mariadb'
username = 'nest'
password = 'nest'
host = 'db'
port = 3306
database = 'nest'
sync = false

More importantly, nest-typed-config provides type-safe configurations, for example in app.module.ts:

TypeOrmModule.forRootAsync({
    // DBConfig is defined in config schema, and automatically injected
    useFactory: (dbConfig: DBConfig) => {
        return {
            ...dbConfig,
            entities: [__dirname + './../**/**.entity{.ts,.js}'],
            synchronize: dbConfig.sync,
        }; // No type casting is required
    },
})

Please let me know your thoughts, and I can open a pull request if this sounds good.

Nikaple avatar Oct 11 '21 04:10 Nikaple