T
TanStack2mo ago
correct-apricot

What is the proper pattern for capturing a one to many (1:*) relationship with useLiveQuery?

Let's say you have a list collection and a tasks collection. One collection has many tasks. How can I return something like the following from useLiveQuery?
{
...list,
tasks: [...tasks]
}
{
...list,
tasks: [...tasks]
}
3 Replies
adverse-sapphire
adverse-sapphire2mo ago
not properly supported atm — here's the issue — very common request! https://github.com/TanStack/db/issues/288
GitHub
Joins with a hierarchical projection (includes) · Issue #288 · Ta...
The data shown in a user interface if often hierarchical (projects -> issues -> comments) The joins supported by Tanstack DB are SQL like joins where the resulting rows are flat, however user...
fascinating-indigo
fascinating-indigo2mo ago
Yeah, for now I've been doing a leftJoin, which of course repeats the parent with each child instance, and then just using javascript to extract unique parent(s) from the returned child dataset. If there's a better way to do that in the meantime, I'm all ears! Or if selecting a single record and its children, you of course select the specific ID of the parent in the where clause, then .select(({ parent, child }) => ({ ...child, parent })). Then the single parent is just data[0].parent. If you need it as if it was done 'properly' like in a hook that's supposed to select a single parent with its children as a property of the parent, you could then do return { ...data[0].parent, childItems: data }
adverse-sapphire
adverse-sapphire2mo ago
yeah a custom hook like this seems like a pretty decent solution!

Did you find this page helpful?