Skip to content
Source
Sign inSign up
TanStack/db

feat: `toArray()` equivalent for `findOne` subqueries #1481

Send to my agent
Closedkevin-dp(GitHub profile, opens in a new tab) opened this issue 3 comments

When using includes (nested queries) we can use toArray to materialize the results of the nested query into an array instead of a collection. However, when the nested query uses findOne to return a single element it is annoying to have to deal with a singleton array. We want a toArray equivalent for this use case that simply returns that one element.

I'm questioning if "toArray" is a good name because it means we now have to introduce another function for subqueries that return a single element. But conceptually both are doing the same: materializing the underlying collection. So perhaps materialize(subquery) would be a better name and it will return an array if the underlying query returns multiple results and a single element if it's a singular query.

  1. @kevin-dp for subqueries another idea could be to use .findOne() (same as base queries) and .findMany() (as alternative to wrap the subquery with toArray(), if none of them is used the collection is returned. WDYT

  2. @kevin-dp for subqueries another idea could be to use .findOne() (same as base queries) and .findMany() (as alternative to wrap the subquery with toArray(), if none of them is used the collection is returned. WDYT

    I like this idea but it means there would be a findMany method on queries that is essentially a no-op if it is in a top-level query. Something we need to think a bit more about. Let's implement the materialize function and we can replace it by findOne and findMany in a follow up PR if we want to. Thanks for the good suggestion 💯

    EDIT: Also, it would mean that a findOne subquery always materializes into a value and cannot remain a singleton collection.

  3. Any status updates on this one? This is a big unlock for much more ergonomic querying!

Sign in to join this conversation.