Skip to content

Commit

Permalink
feat : database setting
Browse files Browse the repository at this point in the history
  • Loading branch information
kangjuhyup committed Oct 24, 2024
1 parent e07d950 commit 074ab83
Show file tree
Hide file tree
Showing 6 changed files with 91 additions and 24 deletions.
6 changes: 6 additions & 0 deletions .github/workflows/ci-cd.yml
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,12 @@ jobs:
echo "BACKGROUND_BUCKET=${{ vars.BACKGROUND_BUCKET }}" >> .env
echo "COMPONENT_BUCKET=${{ vars.COMPONENT_BUCKET }}" >> .env
echo "LETTER_BUCKET=${{ vars.LETTER_BUCKET }}" >> .env
echo "DB_TYPE=${{ vars.DB_TYPE }}" >> .env
echo "DB_HOST=${{ secrets.DB_HOST }}" >> .env
echo "DB_NAME=${{ secrets.DB_NAME }}" >> .env
echo "DB_PORT=${{ secrets.DB_PORT}}" >> .env
echo "DB_PWD=${{ secrets.DB_PWD }}" >> .env
echo "DB_USER=${{ secrets.DB_USER }}" >> .env
- name: docker create
run : docker buildx create --use

Expand Down
25 changes: 20 additions & 5 deletions src/database/database.module.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,32 @@
import { Global, Module } from '@nestjs/common';
import { ConfigModule } from '@nestjs/config';
import { ConfigModule, ConfigService } from '@nestjs/config';
import { TypeOrmModule } from '@nestjs/typeorm';
import { ConfigService } from 'aws-sdk';
import { LetterDataSource } from './datasource/letter.datasource';
import { Enviroments } from '@app/domain/dto/env';
import { plainToClass, plainToInstance } from 'class-transformer';
import { validateSync } from 'class-validator';

@Global()
@Module({
imports: [
TypeOrmModule.forRootAsync({
imports: [ConfigModule],
useFactory: async () => {
await LetterDataSource.initialize();
return LetterDataSource.options;
useFactory: async (config:ConfigService) => {

const env = plainToInstance(Enviroments,process.env, {
enableImplicitConversion : true,
});
const ds = LetterDataSource({
type: env.DB_TYPE,
host: env.DB_HOST,
port: env.DB_PORT,
database: env.DB_NAME,
username: env.DB_USER,
password: env.DB_PWD,
synchronize : false,
})
await ds.initialize();
return ds.options;
},
inject: [ConfigService],
}),
Expand Down
19 changes: 10 additions & 9 deletions src/database/datasource/letter.datasource.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
import { DataSource } from 'typeorm';
import 'dotenv/config';

export const LetterDataSource = new DataSource({
type: process.env.DB_TYPE as any,
host: process.env.DB_HOST,
port: Number(process.env.DB_PORT),
username: process.env.DB_USER,
password: process.env.DB_PWD,
database: process.env.DB_NAME,
export const LetterDataSource =(param : {
type : any,
host : string,
port : number,
username : string,
password : string,
database : string,
synchronize : boolean
}) => new DataSource({
...param,
entities: [__dirname + '/../entity/*.{ts,js}'],
migrations: [__dirname + '/../migration/*.{ts,js}'],
synchronize: process.env.DB_SYNC === 'true' || false,
});
1 change: 0 additions & 1 deletion src/database/entity/letter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ export class LetterEntity extends DefaultEntity {
@Column({
name: UserColumn.userId,
type: 'char',
length: 36,
nullable: false,
})
userId: string;
Expand Down
44 changes: 35 additions & 9 deletions src/domain/dto/env.ts
Original file line number Diff line number Diff line change
@@ -1,35 +1,61 @@
import { IsString, IsUrl, IsNotEmpty } from 'class-validator';
import { Transform } from 'class-transformer';
import { IsString, IsUrl, IsNotEmpty, IsNumber } from 'class-validator';

export class Enviroments {
@IsString()
@IsNotEmpty()
@IsString()
WASABI_ACCESS_KEY: string;

@IsString()
@IsNotEmpty()
@IsString()
WASABI_SECRET_KEY: string;

@IsString()
@IsNotEmpty()
@IsString()
WASABI_REGION: string;

@IsUrl()
@IsNotEmpty()
@IsUrl()
WASABI_ENDPOINT: string;

@IsString()
@IsNotEmpty()
@IsString()
THUMB_BUCKET: string;

@IsString()
@IsNotEmpty()
@IsString()
BACKGROUND_BUCKET: string;

@IsString()
@IsNotEmpty()
@IsString()
COMPONENT_BUCKET: string;

@IsString()
@IsNotEmpty()
@IsString()
LETTER_BUCKET: string;

@IsNotEmpty()
@IsString()
DB_TYPE : string;

@IsNotEmpty()
@IsString()
DB_HOST : string;

@Transform(({value}) => Number(value))
@IsNotEmpty()
@IsNumber()
DB_PORT : number;

@IsNotEmpty()
@IsString()
DB_NAME : string;

@IsNotEmpty()
@IsString()
DB_USER : string;

@IsNotEmpty()
@IsString()
DB_PWD : string;
}
20 changes: 20 additions & 0 deletions src/domain/letter/dto/request/add.letter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { LetterCategory, LetterCategoryCode } from "@util/category";
import { Transform } from "class-transformer";
import { IsDefined, IsIn, IsOptional, IsString, MaxLength } from "class-validator";

export class AddLetterRequest {
@IsDefined()
@IsString()
@IsIn(Object.values(LetterCategoryCode))
category : LetterCategoryCode

@IsDefined()
@IsString()
@MaxLength(20)
title : string

@IsOptional()
@IsString()
@MaxLength(255)
body : string;
}

0 comments on commit 074ab83

Please sign in to comment.