SaraS
Convex Community12mo ago
8 replies
Sara

How do I search for a value in a table that is related to another table in relationship?

So I have a table called messages and another table called messages members, I'm trying to search for messages that are equal to the search query but at the same time the user is part of?

I've tried creating a paginated query:
    const channelsWithUser = await ctx.db.query("members")
    .withIndex("by_userId", q => q.eq("userId",user._id))
    .collect(); // this has the channel id as a coloumn
    
    const allDmsThatMatch = paginator(ctx.db, schema)
    .query("directMessages")
    .withIndex("by_channelId", (q) => q.eq("channelId", /** I want to put the channelId here */))
    .order("desc")
    .paginate({numItems:10, cursor:args.searchQuery});


And I tried to mix and match using asyncMap with search:
    const allDMsThatFit = await asyncMap(channelsWithUser, async (doc) => {
      const findDMsThatMatchInChannel = await ctx.db
        .query('directMessages')
        .withSearchIndex('search_content', (q) =>
          q
            .search('directMessageContent', args_0.searchQuery)
            .eq('directMessageChannelId', doc.directMessageChannelId),
        )
        .take(5);
      return {
        findDMsThatMatchInChannel,
        channelId: doc.directMessageChannelId,
      };
    });

but got lost on how to handle the channelId side? any advice on how can this be done?
Was this page helpful?