thedevstockgirl
thedevstockgirl•11mo ago

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.
No description
23 Replies
Michal Srb
Michal Srb•11mo ago
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?
thedevstockgirl
thedevstockgirlOP•11mo ago
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.
CodingWithJamal
CodingWithJamal•11mo ago
@thedevstockgirl offtopic but how did you get that screenshot look like that?
Michal Srb
Michal Srb•11mo ago
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.
lee
lee•11mo ago
(you still have to sort the array though 🙂 )
Michal Srb
Michal Srb•11mo ago
@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.
CodingWithJamal
CodingWithJamal•11mo ago
wait you can index an array of ids? I thought someone said you couldn’t in my thread just the other day
Michal Srb
Michal Srb•11mo ago
I just tested it. You definitely can.
thedevstockgirl
thedevstockgirlOP•11mo ago
@CodingWithJamal , free vscode plugin, Codesnap
No description
CodingWithJamal
CodingWithJamal•11mo ago
oh thats cool thanks
thedevstockgirl
thedevstockgirlOP•11mo ago
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?
CodingWithJamal
CodingWithJamal•11mo ago
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
Michal Srb
Michal Srb•11mo ago
What you cannot do is "filter by is x within an array"
CodingWithJamal
CodingWithJamal•11mo ago
ah
Michal Srb
Michal Srb•11mo ago
Whereas this is "filter by x is an array"
CodingWithJamal
CodingWithJamal•11mo ago
yeah thats understandable
thedevstockgirl
thedevstockgirlOP•11mo ago
Ah. So put the id's in a sorted array. and index by that field Awesomeness
CodingWithJamal
CodingWithJamal•11mo ago
@thedevstockgirl i have never used lodash you make me want to try it some of the helpers look interestin for async dev
lee
lee•11mo ago
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
thedevstockgirl
thedevstockgirlOP•11mo ago
@Lee , let me share some code to see if I understand what you mean
Michal Srb
Michal Srb•11mo ago
This is how I tested it btw:
No description
thedevstockgirl
thedevstockgirlOP•11mo ago
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
lee
lee•11mo ago
awesome!

Did you find this page helpful?