@Injectable()
export class BoardRepository extends Repository<HotDeal> {
constructor(private readonly dataSource: DataSource) {
super(BoardDeals, dataSource.createEntityManager()); // ❌ BoardDeals 테이블도 사용하려고 함
}
}
Nest.js에서 Repositroy를 만들어서 DB를 컨트롤할 경우, 위처럼 class에 Repository를 상속받는다.
import { DataSource, Repository } from "typeorm";
Repsotiory<T> 로 메서드를 상속받는데, 이는 기본 Repository와 테이블 모델을 결합한것이기 때문에, <T> 테이블에 관련한 데이터 관리만 가능하다.
@Injectable()
export class BoardService {
constructor(
private readonly boardRepository: BoardRepository
) {}
async createDeal(deal: CreateBoardDto): Promise<void> {
const board = new BoardDeals();
board.creater = deal.creater;
board.title = deal.title;
board.link = deal.link;
board.price = deal.price;
board.createAt = getCurrentFormattedDate();
await this.boardRepository.save(board); // ❌ HotDeal 테이블 전용이므로 BoardDeals 저장 불가
}
}
테이블은 <HotDeal> 을 상속받았지만, 컨트롤 하려던건 새로 만든 <BoardDeal> 테이블이었다. API가 정상적으로 콜되고 200이 떨어졌음에도 불구하고 DB에 저장되지 않았던 이유가 이 Repository에서 바라보는 테이블이 다른 테이블이었기 떄문이다.
@Injectable()
export class BoardRepository extends Repository<HotDeal> {
constructor(private readonly dataSource: DataSource) {
super(HotDeal, dataSource.createEntityManager());
}
}
@Injectable()
export class BoardApartRepository extends Repository<BoardDeals> {
constructor(private readonly dataSource: DataSource) {
super(BoardDeals, dataSource.createEntityManager());
}
}
해당 오류를 수정하기 위해 각기 다른 Repository를 작성해 각기 다른 <T>를 상속받았다.
@Injectable()
export class BoardService {
constructor(
private readonly boardRepository: BoardRepository, // HotDeal 전용
private readonly boardApartRepository: BoardApartRepository // BoardDeals 전용
) {}
async createDeal(deal: CreateBoardDto): Promise<void> {
const board = new BoardDeals();
board.creater = deal.creater;
board.title = deal.title;
board.link = deal.link;
board.price = deal.price;
board.createAt = getCurrentFormattedDate();
await this.boardApartRepository.save(board); // ✅ 올바른 Repository에서 저장
console.log(`✅ 저장 완료: ${deal.title}`);
}
async getAllDeals(): Promise<any> {
const hotDeals = await this.boardRepository.find();
const boardDeals = await this.boardApartRepository.find();
return { hotDeals, boardDeals };
}
}
그 후 Service 단에서 Repository를 두가지를 참조하여 각기 사용하면 된다.
@Injectable()
export class BoardRepository {
constructor(private readonly dataSource: DataSource) {}
async findHotDealAndBoardDeals(): Promise<any> {
return await this.dataSource.query(`
SELECT * FROM hot_deals hd
JOIN board_deals bd ON hd.link = bd.link
`);
}
}
Typeorm의 Repostiory를 사용하지 않는다면 위처럼 기본적인 dataSource 를 사용하여 직접 쿼리질의를 진행할 수 있다.
원래 이용하던 방법도 실제로 프로시저 쿼리문을 작성하여 박아넣었기 때문에, 위 방식이 조금 더 익숙했지만
예전 springboot 프로젝트중 진행하던 JPA를 사용하던 생각도 나서, Typeorm을 이용하여 만들기로 했다.
아직 둘중 어떤 방식이 어떠한 이점을 가지고있는지는 알지 못하는 단계이지만,
기술은 사용할 줄 아는 부분을 늘리는 것이 좋을 것 같다.
기본 구조를 알았다면 헤메이지 않았을 방식이지만, 이 문제 때문에 하루를 꼬박 사용했다...ㅋㅋㅋ
'사이드 프로젝트' 카테고리의 다른 글
Nest.js + React.js (Next.js) 사이드 프로젝트 : 페이징 구현하기 (2) (0) | 2025.03.19 |
---|---|
perplexity 퍼플렉시티 api 사용법, postman 호출해보기 (2) | 2025.03.18 |
Nest.js + React.js (Next.js) 사이드 프로젝트 : 페이징 구현하기 (1) (0) | 2025.03.12 |
nest.js 를 이용해 크롤링 해보기 (1) - puppeteer 동적 크롤링 (0) | 2025.03.07 |
Nest.js + React.js(Next.js) 사이드 프로젝트 : TypeORM save(), insert(), update() (0) | 2025.03.07 |