14. Photo Model(Hashtag, Like)

김종민·2022년 4월 25일
0

insta-backend

목록 보기
13/37

1. model( Photo, Hashtag, Like), in schema.prisma

model User {
  id        Int      @id @default(autoincrement())
  username  String   @unique
  email     String   @unique
  password  String
  bio       String?
  avatar    String?
  followers User[]   @relation("FollowRelation", references: [id])
  following User[]   @relation("FollowRelation", references: [id])
  createdAt DateTime @default(now())
  updatedAt DateTime @updatedAt
  photos    Photo[]
  likes     Like[]
}

model Photo {
  id        Int       @id @default(autoincrement())
  user      User      @relation(fields: [userId], references: [id])
  userId    Int
  file      String
  caption   String?
  hashtags  Hashtag[]
  likes     Like[]
  createdAt DateTime  @default(now())
  updatedAt DateTime  @updatedAt
}

model Hashtag {
  id        Int      @id @default(autoincrement())
  hashtag   String   @unique
  photos    Photo[]
  createdAt DateTime @default(now())
  updatedAt DateTime @updatedAt
}

model Like {
  id        Int      @id @default(autoincrement())
  createdAt DateTime @default(now())
  updatedAt DateTime @updatedAt
  photo     Photo    @relation(fields: [photoId], references: [id])
  user      User     @relation(fields: [userId], references: [id])
  photoId   Int
  userId    Int
  @@unique([photoId, userId])
}

2. Photo, Hashtag, Like in photos.typedefs.js

import { gql } from 'apollo-server'

export default gql`
  type Photo {
    id: Int!
    user: User!
    file: String!
    caption: String
    hashtags: [Hashtag]
    createdAt: String!
    updatedAt: String!
    likes: Int!
  }
  type Hashtag {
    id: Int!
    hashtag: String!
    photos(page: Int!): [Photo]
    totalPhotos: Int!
    createdAt: String!
    updatedAt: String!
  }
  type Like {
    id: Int!
    photo: Photo!
    createdAt: String!
    updatedAt: String!
  }
`

check!!! relation,

profile
코딩하는초딩쌤

0개의 댓글