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

Create a hook for direct MongoDB collection access

      Problem

      If for some reason, a developer wanted direct access to a MongoDB collection, they would have to type the following code to do so:

      Unable to find source-code formatter for language: typescript. 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
      function QueryPlants() {
        // Get currently logged in user
        const user = useUser();
      
        const getPlantByName = async (name: string) => {
          // Access linked MongoDB collection
          const mongodb = user!.mongoClient('mongodb-atlas');
          const plants = mongodb.db('example').collection<Plant>('plants');
          // Query the collection
          const response = await plants.findOne({name});
      
          return response;
        };
        // ...
      }
      

      Solution

      This could be abstracted into a single hook:

      Unable to find source-code formatter for language: typescript. 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 plants = useMongoDb({cluster: 'mongodb-atlas', db: 'example', collection: 'plants'});
      

      Under the hood, the hook could register a collection watcher, to update when anything changes.

      Alternatives

      The above is just a suggestion of what could be possible, but in practice, there could be something better or more ergonomic. A bit of design and research should be done to determine the best path forward.

      How important is this improvement for you?

      Would be a major improvement

      Feature would mainly be used with

      Atlas App Services: Auth or Functions etc

            Assignee:
            claus.rorbech@mongodb.com Claus Rørbech (Inactive)
            Reporter:
            andrew.meyer@mongodb.com Andrew Meyer
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated: