diginalog2019 / diginalog

Diginalog 2019
https://youtu.be/8lCq9-KaQJI
0 stars 0 forks source link

[Code 변경] DB에서 product 목록 가져오는 방법 #13

Closed kjh107704 closed 5 years ago

kjh107704 commented 5 years ago

기존에 product table에서 creator랑 category등 jointable의 정보가 한꺼번에 검색이 안돼서

let product = await getConnection().getRepository(Product).findOne(options);
const category = await getConnection().getRepository(Category)
                        .findOne({where: {Cate_ID: product.categoryCateID}});        
const creator = await getConnection().getRepository(Creator)
                        .findOne({where: {CID: product.creatorCID}});    
product = {...product, category, creator};

이렇게 처리했던 부분을

let products = await getConnection().getRepository(Product)
.createQueryBuilder("product")
.where("product.State = :state", {state: 1})
.leftJoinAndSelect("product.creator","creator")
.leftJoinAndSelect("product.category","category")
.leftJoinAndSelect("product.fils","file")
.skip(start_index).take(page_size)
.getMany();

이렇게 await 하나로 바꿀 수 있어요