I am in the process of developing a simple app similar to Tinder using Prisma. In this app, users can swipe left or right to like or dislike other users. I want to be able to retrieve matches (users who also like me) and candidates (all users except myself, my matches, the users I dislike, and the users that dislike me).
Initially, I created a User model along with Likes and Dislikes models
model User {
id String @id
username String @default(value: "")
age Int @default(value: 0)
bio String @default(value: "") @db.VarChar(1000)
}
model Likes {
id Int @id @default(autoincrement())
userId String
likedUserId String
}
model Dislikes {
id Int @id @default(autoincrement())
userId String
dislikedUserId String
}
I then wrote some complex SQL statements and executed them using queryRaw
While it worked, I realized I might not be utilizing Prisma effectively as I was mainly translating SQL to Prisma syntax. This led me to revise my model to include the following changes
model User {
id String @id
username String @default(value: "")
age Int @default(value: 0)
bio String @default(value: "") @db.VarChar(1000)
liked User[] @relation("LikedUserToUser")
disliked User[] @relation("DislikedUserToUser")
}
This modification prompted my IDE to suggest adding the opposite relation or running prisma format
. After making those adjustments, my model looked something like this
model User {
id String @id
username String @default(value: "")
age Int @default(value: 0)
bio String @default(value: "") @db.VarChar(1000)
liked User[] @relation("LikedUserToUser")
disliked User[] @relation("DislikedUserToUser")
userToLike User? @relation("LikedUserToUser", fields: [userId], references: [id])
userToDislike User? @relation("DislikedUserToUser", fields: [userId], references: [id])
userId String?
}
At this point, the functionality seemed to be working smoothly. I could 'like' a user with
const user = await prisma.user.update({
where: { id: '1' },
data: {
liked: {
connect: { id: '2' },
},
},
})
When querying for user '1' including their likes, the output was as follows
{
id: '1',
username: 'Cheryl',
age: 36,
bio: 'ornare, libero at auctor ullamcorper',
userId: null,
liked: [
{
id: '2',
username: 'Daphne',
age: 57,
bio: 'at sem molestie sodales',
userId: '1'
}
]
}
My current challenge lies in performing more intricate queries such as identifying matches and candidates. Any suggestions?