TypeORM & Entity 구현 - 1 : N, N : M

SSAD·2023년 2월 27일
0

BackEnd

목록 보기
40/44
post-thumbnail

1:N (상품: 상품카테고리)

productCategory.entity.ts

import { Entity, Column, PrimaryGeneratedColumn } from 'typeorm';

@Entity()
export class ProductCategory {
  @PrimaryGeneratedColumn('uuid')
  id: string;

  @Column()
  name: string;
}

product.entity.ts

import { ProductCategory } from 'src/apis/productsCategory/entities/productCategory.entity';
import { ProductSaleslocation } from 'src/apis/productsSaleslocation/entities/productSaleslocation.entity';
import { User } from 'src/apis/users/entities/user.entity';
import {
  Column,
  CreateDateColumn,
  Entity,
  JoinColumn,
  JoinTable,
  ManyToMany,
  ManyToOne,
  OneToOne,
  PrimaryGeneratedColumn,
} from 'typeorm';

@Entity()
export class Product {
  // 고유 아이디
  @PrimaryGeneratedColumn('uuid')
  id: string;

  // 상품 제목
  @Column()
  title: string;

  // 상품 설명
  @Column({ type: `text` })
  description: string;

  // 상품 가격
  @Column()
  price: number;

  // 가격 제안 여부
  @Column()
  proposition: boolean;

  // 관심수
  @Column()
  likeCount: number;

  // 조회수
  @Column()
  viewCount: number;

  // 나눔 여부
  @Column()
  sharing: boolean;

  // 생성 일자
  @CreateDateColumn({ type: 'timestamp' })
  createdAt: Date;

  // 삭제 일자
  @Column()
  deletedAt: Date;

  // 업데이트 일자
  @Column()
  updatedAt: Date;

  // 끌올 일자
  @Column()
  refreshedAt: Date;

  // 상품판매 false 면 판매중 : true면 거래예약잡힘
  @Column({ default: false })
  status: boolean;

  // 상품 판매 완료시간
  @Column()
  soldedAt: Date;

  @Column({ default: false })
  // 상품 판매 완료시 True
  isSoldout: boolean;

  // 판매자 id
  @ManyToOne(() => User)
  seller: User;

  // 구매자 id
  @ManyToOne(() => User)
  buyer: User;

  // 카테고리 id
  @ManyToOne(() => ProductCategory)
  productCategory: ProductCategory;

  // 상품 거래 위치 id  - 1대 1 관계
  @JoinColumn()
  @OneToOne(() => ProductSaleslocation)
  productSaleslocation: ProductSaleslocation;
}

@ManyToOne()

  • N:1 관계를 나타내는 데코레이터

@JoinColuimn()

  • Many 부분에 해당하는 테이블(product)에서는 JoinColumn()이 생략 가능
  • @ManyToOne() : @JoinColumn() 생략 가능
  • @OneToOne(): @JoinColumn() 반드시 필요

N:1 (상품: 유저)


product.entity.ts

import { ProductCategory } from 'src/apis/productsCategory/entities/productCategory.entity';
import { ProductSaleslocation } from 'src/apis/productsSaleslocation/entities/productSaleslocation.entity';
import { ProductTag } from 'src/apis/productTags/entities/productTag.entity';
import { User } from 'src/apis/users/entities/user.entity';
import {
  Column,
  CreateDateColumn,
  Entity,
  JoinColumn,
  JoinTable,
  ManyToMany,
  ManyToOne,
  OneToOne,
  PrimaryGeneratedColumn,
} from 'typeorm';

@Entity()
export class Product {
  // 고유 아이디
  @PrimaryGeneratedColumn('uuid')
  id: string;

  // 상품 제목
  @Column()
  title: string;

  // 상품 설명
  @Column({ type: `text` })
  description: string;

  // 상품 가격
  @Column()
  price: number;

  // 가격 제안 여부
  @Column()
  proposition: boolean;

  // 관심수
  @Column()
  likeCount: number;

  // 조회수
  @Column()
  viewCount: number;

  // 나눔 여부
  @Column()
  sharing: boolean;

  // 생성 일자
  @CreateDateColumn({ type: 'timestamp' })
  createdAt: Date;

  // 삭제 일자
  @Column()
  deletedAt: Date;

  // 업데이트 일자
  @Column()
  updatedAt: Date;

  // 끌올 일자
  @Column()
  refreshedAt: Date;

  // 상품판매 false 면 판매중 : true면 거래예약잡힘
  @Column({ default: false })
  status: boolean;

  // 상품 판매 완료시간
  @Column()
  soldedAt: Date;

  @Column({ default: false })
  // 상품 판매 완료시 True
  isSoldout: boolean;

  // 판매자 id
  @ManyToOne(() => User)
  seller: User;

  // 구매자 id
  @ManyToOne(() => User)
  buyer: User;

  // 카테고리 id
  @ManyToOne(() => ProductCategory)
  productCategory: ProductCategory;

  // 상품 거래 위치 id  - 1대 1 관계
  @JoinColumn()
  @OneToOne(() => ProductSaleslocation)
  productSaleslocation: ProductSaleslocation;

  // N: M 관계(상품: 상품태그)
  @JoinTable()
  @ManyToMany(() => ProductTag, (productTags) => productTags.products)
  productTags: ProductTag[];
}

N:M (상품: 상품태그)

productTag.entity.ts

import { Product } from 'src/apis/products/entities/product.entity';
import { Column, Entity, ManyToMany, PrimaryGeneratedColumn } from 'typeorm';

@Entity()
export class ProductTag {
  @PrimaryGeneratedColumn('uuid')
  id: string;

  @Column()
  name: string;

  @ManyToMany(() => Product, (products) => products.productTags)
  products: Product[];
}

@ManyToMany()

  • N:M 관계를 가질 때는 두 테이블 모두 컬럼을 추가하여 연결해 주어야 함

(products) => products.productTags

  • products 입장에서의 productTags와의 관계를 명시해 준것
  • N:M 관계에서는 두 테이블 모두 관계를 나타내 주어야 함

product[]

  • 하나의 태그에 상품이 여러 개 해당 될 수 있기에 배열로 나타내는 것

product.entity.ts

import { ProductCategory } from 'src/apis/productsCategory/entities/productCategory.entity';
import { ProductSaleslocation } from 'src/apis/productsSaleslocation/entities/productSaleslocation.entity';
import { ProductTag } from 'src/apis/productTags/entities/productTag.entity';
import { User } from 'src/apis/users/entities/user.entity';
import {
  Column,
  CreateDateColumn,
  Entity,
  JoinColumn,
  JoinTable,
  ManyToMany,
  ManyToOne,
  OneToOne,
  PrimaryGeneratedColumn,
} from 'typeorm';

@Entity()
export class Product {
  // 고유 아이디
  @PrimaryGeneratedColumn('uuid')
  id: string;

  // 상품 제목
  @Column()
  title: string;

  // 상품 설명
  @Column({ type: `text` })
  description: string;

  // 상품 가격
  @Column()
  price: number;

  // 가격 제안 여부
  @Column()
  proposition: boolean;

  // 관심수
  @Column()
  likeCount: number;

  // 조회수
  @Column()
  viewCount: number;

  // 나눔 여부
  @Column()
  sharing: boolean;

  // 생성 일자
  @CreateDateColumn({ type: 'timestamp' })
  createdAt: Date;

  // 삭제 일자
  @Column()
  deletedAt: Date;

  // 업데이트 일자
  @Column()
  updatedAt: Date;

  // 끌올 일자
  @Column()
  refreshedAt: Date;

  // 상품판매 false 면 판매중 : true면 거래예약잡힘
  @Column({ default: false })
  status: boolean;

  // 상품 판매 완료시간
  @Column()
  soldedAt: Date;

  @Column({ default: false })
  // 상품 판매 완료시 True
  isSoldout: boolean;

  // 판매자 id
  @ManyToOne(() => User)
  seller: User;

  // 구매자 id
  @ManyToOne(() => User)
  buyer: User;

  // 카테고리 id N대 1관계 
  @ManyToOne(() => ProductCategory)
  productCategory: ProductCategory;

  // 상품 거래 위치 id  - 1대 1 관계
  @JoinColumn()
  @OneToOne(() => ProductSaleslocation)
  productSaleslocation: ProductSaleslocation;

  // N: M 관계(상품: 상품태그)
  @JoinTable()
  @ManyToMany(() => ProductTag, (productTags) => productTags.products)
  productTags: ProductTag[];
}
profile
learn !

0개의 댓글