Entity

Database Mapper vs Active Record


Entity


Entity is a class that maps to a database table. (typeORM)

@ObjectType()

@Entity()

restaurants.entity.ts

import { Field, ObjectType } from '@nestjs/graphql';
import { Column, Entity, PrimaryGeneratedColumn } from 'typeorm';

@ObjectType() // GraphQL decorator : 자동으로 스키마를 빌드하기 위해 사용
@Entity() // TypeORM decorator : DB에 해당 클래스를 테이블과 매핑하여 저장
export class Restaurant {
  @PrimaryGeneratedColumn()
  @Field((type) => Number)
  id: number;

  // @Field() : 역시 타입을 리턴하는 함수를 1번째 인자로 갖는다.
  @Field((type) => String) // for GraphQL
  @Column() // for TypeOrm
  name: string;

  @Field((type) => Boolean, { nullable: true })
  @Column()
  isVegan?: boolean;

  @Field((type) => String)
  @Column()
  address: string;

  @Field((type) => String)
  @Column()
  ownerName: string;
}