Likes API
Unanswered
Serengeti posted this in #help-forum
SerengetiOP
Hello everyone. I'm working on a project like a social media. I need to handle the likes of posts. I use prisma. What is the best way for me to build an api? I am open to using server components and actions.
Could you tell me how to get if a user has liked a post? I used to get an array of all the likes and did so: likes.some(e => e.id === user.id). But now I use _count.
Could you tell me how to get if a user has liked a post? I used to get an array of all the likes and did so: likes.some(e => e.id === user.id). But now I use _count.
16 Replies
ideally, I would have likes in another table
so I can join to perform queries and see who liked what
@@ts-ignore ideally, I would have likes in another table
SerengetiOP
Yes, exactly. I store the likes in another table. But how do I find out in a simple way during rendering that the user liked the post if I extract the number of likes?
I can attach an array of likes from a specific user using prisma and check that the array is not empty. But isn't there a more beautiful way? If so, how to do it as cleanly as possible?
@Serengeti Yes, exactly. I store the likes in another table. But how do I find out in a simple way during rendering that the user liked the post if I extract the number of likes?
you can do a include in prisma with a where clause
@@ts-ignore you can do a include in prisma with a where clause
SerengetiOP
Yes, but there will be an array of one element. Maybe there is some way to improve the query using prisma?
prisma.post.findMany({include: {likes: {where: {userId: userId}}}})@Serengeti Yes, but there will be an array of one element. Maybe there is some way to improve the query using prisma?
prisma.post.findMany({include: {likes: {where: {userId: userId}}}})
prisma.post.findMany({include:{_count:{likes: true}, likes:{where:{userId}}})this will, hopefully, include no of likes as well as the like record if the user liked the post
@@ts-ignore this will, hopefully, include no of likes as well as the like record if the user liked the post
SerengetiOP
I'm sorry for bothering you with a stupid question. That's what I wanted to do. I don't really like that the result of likes is an array. That was the question.
result of likes?
I mean the _count will be a number
@@ts-ignore result of likes?
SerengetiOP
likes are [{userId: userId}] or []
the result would look something like this:
but the likes table will only have record if the user liked it
[
{
...post details,
"_count":{likes: 50},
"likes":[{userId}]
}
]but the likes table will only have record if the user liked it
if you don't want this behavior, you will have to write a raw sql call
SerengetiOP
I understand that. I just wanted the result to be immediately isLiked: boolean. But well, I can work with it without any problems. Thanks for the help
@Serengeti I understand that. I just wanted the result to be immediately isLiked: boolean. But well, I can work with it without any problems. Thanks for the help
I had the same issue in the past and also looked for a solution to prevent a lot of DB requests. I solution was to do it the same like ts-ignore mentioned: with another table. And checking if my specific user liked it or not (in my case: bought it or not), I did this:
Dont judge me.. its drizzle, but I guess you get the concept. Works like a charm and is performant, when you working with index
export async function hasBought(contentId: string, userId: string) {
const result = await db.select({purchaseId: purchases.id})
.from(purchases)
.innerJoin(purchaseItems, eq(purchaseItems.purchaseId, purchases.id))
.where(and(eq(purchases.userId, userId), eq(purchaseItems.itemId, contentId)))
return !!result[0].purchaseId;
}Dont judge me.. its drizzle, but I guess you get the concept. Works like a charm and is performant, when you working with index