Repository Pattern
โ๏ธ Today I Learned
-
์ต๊ทผ NestJS + TypeORM ํ๋ ์์ํฌ๋ก ์๋ฒ๋ฅผ ๊ตฌ์ฑํ๋ฉฐ ๊ณต๋ถํ๊ณ ์๋ค.
-
TypeORM์ด Repository Pattern์ ๊ธฐ๋ณธ์ผ๋ก ์งํฅํ๊ณ ์ง์ํ๊ณ ์์ผ๋ฉฐ NestJS ๊ณต์ ๋ฌธ์์๋ Repository Pattern์ ์ด์ฉํ ์ค๊ณ ๋ฐฉ์์ ์๋ดํ๊ณ ์๋ค.
-
๋ ๋ํ ํ์ฌ ์งํ ์ค์ธ ํ๋ก์ ํธ์ ๊ณต์ ๋ฌธ์๋ฅผ ์ฝ๊ณ Repository Pattern๋ฅผ ์ ์ฉํด์ ๊ฐ๋ฐ์ ์ด์ด๋๊ฐ๊ณ ์๋ค.
-
๋ฌธ๋ Repository Pattern๊ฐ ๋ฌด์์ด๊ณ ์ ์ฐ๋๊ฑฐ์ง? ๊ฐ ๊ถ๊ธํด์ก๊ณ ๊ทธ ๊ณผ์ ์ ๊ธ๋ก ๋จ๊ฒจ๋ณด๋ คํ๋ค.
1. Repository Pattern
-
์ฐ์ Repository Pattern์ ์ฌ๋ฌ ๋์์ธ ํจํด ์ค ํ๋์ด๋ค.
-
๋ด๊ฐ ์ดํดํ(?)๋๋ก ๋งํ์๋ฉด ์ค์ ๋ฐ์ดํฐ๊ฐ ์๋ ์ฅ์๋ฅผ ์ถ์ํํ์ฌ Repository๋ก ๋ ๋ค, ํด๋น ์ฅ์์์ ๋ฐ์ดํฐ๋ฅผ ๊ฐ์ ธ์ค๊ณ ๊ฐ๊ณตํ๋ ์ ๋ฌด๋ฅผ ์ ๋ดํ๋ค.
-
๊ทธ๋ ๊ธฐ์ ํด๋ผ์ด์ธํธ์์๋ ๋ด๊ฐ ์ํ๋ ๋ฐ์ดํฐ์ ์ถ์ฒ(Local, Remote)๋ผ๋์ง, ์๋ณธ ๋ฐ์ดํฐ ์์ฑ๋ฑ์ ์์์ผํ ํ์๊ฐ ์๋ค.
-
๊ทธ์ ํน์ ์์ฒญ์ Repository์ ๋ณด๋ด๋ฉด ๊ฐ๊ณต๋ ์ํ๋ ๋ฐ์ดํฐ ์ป์ ์ ์๊ฒ๋ ๋์์ธ ํจํด์ ์ค๊ณํ๋ ๊ตฌ์กฐ๊ฐ Repository Pattern์ด๋ค.
2. ์ค์ ํ๋ก์ ํธ ์ ์ฉ ์์
-
์ฐ์ NestJS ํ๋ ์์ํฌ ๊ฐ์๋ฅผ ๋ค์ผ๋ฉฐ ์์ฑํ Repository Pattern์ด ์ ์ฉ๋ ์ํ ์ฝ๋๋ฅผ ๊ฐ์ ธ์๋ค.
-
boards.service.ts
@Injectable() export class BoardsService { constructor( @InjectRepository(BoardRepository) private boardRepository: BoardRepository, ) {} /* ์ ๋ฌ์ธ์๋ก CreateBoardDTO๋ฅผ ๊ฐ๋ ๋ณด๋๋ฅผ ์์ฑํ๋ createBoard ๋ฉ์๋ ์ ์ธ */ createBoard(createBoardDto: CreateBoardDto): Promise<Board> { return this.boardRepository.createBoard(createBoardDto); // Repository ํจํด ์ ์ฉ } ...
-
boards.repository.ts
/* Board Entity๋ฅผ ์ปจํธ๋กคํ๋ Repository๋ฅผ ์ ์ธ */ @EntityRepository(Board) export class BoardRepository extends Repository<Board> { /* createBoard ๋ฉ์๋, DB ๊ด๋ จ ๋์์ Repository์์ ์ํํ๋ค */ async createBoard(createBoardDto: CreateBoardDto): Promise<Board> { /* title๊ณผ description์ ๊ตฌ์กฐ๋ถํด ํ ๋น์ผ๋ก createBoardDto์์ ๊บผ๋ด์ด์ ์ฌ์ฉํด์ค๋ค */ const { title, description } = createBoardDto; const board = this.create({ title, description, status: BoardStatus.PUBLIC, // status๊ฐ์ BoardStatus.PUBLIC์ผ๋ก ์ด๊ธฐํํ๋ค }); await this.save(board); // board๋ฅผ ๋ฐ์ดํฐ๋ฒ ์ด์ค์ ์ ์ฅ return board; // DB์ ์ ์ฅํ board๋ฅผ ๋ฐํํ๋ค } }
-
-
๊ฐ๋จํ ์ฝ๋์ง๋ง Repository Pattern์ด ์ ์ฉ๋ ๊ฑธ ๋ณผ ์ ์๋ค.
-
๋ฐ์ดํฐ๋ฅผ ์๋ณธ ์ ์ฅ์์์ ๊บผ๋ด์จ ๋ค ๊ฐ๊ณตํด์ ์ ๊ณตํ๋ ์ฅ์(boards.repository.ts)์ ๊ทธ ๋ฐ์ดํฐ๋ฅผ ์ฌ์ฉํ๋ ์ฅ์(boards.service.ts)๋ฅผ ๋ถ๋ฆฌ์ํจ ๋์์ธ ํจํด์์ ์ ์ ์๋ค.
-
TypeORM์์ ๊ธฐ๋ณธ์ผ๋ก Repository Pattern์ ์ ๊ณตํ๊ธฐ ๋๋ฌธ์ ์์ ๊ฐ์ ๋์์ธ ํจํด์ NestJS + TypeORM ํ๋ ์์ํฌ ์ํ๊ณ์์๋ ๊ฐ๋จํ๊ฒ ๊ตฌํํ ์ ์๋ค๋ ์ฅ์ ์ด ์๋ค.
3. Repository Pattern์ ์ฅ์ ?
-
์ฐ์ ์ด๋ค ๊ฐ๋ ์ด๊ณ , ์ด๋ป๊ฒ ์ฐ์ด๋์ง๋ ์์์ผ๋ ์ฅ์ ์ ๊ฐ๋จํ๊ฒ ์ฐพ์๋ณด์๋ค.
-
๋ฐ์ดํฐ ๋ก์ง์ ๋ถ๋ฆฌ์ํฌ ์ ์๋ค.
โ ํ์ผ ๊ตฌ์กฐ๋ถํฐ ๋ถ๋ฆฌ๊ฐ ๋๋ค.
-
์ค์ ์ง์ค์ฒ๋ฆฌ ๋ฐฉ์์ผ๋ก, ์ธ์ ๋ ์ผ๊ด๋ ์ธํฐํ์ด์ค๋ก ๋ฐ์ดํฐ๋ฅผ ์์ฒญํ ์ ์๋ค.
โ ์์ฒญ์ ํ๋ ์ชฝ์ Repository์ ์ฌ์ ์ด ๊ถ๊ธํ์ง๋ ํ์ํ์ง๋ ์๋ค. ์ธ์ ๋ ํญ์ ๊ฐ์ ์ธํฐํ์ด์ค๋ก ์์ฒญ์ํ๊ณ ๊ฒฐ๊ณผ๊ฐ์ ์ ๊ณต๋ฐ์ ์ ์๋ค.
-
๋จ์ ํ ์คํธ๋ฅผ ํตํด ๊ฒ์ฆ์ด ๊ฐ๋ฅํฉ๋๋ค.
โ ํ ์คํธ ๊ฒฝํ์ ์์ง๋ง, ๋ฐ์ดํฐ๋ฅผ ๊ฐ๊ณตํ๊ณ ์ ๊ณตํ๋ Repository์์๋ง ๊ฒ์ฆ์ ์ถ๊ฐํ๋ค๋ฉด ์๋ต์ผ๋ก ๋ฐํ๋ ๋ฐ์ดํฐ๊ฐ๋ค์ ๋จ์ ํ ์คํธ๋ฅผ ๋จผ์ ์ ์ฉ์์ผ ๊ฒ์ฆ์ด ์์ํ ๊ฑฐ ๊ฐ๋ค๋ผ๋ ์๊ฐ์ ๋ ๋ค.
-
์๋ก์ด ๋ฐ์ดํฐ ๋ก์ง ์ฝ๋๋ฅผ ์ฝ๊ฒ ์ถ๊ฐํ ์ ์์ต๋๋ค.
โ Repository์์๋ง ์ฝ๋๋ฅผ ์ถ๊ฐํ๊ฑฐ๋ ์์ ํ์ฌ ๋ฐ์ดํฐ ๋ก์ง์ ์ถ๊ฐํ๊ฑฐ๋ ๋ณ๊ฒฝํ ์ ์์ผ๋ฏ๋ก ํฐ ์ฅ์ ์ด๋ค.
-
๐ค Understanding
-
๊ทธ๋ฅ NestJS ๊ณต์ ๋ฌธ์์์ ์ฐ๋ผํด์ ์ผ๊ณ , TypeORM์์ ๊ธฐ๋ณธ์ผ๋ก ์ง์ํด์ฃผ๋ ์ฐ๋ Repository Pattern์ ๊ทธ๋๋ง ์กฐ๊ธ ์ดํดํ๊ณ ์จ ๋ณผ ์ ์๊ฒ ๋์๋ค.
-
์์ผ๋ก๋ ๋ฐ์ดํฐ๋ฅผ ๊ฐ๊ณตํ๊ฑฐ๋ ๊ฐ์ ธ์ค๋ ๋ก์ง์ ํ์คํ Repository ์์ญ์์ ์ํํด์ฃผ๋ ์ชฝ์ผ๋ก ์กฐ๊ธ ๋ ์๊ฐํ ๋ค ์ฝ๋๋ก ์ฎ๊ฒจ๊ฐ์ผ๊ฒ ๋ค~ ๋ผ๋ ์๊ฐ์ด ๋ค์๋ค.
-๊ท๋ชจ๊ฐ ํฐ ํ๋ก์ ํธ์ผ์๋ก ๋์์ธ ํจํด๋ฑ ๊ตฌ์กฐ ์ค๊ณ๋ถํฐ ์ค์ํ ๊ฑฐ๊ฐ๋ค๋ผ๋ ์๊ฐ์ด ๋ ๋ค.