[Nestjs][TIL][개인과제] Nestjs + TypeORM | 공연 예매 사이트 만들기 - DB 설계

Trippy·2023년 12월 28일
1

SQL

목록 보기
2/2

열심히 db 짜고 회원, 공연 crud를 짜다가 막혔다.
아무래도 db 설계를 잘 못해서 개발 과정 중에 막히는 듯 하다.
데이터 구조가 복잡해질수록 테이블 간의 관계 설정을 논리적으로 따져가면서 해야 할 것 같아서
세팅 했었던 DB를 초기화하고 처음부터 다시 짰다.😂

ERD

USER - RESERVATION = 1 : N
USER - POINT = 1 : N
PERFORMANCE - RESERVATION = 1 : N
PERFORMANCE - SCHEDULE = 1 : N
SCHEDULE - RESERVATION = 1 : N
SEAT - RESERVATION = 1 : N
PERFORMANCE - SEAT = 1 : N


USER

// user.entity.ts

import {
  Column,
  CreateDateColumn,
  DeleteDateColumn,
  Entity,
  Index,
  OneToMany,
  PrimaryGeneratedColumn,
  UpdateDateColumn,
} from 'typeorm';
import { Role } from '../types/userRole.type';
import { Point } from 'src/point/entities/point.entity';

@Index('email', ['email'], { unique: true })
@Entity({
  name: 'users',
})
export class User {
  @PrimaryGeneratedColumn({ name: 'userId' })
  userId: number;

  @OneToMany(() => Point, (point) => point.user)
  point: Point[];

  @Column({ type: 'varchar', unique: true })
  email: string;

  @Column({ type: 'varchar' })
  password: string;

  @Column({ type: 'varchar' })
  name: string;

  @Column({ type: 'varchar' })
  phone: string;

  @Column({ type: 'boolean' })
  sex: boolean;

  @Column({ type: 'enum', enum: Role, default: Role.User })
  role: Role;

  @CreateDateColumn()
  createdAt: Date;

  @UpdateDateColumn()
  updatedAt: Date;

  @DeleteDateColumn()
  deletedAt?: Date;
}

POINT

// point.entity.ts

import { User } from 'src/user/entities/user.entity';
import {
  Column,
  CreateDateColumn,
  Entity,
  JoinColumn,
  ManyToOne,
  PrimaryGeneratedColumn,
} from 'typeorm';

@Entity({
  name: 'point',
})
export class Point {
  @PrimaryGeneratedColumn({ type: 'int', name: 'pointId' })
  pointId: number;

  @Column({ type: 'int', name: 'UserId' })
  UserId: number;

  @ManyToOne(() => User, (user) => user.userId, {
    onDelete: 'CASCADE',
  })
  @JoinColumn({ name: 'UserId' })
  user: User;

  @Column()
  income: number;

  @Column()
  expense: number;

  @Column({ type: 'int', name: 'balance', default: 1000000 })
  balance: number;

  @CreateDateColumn()
  createdAt: Date;
}

Reservation

// reservation.entity.ts
import { User } from 'src/user/entities/user.entity';
import {
  Column,
  CreateDateColumn,
  Entity,
  JoinColumn,
  ManyToOne,
  PrimaryGeneratedColumn,
} from 'typeorm';

@Entity({
  name: 'point',
})
export class Point {
  @PrimaryGeneratedColumn({ type: 'int', name: 'pointId' })
  pointId: number;

  @Column({ type: 'int', name: 'UserId' })
  UserId: number;

  @ManyToOne(() => User, (user) => user.userId, {
    onDelete: 'CASCADE',
  })
  @JoinColumn({ name: 'UserId' })
  user: User;

  @Column()
  income: number;

  @Column()
  expense: number;

  @Column({ type: 'int', name: 'balance', default: 1000000 })
  balance: number;

  @CreateDateColumn()
  createdAt: Date;
}

Performance

// performance.entity.ts
import { number } from 'joi';
import { Reservation } from 'src/reservation/entities/reservation.entity';
import { Schedule } from 'src/schedule/entities/schedule.entity';
import { Category } from 'src/user/types/performance.type';
import {
  Column,
  CreateDateColumn,
  DeleteDateColumn,
  Entity,
  OneToMany,
  PrimaryGeneratedColumn,
  UpdateDateColumn,
} from 'typeorm';

@Entity({
  name: 'performance',
})
export class Performance {
  @PrimaryGeneratedColumn({ type: 'int', name: 'performanceId' })
  performanceId: number;

  @Column({ type: 'varchar', name: 'performanceTitle', nullable: false })
  performanceTitle: string;

  @Column({ type: 'enum', enum: Category })
  category: Category;

  @Column({ type: 'int', nullable: false })
  age: number;

  @Column({ type: 'int', nullable: false })
  price: number;

  @Column({ type: 'varchar', nullable: false })
  imageUrl: string;

  @Column({ type: 'int' })
  hours: number;

  @Column({ type: 'varchar', nullable: false })
  place: string;

  @Column({ type: 'varchar' })
  startDate: string;

  @Column({ type: 'varchar' })
  endDate: string;

  @CreateDateColumn()
  createdAt: Date;

  @UpdateDateColumn()
  updatedAt: Date;

  @DeleteDateColumn()
  deletedAt: Date;

  @OneToMany(() => Schedule, (schedule) => schedule.performance)
  schedules: Schedule[];

  @OneToMany(() => Reservation, (reservation) => reservation.performance)
  reservations: Reservation[];
}

Schedule

// schedule.entity.ts

import { number } from 'joi';
import { Reservation } from 'src/reservation/entities/reservation.entity';
import { Schedule } from 'src/schedule/entities/schedule.entity';
import { Category } from 'src/user/types/performance.type';
import {
  Column,
  CreateDateColumn,
  DeleteDateColumn,
  Entity,
  OneToMany,
  PrimaryGeneratedColumn,
  UpdateDateColumn,
} from 'typeorm';

@Entity({
  name: 'performance',
})
export class Performance {
  @PrimaryGeneratedColumn({ type: 'int', name: 'performanceId' })
  performanceId: number;

  @Column({ type: 'varchar', name: 'performanceTitle', nullable: false })
  performanceTitle: string;

  @Column({ type: 'enum', enum: Category })
  category: Category;

  @Column({ type: 'int', nullable: false })
  age: number;

  @Column({ type: 'int', nullable: false })
  price: number;

  @Column({ type: 'varchar', nullable: false })
  imageUrl: string;

  @Column({ type: 'int' })
  hours: number;

  @Column({ type: 'varchar', nullable: false })
  place: string;

  @Column({ type: 'varchar' })
  startDate: string;

  @Column({ type: 'varchar' })
  endDate: string;

  @CreateDateColumn()
  createdAt: Date;

  @UpdateDateColumn()
  updatedAt: Date;

  @DeleteDateColumn()
  deletedAt: Date;

  @OneToMany(() => Schedule, (schedule) => schedule.performance)
  schedules: Schedule[];

  @OneToMany(() => Reservation, (reservation) => reservation.performance)
  reservations: Reservation[];
}
profile
감금 당하고 개발만 하고 싶어요

0개의 댓글