Skip to main content

Votable Posts

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

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

model User {
id Int @id @default(autoincrement())
createdAt DateTime @default(now())
email String @unique
name String?
role Role @default(USER)
posts Post[]
PostVote PostVote[]
}

model Post {
id Int @id @default(autoincrement())
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
published Boolean @default(false)
title String @db.VarChar(255)
author User? @relation(fields: [authorId], references: [id])
authorId Int?

votesCount Int @default(0)
downvotesCount Int @default(0)
upvotesCount Int @default(0)
PostVote PostVote[]
}

enum Role {
USER
ADMIN
}

model PostVote {
post Post @relation(fields: [postId], references: [id])
postId Int
user User @relation(fields: [userId], references: [id])
userId Int
voteType Int // 1 = upvote, -1 = downvote
@@id([userId, postId])
}