Is it possible to sync data through Realm regardless of the partition key?

I’m working on an app where there are users that create records with a partition key, so that other users aren’t allowed to see it, but admins are supposed to be able to. I tried opening a realm with a null partition key, but I think I misread the documentation and that doesn’t do what I want. I changed the cluster read permission to allow the admin to read all records, but I can’t figure out how to sync it to the app.

Hi Weekend_Wings!

Right now, there isn’t a good way to sync all realms for an app to a single device. In fact, you generally shouldn’t open more than a few realms at a time for optimal performance. And depending on the amount of data your app puts in each realm, I’m not sure that you’d have enough storage on your admin’s device to store all of that data!

If you want to give admins access to all data, you could consider using MongoDB Data Access instead. This lets you view data in your linked MongoDB Atlas cluster (which stores a copy of all of your synced data) using MongoDB queries instead of syncing all of the data to persistent storage on your local device. For your admin use case, you could just query for all of the documents regardless of their partition value. You can take a look in the Realm SDKs section of the docs for guides on how to query with MongoDB Data access in your SDK of choice. Do you think this would fit your use case?

Thank you, that’s what I ended up doing.

For anyone else reading this in the future, I was using react native, so I did:

const [collection, setCollection] = useState([]);
user.mongoClient("mongodb-atlas").db("db").collection("Collection").find().then(res => setCollection(res));

If you’re doing it in a component function, you probably want to wrap it in useEffect with a function as the first argument and [user, refresh] as the second, where refresh is state that can be updated to refresh the data, so you don’t create a request after each render (since updating the state causes a re-render), or you’ll use a lot of data and bandwith and get terrible performance.

For an admin to be able to read all records, you need to set the permissions properly. I used the following:

"$or": [
    { "%%user.custom_data.name": "admin" },
    {
      "%%true": {
        "%function": {
          "arguments": [
            "%%partition"
          ],
          "name": "canReadPartition"
        }
      }
    }

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.