Hi everyone, I am currently building a chat applic...
# orm-help
s
Hi everyone, I am currently building a chat application (using Prisma and MongoDB) that supports multi-user group chats, and I am having some trouble when trying to query for all conversations that the signed in user is a part of. The entities of interest here are
Conversations
,
ConversationParticipants
, and
Users
. The relationship between
Users
and
Conversations
is many-to-many, hence the creation of the
ConversationParticipant
model. Here is my schema:
Copy code
model User {
  id             String    @id @default(auto()) @map("_id") @db.ObjectId
  name           String?
  email          String? @unique
  username       String  @unique
  messages       Message[]
  conversations  ConversationParticipants[]
  createdAt      DateTime  @default(now())
  updatedAt      DateTime  @updatedAt
}

model Conversation {
  id              String    @id @default(auto()) @map("_id") @db.ObjectId
  participants    ConversationParticipants[]
  messages        Message[]  @relation("conversationMessages")
  latestMessage   Message?   @relation(name: "latestConversationMessage", fields: [latestMessageId], references: [id], onUpdate: NoAction, onDelete: NoAction)
  latestMessageId String?    @unique
  createdAt       DateTime  @default(now())
  updatedAt       DateTime  @updatedAt
}

model ConversationParticipants {
  id              String    @id @default(auto()) @map("_id") @db.ObjectId
  conversation    Conversation @relation(fields: [conversationId], references: [id])
  conversationId  String
  user            User      @relation(fields: [userId], references: [id])
  userId          String
}

model Message {
  id              String        @id @default(auto()) @map("_id") @db.ObjectId
  conversation    Conversation  @relation(name: "conversationMessages", fields: [conversationId], references: [id])
  conversationId  String
  isLatestIn      Conversation? @relation("latestConversationMessage")
  sender          User          @relation(fields: [senderId], references: [id])
  senderId        String
  body            String
  createdAt       DateTime      @default(now())
  updatedAt       DateTime      @updatedAt
}
When the user signs in, I am trying to find all of the users conversations (i.e. all of the conversations where there is an existing
ConversationParticipant
entity). My current query is the following:
Copy code
// the user id comes from the session - working properly
const { id } = session;

const conversations = await prisma.conversation.findMany({
    where: {
      participants: {
        some: {
          userId: {
            equals: id,
          },
        },
      },
    },
    include: {
      participants: true,
    },
});
All of the similar scenarios I can find online are all using the
some
operator as well, so I am not sure why this isn’t working. I must be missing something 😅 Any and all help would be greatly appreciated!
1
a
Hey Shadee! At first glance, everything you have looks ok to me. What exactly about it isn’t working? Are you getting specific errors or just the wrong result?
s
Hey Austin! Thanks for the reply ☺️ Currently no errors, just the wrong result. With that query, it returns an empty array. And the data does indeed exist. The same type of logic seems to be working on the include statement which is puzzling
a
Yeah, something isn’t right here. It looks like the query works for me if I switch
some
to
every
, which doesn’t make sense. The schema + query work as expected on MySQL and SQLite. Would you mind opening a bug report?
s
So you think the issue is specific to Mongo? And yes, I can definitely do that