Uploaded image for project: 'Realm JavaScript SDK'
  1. Realm JavaScript SDK
  2. RJS-2195

Add argument to `useQuery` to allow result to be filtered and sorted

      Problem

      Currently when using useQuery, the result must be passed into a useMemo in order to get a memoized filtered or sorted result. This is also not particularly useful, as when the result of useQuery is re-rendered, the memo is called again as well as any filtered or sorted method calls.

      For example

      Unable to find source-code formatter for language: tsx. Available languages are: actionscript, ada, applescript, bash, c, c#, c++, cpp, css, erlang, go, groovy, haskell, html, java, javascript, js, json, lua, none, nyan, objc, perl, php, python, r, rainbow, ruby, scala, sh, sql, swift, visualbasic, xml, yaml
        const user = useUser();
        const userId = user.id;
        const result = useQuery(Task);
      
        const tasks = useMemo(() => result.filtered(“ownerId == $0”, userId).sorted('createdAt'), [result, userId]);
      

      Solution

      A more elegant solution would be to have a callback within useQuery which would allow the filtered and sorted functions to be applied within useQuery itself. It could also take a dependency array to determine when to reset the filtered or sorted method calls.

      Unable to find source-code formatter for language: tsx. Available languages are: actionscript, ada, applescript, bash, c, c#, c++, cpp, css, erlang, go, groovy, haskell, html, java, javascript, js, json, lua, none, nyan, objc, perl, php, python, r, rainbow, ruby, scala, sh, sql, swift, visualbasic, xml, yaml
       const tasks = useQuery("Task", (tasks) => {
         return tasks.filtered(“ownerId == $0”, userId);
       }, [userId]);
      

      Alternatives

      No response

      How important is this improvement for you?

      Would be a major improvement

      Feature would mainly be used with

      Local Database only

            Assignee:
            andrew.meyer@mongodb.com Andrew Meyer
            Reporter:
            andrew.meyer@mongodb.com Andrew Meyer
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated:
              Resolved: