node.js

계층형 아키텍처로 프로젝트 작성하기 (02 프리즈마 설계)

늘곰's 2023. 9. 16. 20:54

몰랏던 사실 : 프리즈마에서 @map 은 굉장히 특수한 상황에서 쓰는 것이고 지금처럼 이렇게 남발하면 안된다는걸 깨달음.. 강의에서 나온다고 뭔지 모르고 그냥 썻는데 이름과 같은 내용을 @map 으로 굳이 작성할 필요가 없는데
내용을 중복해서 작성하고 있었음

공식문서를 너무 등한시 했는데 앞으로 공식문서를 참고하는 습관을 가져야 겠다.

 

또한 암묵적으로 id 는 다른것으로 바꾸기 않고 그냥 id 로 쓴다는 것또한 이제 알았음

그동안 강의에서 나온대로 user 테이블은 user id 이런식으로 썻는데 이러면 나중에 user id를 가지고올때 중복해서 쓰게 된다는걸 알았음 

 

 

// This is your Prisma schema file,
// learn more about it in the docs: https://pris.ly/d/prisma-schema

generator client {
  provider = "prisma-client-js"
}

datasource db {
  provider = "mysql"
  url      = env("DATABASE_URL")
}


//회원가입 테이블 정의
model Users {
  userId          Int   @id @default(autoincrement()) @map("userId")
  name        String    @unique @map("name")
  email       String    @unique @map("email")
  password    String    @map("password")
  avatarUrl   String    @map("avatarUrl")
  description String    @map("description")
  githubUrl   String    @map("githubUrl")
  linkedinUrl String    @map("linkedinUrl")
  createdAt   DateTime  @default(now()) @map("createdAt")
  //updatedAt   DateTime  @updatedAt @map("updatedAt")


  //관계 설정
  Projects      Projects[]        
  ViewsLog      ViewsLog[]
  Likes         Likes []         
  Bookmarks     Bookmarks [] 

@@map("Users")

}


// 프로젝트 테이블 정의

model Projects {
  projectId   Int       @id @default(autoincrement()) @map("projectId")
  title       String    @map("title")
  description String    @map("description")    //@db.Text
  image       String    @map("image")
  liveSiteUrl String    @map("liveSiteUrl")
  githubUrl   String    @map("githubUrl")
  category    String    @map("category")
  createdAt   DateTime  @default(now()) @map("createdAt")
  //updatedAt DateTime  @updatedAt @map("updatedAt")

  //외래키
  //createdBy   Int       @map("createdBy")
  UserId        Int       @map("userId")


  // 관계 설정
  Users Users @relation(fields: [UserId], references: [userId],onDelete: Cascade)
  ViewsLog ViewsLog[]
  Likes   Likes []
  Bookmarks Bookmarks []   
  
  @@map("Projects")
}


//뷰로그 테이블 정의
model ViewsLog {
  
  viewsLogId         Int       @id @default(autoincrement()) @map("viewsLogId")
  viewedAt    DateTime  @default(now()) @map("createdAt")

  // 외래키
  ProjectId  Int       @map("projectsId")
  UserId        Int       @map("userId")
 
 
 // 관계 설정
  Projects    Projects  @relation(fields: [ProjectId], references: [projectId], onDelete: Cascade)
  Users       Users     @relation(fields: [UserId], references: [userId],onDelete: Cascade)

 @@map("ViewsLog")
}



//좋아요 테이블 정의
model Likes {
  likeId        Int       @id @default(autoincrement()) @map("likeId")
  createdAt     DateTime  @default(now()) @map("createdAt")

  // 외래 키
  ProjectId    Int       @map("projectId")
  UserId        Int       @map("userId")

  // 관계 설정
  Users         Users     @relation(fields: [UserId], references: [userId], onDelete: Cascade)
  Projects      Projects  @relation(fields: [ProjectId], references: [projectId], onDelete: Cascade)

  @@map("Likes")
}




//북마크 테이블 정의 
model Bookmarks {
  bookmarksId   Int       @id @default(autoincrement()) @map("bookmarksId")
  createdAt     DateTime  @default(now()) @map("createdAt")

  // 외래 키
  ProjectId    Int       @map("projectId")
  UserId        Int       @map("userId")

  // 관계 설정
  Users         Users     @relation(fields: [UserId], references: [userId], onDelete: Cascade)
  Projects      Projects  @relation(fields: [ProjectId], references: [projectId], onDelete: Cascade)

  @@map("Bookmarks")
}

 

수정된 코드 

 

확실히 맵이 덕지덕지 붙을때보다 수정하니 훨씬 코드가 깔끔해보인다.

// This is your Prisma schema file,
// learn more about it in the docs: https://pris.ly/d/prisma-schema
// cuid 쓰기 uuid  대체
generator client {
  provider = "prisma-client-js"
}

datasource db {
  provider = "mysql"
  url      = env("DATABASE_URL")
}


//회원가입 테이블 정의
model Users {
  id          Int   @id @default(autoincrement())
  name        String    @unique 
  email       String    @unique 
  password    String    
  avatarUrl   String    
  description String    
  githubUrl   String    
  linkedinUrl String    
  createdAt   DateTime  @default(now()) 
  //updatedAt   DateTime  @updatedAt @map("updatedAt")


  //관계 설정
  projects      Projects[]        
  viewLogs      ViewLogs[]
  likes         Likes []         
  bookmarks     Bookmarks [] 


}


// 프로젝트 테이블 정의

model Projects {
  id   Int       @id @default(autoincrement())
  title       String    
  description String   
  image       String    
  liveSiteUrl String    
  githubUrl   String    
  category    String    
  createdAt   DateTime  @default(now()) 
  //updatedAt DateTime  @updatedAt @map("updatedAt")

  //외래키
  userId        Int       


  // 관계 설정
  users Users @relation(fields: [userId], references: [id],onDelete: Cascade)
  
  viewsLogs ViewLogs[]
  likes     Likes []
  bookmarks Bookmarks []   
  

}


//뷰로그 테이블 정의
model ViewLogs {
  
  id          Int       @id @default(autoincrement())
  viewedAt    DateTime  @default(now()) 

  // 외래키
  projectId     Int       
  userId        Int       
 
 
 // 관계 설정
  projects    Projects  @relation(fields: [projectId], references: [id], onDelete: Cascade)
  users       Users     @relation(fields: [userId], references: [id],onDelete: Cascade)


}



//좋아요 테이블 정의
model Likes {
  id        Int       @id @default(autoincrement())
  createdAt     DateTime  @default(now()) 

  // 외래 키
  projectId     Int       
  userId        Int      

  // 관계 설정
  users         Users     @relation(fields: [userId], references: [id], onDelete: Cascade)
  projects      Projects  @relation(fields: [projectId], references: [id], onDelete: Cascade)


}



//북마크 테이블 정의 
model Bookmarks {
  id   Int       @id @default(autoincrement())
  createdAt     DateTime  @default(now()) 

  // 외래 키
  projectId    Int      
  userId        Int      

  // 관계 설정
  users         Users     @relation(fields: [userId], references: [id], onDelete: Cascade)
  projects      Projects  @relation(fields: [projectId], references: [id], onDelete: Cascade)


}