Skip to content

Commit

Permalink
chore: typeOrmConfig => config 로직 따라가기, multer 지금 안쓰니까 주석
Browse files Browse the repository at this point in the history
  • Loading branch information
tomatoziyun committed Nov 11, 2023
1 parent 2bfa619 commit b65d49c
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 52 deletions.
26 changes: 21 additions & 5 deletions src/app.module.ts
Original file line number Diff line number Diff line change
@@ -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<typeof typeOrmConfig>,
): 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,
Expand Down
60 changes: 30 additions & 30 deletions src/config/multer.config.ts
Original file line number Diff line number Diff line change
@@ -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) {}
// };
// },
// };
40 changes: 23 additions & 17 deletions src/config/typeorm.config.ts
Original file line number Diff line number Diff line change
@@ -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<string>('POSTGRES_HOST'),
port: configService.get<number>('POSTGRES_PORT'),
username: configService.get<string>('POSTGRES_USER'),
password: configService.get<string>('POSTGRES_PASSWORD'),
database: configService.get<string>('POSTGRES_DB'),
entities: [__dirname + '/../**/entities/*.entity.{js,ts}'],
synchronize: true,
};
},
};
return {
TYPE,
POSTGRES_HOST,
POSTGRES_PORT,
POSTGRES_USER,
POSTGRES_PASSWORD,
POSTGRES_DB,
ENTITIES,
SYNCHRONIZE,
};
});

0 comments on commit b65d49c

Please sign in to comment.