instantdb / instant

Instant is a modern Firebase. We make you productive by giving your frontend a real-time database.
https://instantdb.com
Apache License 2.0
6.63k stars 181 forks source link

Recursive queries #44

Open anyinfa opened 2 months ago

anyinfa commented 2 months ago

Assuming I have a Block with a one-to-many relationship field children that contains self-links. When I execute the query db.useQuery({ blocks: { children: {} } });, it only returns the first level of children. How can I make it return all levels of children together (up to a finite depth) without knowing the exact number of levels in advance? Thank you.

Block {
  id
  name
  children: [Block]
}
nezaj commented 2 months ago

At the moment we don't support recursive queries so you would need to explicitly query for the levels in advance. Making a note of this!

verveguy commented 2 months ago

At first I also thought I wanted recursive queries, but instead I've tackled this so far by letting my React app nest the UI layer components and each nested component using its own useQuery() only fetching 'Blocks' for their current level of the tree. Since my app allows the user to show/hide components in the tree, this actually helps address the issue of determining depth - I don't need to.

The tricky bit is having all those useQuery() hooks outstanding ... I'm not sure but I may be seeing way too many render() calls for no good reason.

However, I can also see cases where I'd want to grab a larger tree depth for some other purpose in a single UI component ... so this would be useful, yes.

anyinfa commented 2 months ago

Indeed, I have encountered such a need where I want to export all the data of a tree at once, which is unrelated to component rendering. In this case, it might be more appropriate to use a server-side approach to fetch the data or use useQuery within the component.

You are correct. If the data is being fetched for rendering purposes, I would also control the depth of data retrieval using useQuery, as it is sufficient for the current needs.