import { Module } from '@nestjs/common'; import { BoysModule } from './boys/boys.module'; import { TypeOrmModule, TypeOrmModuleOptions } from '@nestjs/typeorm'; import { ConfigModule, ConfigService } from '@nestjs/config'; import * as dotenv from 'dotenv'; import { ConfigEnum } from './enum/config'; import * as Joi from 'joi';
const envFilePath = `.env.${process.env.NODE_ENV || `development`}`;
@Module({ imports: [ TypeOrmModule.forRootAsync({ imports: [ConfigModule], inject: [ConfigService], useFactory: (ConfigService: ConfigService) => ({
type: ConfigService.get(ConfigEnum.DB_TYPE), host: ConfigService.get(ConfigEnum.DB_HOST), port: ConfigService.get(ConfigEnum.DB_PORT), username: ConfigService.get(ConfigEnum.DB_USERNAME), password: ConfigService.get(ConfigEnum.DB_PASSWORD), database: ConfigService.get(ConfigEnum.DB_DATABASE), retryDelay: 500, retryAttempts: 10,
synchronize: ConfigService.get(ConfigEnum.DB_ASYNC), autoLoadEntities: true, logging: ['error'], } as TypeOrmModuleOptions), }),
ConfigModule.forRoot({ isGlobal: true, envFilePath, load: [() => dotenv.config({ path: '.env' })],
validationSchema: Joi.object({ NODE_ENV: Joi.string() .valid('development', 'production') .default('development'), DB_PORT: Joi.number().default(3306), DB_HOST: Joi.string().ip(), DB_TYPE: Joi.string().valid('mysql', 'mongodb', 'postgres'), DB_DATABASE: Joi.string().required(), DB_USERNAME: Joi.string().required(), DB_PASSWORD: Joi.string().required(), DB_ASYNC: Joi.boolean().default(false), }), }), BoysModule, ], controllers: [], providers: [], }) export class AppModule {}
|