TypeORM - Join

김세겸·2023년 1월 3일
0

code-camp

목록 보기
10/10

1. OneToOne

dto

상품 등록할 때 위치정보 dto를 만들어 줘야함 => graphql (objectType, inputType 따로 만들어 줘야 함)

inputType만들 때 objectType를 상속 받는다.(omitType, pickType)

ex) class ... extends OmitType(..., ["", ""...], InputType) {}

input

createProductInput에 넣어준다.

service

constructor(
        @InjectRepository(Product)
        private readonly productRepository: Repository<Product>
        @InjectRepository(productSaleslocaion)
        private readonly productSaleslocaionRepository: Repository<productSaleslocaion>
    ){}
const { productSaleslocation, ...product } = createProductInput;

// 상품 주소 저장
const locationInfo = await this.productSaleslocationRepository.save({ ...productSaleslocation });

// 상품 저장
const result = await this.productRepository.save({
  ...product,
  locationInfo // 전체를 프론트로 보내 줄 수 있다. 필요한 경우 id만 등록 해도 가능
});

return result;

2. 조회

await this.productRepository.find({ relation: ["productSaleslocation"], });

3.ManyToOne

dto

service

const { productSaleslocation, productCategoryId ...product, } = createProductInput;

const result = await this.productRepository.save({
  ...product,
  productCategoryId,
  locationInfo
});

0개의 댓글