
플라즈마 스키마 추가
generator client {
provider = "prisma-client-js"
}
datasource db {
provider = "sqlite"
url = env("DATABASE_URL")
}
model User {
id Int @id @default(autoincrement())
username String @unique
email String? @unique
password String?
phone String? @unique
github_id String? @unique
avatar String?
created_at DateTime @default(now())
updated_at DateTime @updatedAt
tokens SMSToken[]
products Product[]
posts Post[]
Comment Comment[]
Like Like[]
}
model SMSToken {
id Int @id @default(autoincrement())
token String @unique
created_at DateTime @default(now())
updated_at DateTime @updatedAt
user User @relation(fields: [userId], references: [id], onDelete: Cascade)
userId Int
}
model Product {
id Int @id @default(autoincrement())
title String
price Float
photo String
description String
created_at DateTime @default(now())
updated_at DateTime @updatedAt
user User @relation(fields: [userId], references: [id], onDelete: Cascade)
userId Int
}
model Post {
id Int @id @default(autoincrement())
title String
description String?
views Int @default(0)
created_at DateTime @default(now())
updated_at DateTime @updatedAt
user User @relation(fields: [userId], references: [id], onDelete: Cascade)
userId Int
comments Comment[]
likes Like[]
}
model Comment {
id Int @id @default(autoincrement())
payload String
created_at DateTime @default(now())
updated_at DateTime @updatedAt
user User @relation(fields: [userId], references: [id], onDelete: Cascade)
post Post @relation(fields: [postId], references: [id], onDelete: Cascade)
userId Int
postId Int
}
model Like {
created_at DateTime @default(now())
updated_at DateTime @updatedAt
user User @relation(fields: [userId], references: [id], onDelete: Cascade)
post Post @relation(fields: [postId], references: [id], onDelete: Cascade)
userId Int
postId Int
@@id(name: "id", [userId, postId])
}
atomic operation
- 현재 값을 모르더라도 연산할 수 있음
- increment: 1 → 1을 더해줘라
async function getPost(id: number) {
try {
const post = await db.post.update({
where: {
id,
},
data: {
views: {
**increment: 1,**
},
},
include: {
user: {
select: {
username: true,
avatar: true,
},
},
_count: {
select: {
comments: true,
likes: true,
},
},
},
});
return post;
} catch (e) {
return null;
}
}
revaildatePath
Cache Tags
function getCachedLikeStatus(postId: number) {
const cachedOperation = nextCache(getLikeStatus, ["product-like-status"], {
tags: [`like-status-${postId}`],
});
return cachedOperation(postId);
}
- like/dislike가 revalidatePath로 getpost를 재실행하면 조회수도 올라가버림
- 이런 문제를 해결하려면 좋아요의 정보를 제공하는 action을 별도로 만들어서
- like/ dislike를 실행하면 getPost가 아니라 해당 action을 revalidate하게 만들면 됨
- 일반적으로
like-status
만 하면 모든 포스트가 revalidate 될꺼임. 그래서 id 사용