A prisma.schema for an advance Social Media Website
Tags: javascript, prisma, nextjs, sh20raj
Published: 2024-04-09 17:46:51


This is a Prisma schema file that defines the data models, enums, and relationships for a social media application. 

> You can suggest things in Comments. ❤️

This is example Repository :- https://github.com/SH20RAJ/shadev2/

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

datasource db {
  provider = "mysql"
  url      = env("DATABASE_URL")
}
enum Type {
  VIDEO
  SHORTVIDEO
  TEXT
  IMAGE
  AUDIO
  POLL
  GAME
}

enum Status {
  PUBLIC
  PRIVATE
  UNLISTED
}

enum ReactionType {
  LIKE
  LOVE
  HAHA
  WOW
  SAD
  ANGRY
}

model User {
  id            Int            @id @default(autoincrement())
  username      String         @unique
  email         String         @unique
  password      String
  name          String?
  bio           String?
  avatar        String?
  coverPhoto    String?
  createdAt     DateTime       @default(now())
  updatedAt     DateTime       @updatedAt
  posts         Post[]
  likes         Like[]
  status        Status         @default(PUBLIC)
  comments      Comment[]
  followers     Follow[]       @relation("UserFollowers")
  following     Follow[]       @relation("UserFollowing")
  roles         UserRole[]
  receivedMessages Message[]   @relation("ReceiverMessages")
  sentMessages  Message[]     @relation("SenderMessages")
  pollResponses PollResponse[]

}

model Post {
  id            Int            @id @default(autoincrement())
  content       String
  image         String?
  contentURL    String?
  type          Type?          @default(TEXT)
  createdAt     DateTime       @default(now())
  updatedAt     DateTime       @updatedAt
  author        User           @relation(fields: [authorId], references: [id])
  authorId      Int
  status        Status         @default(PUBLIC)
  likes         Like[]
  comments      Comment[]
  tags          Tag[]    
  pollOptions PollOption[]
     
}

model Like {
  id            Int            @id @default(autoincrement())
  user          User           @relation(fields: [userId], references: [id])
  userId        Int
  post          Post           @relation(fields: [postId], references: [id])
  postId        Int
  reaction      ReactionType   @default(LIKE)
  createdAt     DateTime       @default(now())
}

model Comment {
  id            Int            @id @default(autoincrement())
  content       String
  user          User           @relation(fields: [userId], references: [id])
  userId        Int
  post          Post           @relation(fields: [postId], references: [id])
  postId        Int
  createdAt     DateTime       @default(now())
  updatedAt     DateTime       @updatedAt
  media         String?        
}

model Tag {
  id        Int      @id @default(autoincrement())
  name      String   @unique
  createdAt DateTime @default(now())
  updatedAt DateTime @updatedAt
  posts     Post[]  
}

model Follow {
  id            Int            @id @default(autoincrement())
  follower      User           @relation("UserFollowers", fields: [followerId], references: [id])
  followerId    Int
  following     User           @relation("UserFollowing", fields: [followingId], references: [id])
  followingId   Int
  createdAt     DateTime       @default(now())
  @@unique([followerId, followingId])
}

model UserRole {
  id            Int            @id @default(autoincrement())
  userId        Int
  role          Role
  user          User           @relation(fields: [userId], references: [id])
  createdAt     DateTime       @default(now())
}

enum Role {
  ADMIN
  MODERATOR
  USER
}

model Message {
  id            Int            @id @default(autoincrement())
  content       String
  senderId      Int
  receiverId    Int
  sender        User           @relation("SenderMessages", fields: [senderId], references: [id])
  receiver      User           @relation("ReceiverMessages", fields: [receiverId], references: [id])
  createdAt     DateTime       @default(now())
  updatedAt     DateTime       @updatedAt
}


model PollOption {
  id        Int      @id @default(autoincrement())
  postId    Int
  post      Post     @relation(fields: [postId], references: [id])
  option    String
  responses PollResponse[]
  createdAt DateTime @default(now())
  updatedAt DateTime @updatedAt
}

model PollResponse {
  id          Int      @id @default(autoincrement())
  pollOptionId Int
  pollOption  PollOption @relation(fields: [pollOptionId], references: [id])
  userId      Int
  user        User     @relation(fields: [userId], references: [id])
  createdAt   DateTime @default(now())
}
```

Here's a brief overview of the models and relationships:

- `User`: Represents a user of the social media app. A user has a unique username and email, and can have a password, name, bio, avatar, and cover photo. A user can create posts, like posts, comment on posts, follow other users, and send and receive messages. A user can have multiple roles, such as admin, moderator, or user.
- `Post`: Represents a post made by a user. A post can be of different types, such as video, text, image, audio, poll, or game. A post has a content, image, content URL, type, created at, updated at, author, status, likes, comments, tags, and poll options.
- `Like`: Represents a user's like on a post. A like has a user, post, reaction, and created at.
- `Comment`: Represents a user's comment on a post. A comment has a content, user, post, created at, updated at, and media.
- `Tag`: Represents a tag that can be associated with a post. A tag has a name and created at.
- `Follow`: Represents a user's follow relationship with another user. A follow has a follower, following, and created at.
- `UserRole`: Represents a user's role in the social media app. A user role has a user, role, and created at.
- `Message`: Represents a message sent between two users. A message has a content, sender, receiver, created at, and updated at.
- `PollOption`: Represents an option in a poll created by a user. A poll option has a post, option, created at, updated at, and responses.
- `PollResponse`: Represents a user's response to a poll option. A poll response has a poll option, user, and created at.

The schema also includes several enums for different types and statuses, such as `Type`, `Status`, and `ReactionType`. These enums are used to define the possible values for different fields in the models.

The schema uses the `prisma-client-js` provider for the generator client, and the `mysql` provider for the datasource db. The database connection URL is set using the `DATABASE_URL` environment variable.