Looking up by an array equality
QQ: Will this code do what I think it will? Basically trying to model a group chat. And want to check if there is an existing thread with a user, when a user initiates a new conversation. For example, say from a providers profile, a user clicks the message icon, instead of creating a chat, I want to check if there is an existing thread. We model the thread association with threadParticipants. Just looking for some feedback or if there is a better approach.

23 Replies
My understanding is that you are trying to look up a thread based on the set of its participants.
The most efficient solution would be a point lookup. The point is the set of participants. If you're willing to only support a small number of participants (I would guess this is OK), you can sort the participants IDs and write them into a field (on the thread document or on a 1:1 connected document), as one long string, and lookup by this string.
What do you think?
Thanks Michal. I thought of that, and def think it's a viable solution. I created a string field on the thread, that would basically be participantIds, since convex does not support array field filter. The problem with that approach is having to keep track of it with every. update or change. Adding new participants, deleting, or any type of update.
Do you think the approach above adds too much complexity?
The string field also becomes a problem with perms, etc.
@thedevstockgirl offtopic but how did you get that screenshot look like that?
Small correction to what I wrote: Convex does support array fields and indexing on them, so you don't need to concatenate the strings, you can leave them as an array of IDs.
(you still have to sort the array though 🙂 )
@thedevstockgirl I think it is workable amount of complexity. You can probably tame it by careful layering of helpers that are used to write to the respective tables.
wait you can index an array of ids? I thought someone said you couldn’t in my thread just the other day
I just tested it. You definitely can.
@CodingWithJamal , free vscode plugin, Codesnap

oh thats cool thanks
And @Michal Srb , yes... wait what? I def did not think you can index on array fields
I am pretty sure I read somewhere in the docs this is not possible
Can you share a code sample?
interesting, i will play with this too. Although tbh convex is already so fast i think i just have the need to want to optimize for no reason...most my querys get cached and are under 10ms or paginated
yeah me too
What you cannot do is "filter by is x within an array"
ah
Whereas this is "filter by x is an array"
yeah thats understandable
Ah. So put the id's in a sorted array. and index by that field
Awesomeness
@thedevstockgirl i have never used lodash you make me want to try it some of the helpers look interestin for async dev
you also can't use an index to check if
args.participantIds
is a subarray of threadParticipants.participantIds
which might be what you want. you can only use an index to check direct equality@Lee , let me share some code to see if I understand what you mean
This is how I tested it btw:

This is awesome. And would absolutly work. Thanks so much
@Lee , we do probably want the direct equality. Say if for example you click chat with a provider, we only want to return the thread that has just you and the provider, not anyone that has you, the provider and someone else. But the way our chat is structured, a provider can add another provider or assistant to a chat. We still do need the thread participants table. But for a quick lookup on "create", this would work
awesome!