tamy
tamy

Reputation: 125

What is a realm?

I'm looking into using Realm in a React Native app and am having trouble grasping the concept of a realm, the realm object server, and how to design my database.

Lets say we have three tables:

"User" with columns id (primary key), name (string)
"Task" with columns id (primary key), user_id (foreign key to User), description (string)
"Friendships" with columns id (primary key), user_id (foreign key to User), friend_user_id (foreign key to User)

Users can make tasks for themselves and see their friend's tasks but can only edit and delete their own. All these users and tasks will be created on their mobile device and synced to a server.

Lets say user1 wants to see all the tasks for his friends.

Coming from a regular sql background, I'd imagine the javascript pseudocode would be something like this:

var friends_list = [user2.id, user3.id, ...];
let realm = Realm.URL("...server url...");
let friends_tasks = realm.objects('Tasks').filtered('user_id in friends_list');

But if we only have one realm, then in order for user's to be able to make their own tasks, they need write access to the realm and thus would also have permission to delete other user's tasks.

Looking at the Realm docs, and specifically about multiple realms and access control.

A solution would be for each user to make a shared realm on their mobile device. Each user's realm will get synced with the object server. And the user's friends will get read access to the realm.

So for user1 to see all his friend's tasks. The pseudocode will be something like:

var friends_list = [user2.id, user3.id, ...];
var friends_tasks = [];
foreach (user_id in friends_list) {
  let realm = Realm.URL("...server_url/user_id/...");
  let tasks = realm.objects("Tasks");
  friends_tasks.push(tasks);
}

Is this the best design? This just feels wrong having to connect to separate realms.

Upvotes: 1

Views: 1173

Answers (1)

geisshirt
geisshirt

Reputation: 2497

Realm is an object database and not a relational database. That means you don't have to map your objects to rows in tables but you can store your objects directly in your database. It is described in general terms here.

An object database like Realm is using classes as data model, and in your case, you will need one for User and one for Task. A user's task can be modelled as a relationship instead of using primary/foreign keys and joins in a relational database. Likewise, relationships can be used to model friendship between two users.

Realm can be a local database, or you can connect to a Realm Object Server and synchronize your objects with your backend infrastructure or other mobile devices.

Upvotes: 1

Related Questions