import {
Column,
CreateDateColumn,
Entity,
JoinColumn,
ManyToOne,
OneToMany,
PrimaryGeneratedColumn,
} from 'typeorm';
import { CollectionBookmark } from './collection-bookmark.entity';
import { Users } from 'src/user/entities/user.entity';
import { WebContents } from 'src/web-content/entities/webContents.entity';
@Entity({
name: 'collections',
})
export class Collections {
@PrimaryGeneratedColumn({ type: 'int' })
id: number;
@Column({ type: 'varchar', nullable: false })
title: string;
@Column({ type: 'varchar', nullable: false })
desc: string;
@Column({ type: 'int', nullable: true })
bookmarkCount: number;
@CreateDateColumn()
createdAt: Date;
// 관계 설정
// 컬렉션 - 컬렉션 북마크
@OneToMany(
() => CollectionBookmark,
(collectionBookmark) => collectionBookmark.collection,
)
collectionBookmarks: CollectionBookmark[];
@Column('int', { name: 'collection_bookmark_id', nullable: true })
collectionBookmarkId: number;
// 컬렉션 - 유저
@ManyToOne(() => Users, (user) => user.collections)
@JoinColumn([{ name: 'user_id', referencedColumnName: 'id' }])
// 컬렉션 - 웹컨텐츠
@OneToMany(() => WebContents, (webContents) => webContents.collection)
webContent: WebContents[];
@Column('int', { name: 'web_contents_id', nullable: false })
webContentsId: number;
}
collections.entity.ts
import {
Column,
Entity,
JoinColumn,
ManyToOne,
OneToMany,
PrimaryGeneratedColumn,
} from 'typeorm';
import { Collections } from './collections.entity';
import { CollectionBookmarkUser } from './collection-bookmark-user.entity';
@Entity({
name: 'collection_bookmark',
})
export class CollectionBookmark {
@PrimaryGeneratedColumn({ type: 'int' })
id: number;
// 컬렉션 북마크 - 컬렉션
@ManyToOne(() => Collections, (collection) => collection.collectionBookmarks)
@JoinColumn({ name: 'collection_id' })
collection: Collections;
@Column('int', { name: 'collection_id', nullable: false })
collectionId: number;
// 컬렉션 북마크 - 컬렉션 북마크 유저
@OneToMany(
() => CollectionBookmarkUser,
(bookmarkUser) => bookmarkUser.bookmark,
)
bookmarkUsers: CollectionBookmarkUser[];
// // 컬렉션 좋아요 - 유저
// @ManyToOne(() => Users, (users) => users.collections)
// users: Users;
// @Column('int', { name: 'user_id', nullable: false })
// userId: number;
}
collection-bookmark.entity.ts
import { Entity, JoinColumn, ManyToOne, PrimaryGeneratedColumn } from 'typeorm';
import { CollectionBookmark } from './collection-bookmark.entity';
import { Users } from 'src/user/entities/user.entity';
import { Collections } from './collections.entity';
@Entity({
name: 'collection_bookmark_user',
})
export class CollectionBookmarkUser {
@PrimaryGeneratedColumn({ type: 'int' })
id: number;
@ManyToOne(() => Collections, { eager: true })
@JoinColumn({ name: 'collection_id' })
collection: Collections;
@ManyToOne(() => CollectionBookmark, (bookmark) => bookmark.bookmarkUsers)
@JoinColumn({ name: 'bookmark_id' })
bookmark: CollectionBookmark;
@ManyToOne(() => Users, (user) => user.collectionBookmarks)
@JoinColumn({ name: 'user_id' })
user: Users;
}
collection-bookmark-user.entity.ts
collections와 collection-bookmark-user는 각각 다대다 관계이기 때문에
중간에 collection-bookmark 테이블로 관계를 매핑해주었다.
관계가 복잡하다보니 엔티티를 계속 수정하고, 관계 관련 오류가 잦은 점이 힘들었다.
'💻 > 프로젝트' 카테고리의 다른 글
프로젝트 컬렉션 기능 문제 해결 (0) | 2024.04.16 |
---|---|
컬렉션 컨텐츠 추가 기능 구현 (0) | 2024.04.11 |
최종 프로젝트 트러블 슈팅 (0) | 2024.04.04 |
컬렉션 북마크 기능 고민 (0) | 2024.04.03 |
컬렉션 상세 조회 관련 고민 (0) | 2024.04.01 |