From b65d49cd378cd18a4a599da5758e84e08b73032d Mon Sep 17 00:00:00 2001 From: jiyun Date: Sat, 11 Nov 2023 15:09:51 +0900 Subject: [PATCH] =?UTF-8?q?chore:=20typeOrmConfig=20=3D>=20config=20?= =?UTF-8?q?=EB=A1=9C=EC=A7=81=20=EB=94=B0=EB=9D=BC=EA=B0=80=EA=B8=B0,=20mu?= =?UTF-8?q?lter=20=EC=A7=80=EA=B8=88=20=EC=95=88=EC=93=B0=EB=8B=88?= =?UTF-8?q?=EA=B9=8C=20=EC=A3=BC=EC=84=9D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/app.module.ts | 26 +++++++++++++--- src/config/multer.config.ts | 60 ++++++++++++++++++------------------ src/config/typeorm.config.ts | 40 ++++++++++++++---------- 3 files changed, 74 insertions(+), 52 deletions(-) diff --git a/src/app.module.ts b/src/app.module.ts index 5c6228a..5c702a5 100644 --- a/src/app.module.ts +++ b/src/app.module.ts @@ -1,23 +1,39 @@ import { Module } from '@nestjs/common'; -import { ConfigModule } from '@nestjs/config'; -import { TypeOrmModule } from '@nestjs/typeorm'; +import { ConfigModule, ConfigType } from '@nestjs/config'; +import { TypeOrmModule, TypeOrmModuleOptions } from '@nestjs/typeorm'; import { AppController } from './app.controller'; import { AppService } from './app.service'; import { AuthModule } from './auth/auth.module'; import ftConfig from './config/ft.config'; import jwtConfig from './config/jwt.config'; import pgadminConfig from './config/pgadmin.config'; -import { typeOrmConfig } from './config/typeorm.config'; +import typeOrmConfig from './config/typeorm.config'; import userConfig from './config/user.config'; import { UsersModule } from './users/users.module'; @Module({ imports: [ - TypeOrmModule.forRootAsync(typeOrmConfig), ConfigModule.forRoot({ isGlobal: true, envFilePath: './BE-config/.env', - load: [ftConfig, userConfig, pgadminConfig, jwtConfig], + load: [ftConfig, userConfig, pgadminConfig, jwtConfig, typeOrmConfig], + }), + TypeOrmModule.forRootAsync({ + inject: [typeOrmConfig.KEY], + useFactory: ( + typeOrmConfigure: ConfigType, + ): TypeOrmModuleOptions => { + return { + type: typeOrmConfigure.TYPE, + host: typeOrmConfigure.POSTGRES_HOST, + port: +typeOrmConfigure.POSTGRES_PORT, + username: typeOrmConfigure.POSTGRES_USER, + password: typeOrmConfigure.POSTGRES_PASSWORD, + database: typeOrmConfigure.POSTGRES_DB, + entities: typeOrmConfigure.ENTITIES, + synchronize: typeOrmConfigure.SYNCHRONIZE, + }; + }, }), AuthModule, UsersModule, diff --git a/src/config/multer.config.ts b/src/config/multer.config.ts index cf52342..0d1fb98 100644 --- a/src/config/multer.config.ts +++ b/src/config/multer.config.ts @@ -1,30 +1,30 @@ -import { ConfigService } from '@nestjs/config'; -import { MulterModuleAsyncOptions } from '@nestjs/platform-express'; -import { diskStorage } from 'multer'; -import { existsSync, mkdirSync } from 'fs'; -import { extname } from 'path'; - -export const multerConfig: MulterModuleAsyncOptions = { - inject: [ConfigService], - useFactory: async (configService: ConfigService) => { - return { - storage: diskStorage({ - destination: (req, file, callback) => { - const uploadPath = '/app/dist/uploads'; - if (!existsSync(uploadPath)) { - // uploads 폴더가 존재하지 않을시, 생성합니다. - mkdirSync(uploadPath); - } - callback(null, uploadPath); - }, - filename: (req, file, callback) => { - const userAccessToken = 'test'; - const extension = extname(file.originalname); - callback(null, `${userAccessToken}-${file.fieldname}${extension}`); - }, - }), - // limits: {}, - // fileFilter(req, file, callback) {} - }; - }, -}; +// import { ConfigService } from '@nestjs/config'; +// import { MulterModuleAsyncOptions } from '@nestjs/platform-express'; +// import { diskStorage } from 'multer'; +// import { existsSync, mkdirSync } from 'fs'; +// import { extname } from 'path'; +// +// export const multerConfig: MulterModuleAsyncOptions = { +// inject: [ConfigService], +// useFactory: async (configService: ConfigService) => { +// return { +// storage: diskStorage({ +// destination: (req, file, callback) => { +// const uploadPath = '/app/dist/uploads'; +// if (!existsSync(uploadPath)) { +// // uploads 폴더가 존재하지 않을시, 생성합니다. +// mkdirSync(uploadPath); +// } +// callback(null, uploadPath); +// }, +// filename: (req, file, callback) => { +// const userAccessToken = 'test'; +// const extension = extname(file.originalname); +// callback(null, `${userAccessToken}-${file.fieldname}${extension}`); +// }, +// }), +// // limits: {}, +// // fileFilter(req, file, callback) {} +// }; +// }, +// }; diff --git a/src/config/typeorm.config.ts b/src/config/typeorm.config.ts index a0f070f..a8311f7 100644 --- a/src/config/typeorm.config.ts +++ b/src/config/typeorm.config.ts @@ -1,18 +1,24 @@ -import { ConfigService } from '@nestjs/config'; -import { TypeOrmModuleAsyncOptions } from '@nestjs/typeorm'; +import { registerAs } from '@nestjs/config'; +import { z } from 'zod'; +import * as process from 'process'; +export default registerAs('typeorm', () => { + const TYPE = 'postgres' as 'postgres'; + const POSTGRES_HOST = z.string().parse(process.env.POSTGRES_HOST); + const POSTGRES_PORT = z.string().parse(process.env.POSTGRES_PORT); + const POSTGRES_USER = z.string().parse(process.env.POSTGRES_USER); + const POSTGRES_PASSWORD = z.string().parse(process.env.POSTGRES_PASSWORD); + const POSTGRES_DB = z.string().parse(process.env.POSTGRES_DB); + const ENTITIES = [__dirname + '/../**/entities/*.entity.{js,ts}']; + const SYNCHRONIZE: boolean = true; -export const typeOrmConfig: TypeOrmModuleAsyncOptions = { - inject: [ConfigService], - useFactory: async (configService: ConfigService) => { - return { - type: 'postgres', - host: configService.get('POSTGRES_HOST'), - port: configService.get('POSTGRES_PORT'), - username: configService.get('POSTGRES_USER'), - password: configService.get('POSTGRES_PASSWORD'), - database: configService.get('POSTGRES_DB'), - entities: [__dirname + '/../**/entities/*.entity.{js,ts}'], - synchronize: true, - }; - }, -}; + return { + TYPE, + POSTGRES_HOST, + POSTGRES_PORT, + POSTGRES_USER, + POSTGRES_PASSWORD, + POSTGRES_DB, + ENTITIES, + SYNCHRONIZE, + }; +});