List with multiple Models JavaScript?

Hi! I was wondering if a list can hold references to multiple models/schemas?

Nope, I think it’s not possible.
As a workaround, create a list of some ids instead of references, e.g. GUIDs and maintain separate objects with primary id (GUID). Then access this objects by realm.objectForPrimaryKey().

An alternative solution is to use composition: One model schema with multiple properties (one of each possible type).

Let’s say you want to make a list of Person or Robot, you could make a helper object schema called Humanoid with two properties, person and robot.

Makes sense?

2 Likes

I’m currently doing the former mentioned, from a performance aspect is your solution better?

It should be faster to follow links over looking up objects by primary keys, as it’s practically just following a pointer instead of looking up a row in the table index. Personally I also like that you can filter on properties of the Robot and Person using the Realm query language. This is harder if you first need to grab a list of ids, translate them to objects from the database and then manually filter out objects from the list of objects.

That being said: If you’ve got it working now and you don’t experience neither a performance nor code maintenance issue, it might not be worth changing what ain’t broken.

1 Like

Interesting I haven’t implemented my approach fully so i’ll look into trying your’s. Lastly my data model is Collections, Folders, and Checklists, which are all arrays of objects with the required props, id & parentID. I convert the arrays to maps adding the children field and merge them into a dictionary map. I loop over the checklists array and use the dictionary to look up the parent and add the child id to it children and go up the tree until there’s no parentID on the parent. As you know I’m doing the string approach with your approach do I have to worry about object duplication? Memory usage is a concern for our app. @kraenhansen

I don’t think I understand your use-case enough to answer your question.

Is it important for you to be able to control the order of children? If not, a reference to a parent object could be an alternative solution. If you need all three types to be able to be children of each other a helping Parent schema with three optional properties (one for each possible parent type) could be a solution.

So in our file system we have collections at the highest level which have folders and checklist within them. The folders hold checklist and all entities have one parent, with collections having none. The children are all entities that have the same parentGuid to id of the parent. My concern is if I create a model with type FileNode and give it list properties of Folder and Checklist. Collections is not needed as a prop on it since it’s top level and can’t be a child. Is there a way to save all my Collections, Folders, and Checklists once and reference the object in my children instead of duplicating. I want my entities in a flat list so I can fetch all checklists if needed and vice versa.