Having issues adding likes based on the model bel...
# orm-help
r
Having issues adding likes based on the model below. Is this the right way to go about it? I'm trying to do it off the Playlist so I can ++ the likeCount and create the Like at the same time but not having any luck, I've tried just about everything here(nested writes) & I'm super stuck
Copy code
model User {
  id            String     @id @default(cuid())
  name          String?
  email         String?    @unique
  emailVerified DateTime?
  image         String?
  accounts      Account[]
  sessions      Session[]
  Playlist      Playlist[]
  Like          Like[]
}

model Playlist {
  id          String   @id @default(cuid())
  createdAt   DateTime @default(now())
  updatedAt   DateTime @updatedAt
  likeCount   Int?
  type        PlaylistType
  url         String
  name        String?
  description String?
  author      String?
  tags        Tag[]
  submittedBy User?   @relation(fields: [userId], references: [id])
  likes       Like[]
  userId      String?
}

model Like {
  id         String   @id @default(cuid())
  createdAt  DateTime @default(now())
  user       User    @relation(fields: [userId], references: [id])
  userId     String
  playlist   Playlist @relation(fields: [playlistId], references: [id])
  playlistId String

  @@unique([userId, playlistId])
}
Copy code
const newLike = { userId: 'cl883tgpm0079x4u5ivj6bzv9', playlistId: 'cl86l256l00935cu59fv08pc3' }
    const result = await prisma.playlist.update({
        where: {
            id: 'cl86l256l00935cu59fv08pc3'
        },
        data: {
            likeCount: { increment: 1 },
            likes: {
                upsert: {
                    create: newLike,
                    update: newLike
                }
            },
        },
        include: {
            likes: true,
            tags: true
        }

    })
    console.log(result)
āœ… 1
n
Hi Rob šŸ‘‹ In the query that you have shared what is the purpose of doing upsert? The upsert call is missing the where clause due to which you must be facing an issue. Can you use create query like this?
Copy code
const result = await prisma.playlist.update({
    where: {
      id: 'cl86l256l00935cu59fv08pc3',
    },
    data: {
      likeCount: { increment: 1 },
      likes: {
        create: {
          userId: 'cl883tgpm0079x4u5ivj6bzv9',
        },
      },
    },
    include: {
      likes: true,
    },
  });
  console.log(result);
šŸ‘ 1
j
@Nurul just a generic question for what Rob is doing here. Would it be considered best practice to have a separate field for like count, or just do a _count on the likes relation every time? I’m sure the extra field is more performant, but is it worth the extra workload of having to manage the state in multiple places, plus the chance that they might fall out of sync? I know this is subjective, just curious what your thoughts are?
r
@Joey -- for sure I had the same thought but I saw the alternative was running a query on likes to get sum for every playlist when there's hundreds or thousands would be become costly. This is why I'm trying to do it on/with the Playlist Model bc the whole thing either works or doesn't so helps ensure things are in sync.
šŸ‘ 1
@Nurul that works, thanks so much! Can I buy you a coffee? šŸ˜‰
šŸ™ 1
šŸ˜„ 1