Hello guys!
I’m finally starting to move my app from Realm.io to MongoDBRealm and I’d like to discuss with you about my goals and how to reach them in a proper way.
GOAL
Design a mongo db schema and a Realm configuration for a react-native and GraphQL webapp.
DATA MODEL
UserData
Only the owner can read/write.
UserPermission
Only the system can read/write. It’s used for user_custom_data.
Content
Each content has an id. The user can’t create/update/delete any document. The user can read a document only if it’s in a list. This list could be stored in user_custom_data and should be not editable by the user.
A content contain also the total number of likes that is updated by a trigger on every “Like” (see below) insertion/deletion.
Like
User can insert document and delete it’s own documents.
WHAT I DID UNTIL NOW
I started creating a new react-native app and installing realm@beta. After struggling a little bit I setted up everything and tried some CRUD operation with some random models. Everything fine. On Realm cloud I used the query-based sync model so I could reach every specifications using a single realm and the fine grained permissions system. Here it seems a little bit different.
I created a collection of Content. Each document has a specific _pid (partition_id).
After I setted up the user_custom_data creating the collection UserPermissions on Atlas UI and associating that to user in Realm UI. In UserPermissions I have a list (the array “readable_contens”) of ids that are the id of the document I can read of the class Content.
I setted up the Sync to use _-pid as partition key. So, as the documentation says, I should be able to use something like this in the Sync rules:
{ "%%user.custom_data.readable_contents" : "%%partition" }
*in the read rules
Perfect! It should works. I have to insert an “or” in the rule to include also every content in the class UserData and Like the user own and I should be good (ok I have to write also the trigger for synching the likes to the “total likes” field in the corrispondent “Content” document.
So at the end should be something like this
{ "%OR" : { "%%user.custom_data.readable_contents" : "%%partition", "%%user.id" : "%%partition", } }
*in the read rules
I don’t need even a function for this. But here is the big problem. When I went back to my react-native app to see this:
sync: {
user: user,
partitionValue: **AND NOW?***,
}
Now that I use a rule like that on the Realm Sync panel, what partition value should I use? It seems it gives an error if I omit that but it accept only “numbers, strings and objectId” as value so I can’t use neither the user.id nor the user_custom_data.readable_contents (that is an array). Anyway I can’t think of what partitionValue rapresents in this Sync rules scenario.
What am I doing wrong?