"expand" a field on a document, that is array of ids?
I'm trying to figure out if there is a easy way to go about this?
My video table:
And i'm wondering how to best get the tasks, when i query a video that gets displayed in a detailed view in my app, where i then pass the tasks to a component that renders them in a list below the video.
What i'm doing right now:
The relation and setup i'm after here:
A video can have many tasks, but a task does not store a specific video id, like its doing right now. And when i then query a video i want to be able to 'expand' the tasks off the video and get their data and not only their ids
9 Replies
I read somewhere from the Convex team (can't find at the moment) that they recommend limiting nesting only if you expect a small number of values, eg., 10 or so I think.
Is there a reason you don't want to keep tasks and likes in their own tables and give each document a
videoId
?My tasks and likes is already stored in their own table. I think you might have misunderstood my post and the code snippets, or maybe i failed to make myself clear enough.
I'm trying to figure out how i would best get the data from the related tasks to a video, when querying the video. In some ORM's u can expand a field that holds relations, so i was wondering if something like that was possible here.
What i could do, is looping over the task ids i have on a video, and then querying them 1 by 1, pushing those that match the ids into an array, and then returning that with the video. But unsure whether there is a smarter way to do it
That's exactly it, except you can do it concurrently and let Convex take care of how the concurrency is actually handled:
They have helpers for this too, but underneath I'm pretty sure it's just doing this
Ohh that's interesting. Didn't think about that.
Yeah i can see what you mean
I'll try this out. Thanks!
GitHub
convex-helpers/convex/lib/relationships.ts at main · get-convex/con...
A collection of useful code to complement the official packages. - get-convex/convex-helpers
There's the helper you'd need, which actually does do something super obvious and simple - get all tasks by id, using an index on the id field of the task table
I think that's where I'm confused, why do you have a list of task ids stored with each video rather than storing the video id on the task? Can one task be related to multiple videos?
Ohh... yeah i see that is def obvious and simple hahah
So as of right now, different tasks might be related to mutiple videos yeah
okay, so then that helper won't work if you don't have
videoId
on the tasks table
without knowing more, using Promise.all()
would be my recommendationYeah, that's what i will go with for now. Does the job