How should I manage relations?

Hi,
I’m creating an Rubiks Cube TImer app. This app will have multiple users, from which each one will need to save his own Results and Sessions.

Result will consist of actual result time, scramble algorithm and so on.

Session will hold up a group of Results.

User will never query for each particular solve, instead he will always need to take whole Session with all solves that are connected to this Session.

How should I make a schema for that?
For now I’ve made Result schema:

export class Result extends Realm.Object<Result> {
    _id: Realm.BSON.ObjectId = new Realm.BSON.ObjectId();
    createdAt: Date = new Date();
    time!: number; // in ms
    scramble!: string;
    owner_id!: string;

    static primaryKey = '_id';
}

Here comes real question. How should I connect sessions with results? Sessions/Results needs to be tied to only one user. In MongoDB in Node.js I know how to make this happen. Here I’m not really sure.

Should I create session and just make an array of results in there embedded?
Or should I create session, and each result whilst creating should be bound to Session by some flag in Schema?

The question is a little vague.

Conceptual questions are often very difficult to answer or address because without understanding the entire use case, we may send you down the wrong path - only you know about the app concept. Because of that, it’s often better to post specific coding questions rather than app design questions. But, let me see if we can help a bit.

I see the Result class but what is a Session and a solve as mentioned in the question, do you have a proposed model you can post?

If a solve is specific to one Session, never shared with any other Sessions then making it an embedded object would make sense, since it doesn’t need to be a separately managed object.

I would be careful with that naming; Realm has a Results object and that ends up being a naming collision and cause confusing code. Perhaps naming it something else may be a good idea.

Ok, but when you say ‘tied to’ what does that mean exactly. Meaning it doesn’t need to be managed or queried?

Try to address the above and we’ll take a look.

Ok, I will try to explain it better. There is only(changed that) two Schemas: Solve and Session.

Each user will create their own sessions(probably like 10-15 per user), and each Solve needs to be connected to one Session.

So one Session can have multiple Solves.

App will almost always query for all Solves that are connected with certain Session.

Sometimes there is need to edit certain Solve.

That’s why I thought that I should embed Solves into Session. Is there any example Schema for that? In DOC’s there is only example where I need to manually specify Schema in Typescript, which now is not mandatory, since babel plugin does that for us.

It sounds to me like you’re well on your way.

At a high level, theres a session with a List of solves that are related to the session

The code in the docs is almost exactly that with a User having a list of Posts (akin to Session with a list of Solves)

In your case, since the Solves are very specific to a Session and will never be shared with other Sessions and it doesn’t sound like a large amount of data in each Solve, they could easily be embedded objects.

See One-to-Many relationship

I only mention the amount of data as there is a limit on the size of a property, and if the data is going to get lengthy or large, it may be better to have the solves as separately managed objects. But it doesn’t sound like that’s needed.

1 Like