Realm Relationships: From Mobile to MongoDB Atlas
I very much believe in the power of working together, no matter what tech you are learning or want to learn, if you like to work with me and our wider MongoDB Community together, scan the QR code or join us at community.mongodb.com/100daysofcode.
I am doing Realm today as Flexbox absolutely Flexboxed (read Fried) my brain yesterday.
So two days ago, I did Realm Relationships and explained three different types and how they are implemented on the client-side(mobile). Today I am trying to learn how to sync that schema from my mobile to MongoDB Atlas on the cloud which will allow me to sync my data to the cloud as well.
I have done basic setup steps before, so I won't be repeating them here, but if you are new to this, do check my article on “Getting Started with MongoDB Realm Sync..”
The steps that you need to follow before moving on
- Create a free M0 Cluster
- Create a Realm application. I named it BookLog.
- Enable Authentication Provider. (I enabled email/password)
The next step now is to Enable Sync. Turning Development mode On is the easiest win when syncing data from mobile to Atlas.
PartitionKey
This is an important key. This divides the data in Atlas into realms/partitions based on what value you assign to the key.
So I am making a BookLog Application, I need to think about how to model my data as this is an important step in any application.
You can give any name you like to partition key but the value is important as it divides your data based on that.
Permissions
Now, I need to decide permissions so that the user accesses only the data that is meant for it. Now, it's a BookLog application with information on authors and their books. I of course don't mind users reading all that information, but I would be careful what they save for themselves and not access other users' info. So I defined permissions as below.
I enabled Sync after defining permissions.
Part 02: Setting up Android Application
The next part is to link the Android application to Realm Cloud and implement login details in the app.
The steps to follow in the Android App
- Adding Realm dependencies
- Adding Realm app ID to Application subclass
- Implementing Login details (email/password as enabled in the previous step)
Now, Author and Book Model Classes are as below:
open class Author(
@PrimaryKey
var _id: ObjectId? = null,
var name: String? = null,
var books: RealmList<Book>? = RealmList()
): RealmObject() { }open class Book( @PrimaryKey
var _id: ObjectId? = null,
var name: String? = null,
var isRead: Boolean = false,
@LinkingObjects("books")
var authors: RealmResults<Author>? = null
) : RealmObject() {}
There is a slight difference in how the schema was in the previous blog.
I added field _id
in both the model classes. This is a mandatory primary key for MongoDB documents as that's how uniqueness is defined. A different primary key throws an error when the app is run and synced.
Will continue this tomorrow and implement BookDisplay Screen in the app and code for opening the Realm and syncing data.
You can refer to the current BookLog code on Github
With this, I finish with Day12/100DaysOfCode. Until tomorrow 👋