I have been using Realm Database with Flexible Sync to synchronize with a remote Atlas MongoDB database, and my cluster only contains one database, so the following code has been working fine until now.
However, I need to add different databases which have the same collections, and since I’m using Flexible Sync which does not support using Partition Values for differentiation, how should I subscribe to different databases? How can I distinguish between different databases?
The TLDR is that by default we use the “tableName” in realm to map to the “collection name” in MongoDB, but you can override this with the “title” field in the schema and therefore you can have two collection (Employees.Information) and (Customers.Information) map to Realm tables with the names “employee_info” and “customer_info” if you just use the title field in the json schema defined in the cloud.
For what its worth, I am a bit confused what you are suggesting is different with partition sync, as the same problem would exist there.
Thanks for replying.
Let me clarify the issue first. I have one cluster in Atlas, and the cluster can have many databases, and one database can have many collections. I use Device sync to make the synchronization between my Android app with Realm and the cluster.
I only had one database before named “database”, and it have a collection named “Word”. Now I want to create another database named like “test” also having a collection “Word”, the Realm Model is the same. How can I make different subscription to both “database”-“Word” and “test”-“Word”?
I have never used partition sync before, just ChatGPT suggests that I can use the partition value to differentiate different database.
Unfortunately, one of the times ChatGPT has led people astray. In order to sync on database.Word and test.Word, you just define schemas for each in the UI such that they have different “title” fields. Then you will need to define a model in Realm for each SDK and you can subscribe to it as normal.
So there is no way to subscribe to different database, but only through App Service Schema in the web UI, is that correct?
In fact, I want to realize multi-user, every user has collections like “Word” in their own database, to ensure the safety and segregation. But it seems I can neither create new database nor create new schema to map them together only in Android Java Code.
Please tell me what is the best practice for multi-user in Realm~
I would highly recommend having a single collection and using subscriptions and/or permissions to enforce that access is only granted to specific users / documents. Multiple collections means duplicating the realm tables (which is fine), but it is a bit of an anti-pattern.
I have read the doc, and to conclude is that the best approach is to set a userId or tenantId for each document in collection, and use it to differ when log in or CRUD.If possible, it’s better to make small replica set from for every user to ensure the security.
Is my conclution right?
I have little experience in database, especially for multi-user, thanks for your patience!
A small repica set for each user is a way to go, but there is a lot of overhead associated with that. We have many users with hundreds of GB of data partitioned for thousands of users and they have mostly all opted to go the route of putting it on one Cluster/Database. Also, note that Device Sync only supports syncing to one cluster. So if you went the route of one-customer-one-cluster, you would also need a new “app” for each customer.
Do you have any idea about these errors when build with gradle?It seems the generated files by realm make errors.But these errors don’t influence my app usage of Realm.