Sample realm sync with partition keys for different types (public, private, group)

Is there a good sample realm sync project with partition keys for different types (public, private, group) with multiple collections?

The current realm tutorial discussed a sample with collection user, project, task.But it does not discuss how to set up collections with different partition keys so that users can be associated with certain realms for task and project (public, private, group) . A practical “real” sample would be helpful to design proper realm sync.

@jerry_he we are working on v2 of the tutorial right now and should be released shortly. But to answer your question you will need to open multiple realms and name them different realm references with different sync configurations that have different partitionKeys - for example:

let user = app.currentUser()
let partitionValue = "PUBLIC"
publicRealm = try! Realm(configuration: user.configuration(partitionValue: partitionValue)) 

...

let user = app.currentUser()
let partitionValue = "user=1231"
privateRealm = try! Realm(configuration: user.configuration(partitionValue: partitionValue))

...

let user = app.currentUser()
let partitionValue = "family=851024"
sharedRealm = try! Realm(configuration: user.configuration(partitionValue: partitionValue))

Docs here:
https://docs.mongodb.com/realm/sync/partitioning/

1 Like

Hi Ian,

Thanks for note. The above code is good. How to make sure some/current user can access realm with partitionValue = “family=851024”? We need to do something in rules/filters to make permission. That is the part l am struggling. We can setup any partition value, but in order for someone to access that realm, some permission rules/filers need to be in place. Wonder you have good example?

Also for v2 of the tutorial, is something already in somewhere so that we can have earlier access?

Thanks,
JH

@jerry_he One way would be set custom user data that includes metadata fields about what partitions the user has read or write access to - https://docs.mongodb.com/realm/users/define-custom-user-data/index.html

You can then define the permissions like so in the Sync Permissions UI
https://docs.mongodb.com/realm/sync/rules/#id5

For instance this could be the syntax for read permissions

{
"%%user.custom_data.readPartitions" : "%%partition"
}

@Ian_Ward This was the missing link that I was looking for. Thanks! The key here is that the custom_data is modified by the server (through MongoDB CRUD functions), not the user on the client side, to store application specific data about the user. This should not be confused with the user_data metadata that is set on the User object through a JWT authentication token.