Opening Realm with Multiple Schema Classes

Hello - I am referencing this page here: https://docs.mongodb.com/realm/sdk/node/examples/define-a-realm-object-model/#define-realm-object-types-with-javascript-classes

I have no issue opening a Realm with a singular TypeScript class containing a schema definition. However, referencing later in that article: https://docs.mongodb.com/realm/sdk/node/examples/define-a-realm-object-model/#define-an-inverse-relationship-property

If I define two or more classes (in separate .ts files), how can I get that whole schema into the Realm.Configuration object with which I open the realm?

Thank you,
Sam

Hey Sam!

The Configuration.schema property takes an array of object models so you would just add any additional classes or object schemas to the array. You can pull them in from separate files using standard module import syntax.

See below for an example and please let us know if you still have issues or questions!

// src/user.ts
import { Task } from "./task"

export class User {
  public _id: ObjectId = "";
  public _partition: string = "";
  public name: string = "";
  public tasks: Realm.List<Task>;
  public static schema: Realm.ObjectSchema = {
    name: "User",
    primaryKey: "_id",
    properties: {
      _id: "objectId",
      _partition: "string",
      name: "string",
      tasks: "Task[]"
    }
  };
}

// src/task.ts
import { User } from "./user"

export class Task {
  public _id: ObjectId = "";
  public _partition: string = "";
  public text: string;
  public assignee: Realm.Results<User>;
  public static schema: Realm.ObjectSchema = {
    name: "Task",
    primaryKey: "_id",
    properties: {
      _id: "objectId",
      _partition: "string",
      text: "string",
      assignee: {
        type: 'linkingObjects',
        objectType: 'User',
        property: 'tasks'
      }
    }
  };
};

// src/main.ts
import { User } from "./user"
import { Task } from "./task"

export async function main() {
  const realm = await Realm.open({
    schema: [User, Task],
    // ... the rest of your config
  });
}
1 Like

Thanks, Nick! Here’s a related follow up:

Do you recommend opening one big synced realm with all of the object types, or opening one realm per object type, like is done here: Building a Mobile Chat App Using Realm – Data Architecture | MongoDB

If I have 100 object types and I’m always applying the same partition key to all of them, I would think it would be better to just open one realm. Thanks again!

1 Like

I can’t really recommend one way or the other generically - it’s highly dependent on your application and data model.

You’re spot on! A partition is essentially the same thing as a synced realm so you’d use a single realm for every object that has the same partition key. For Configuration.schema you can specify all of the object types in the partition or only include a subset - only the types you specify in your config get synced.

1 Like

Just a follow-on here from one of Nick’s teammates: it’s easy to fall into the trap of thinking in terms of SQL databases and tables… and ending up with a realm per object type. That’s not necessarily a bad design (if you’re writing a really big application where one object type lives on each screen, it might be a good idea to break up the objects into separate realms just to reduce the overhead of opening each realm/sync time/make the app more responsive), but if your app has a separate partitioning scheme that works across object types and performance doesn’t seem to be a problem, embrace it and create bigger realms that contain multiple types!

That’s part of why we call them “realms” and not “tables.” :grinning_face_with_smiling_eyes:

2 Likes

Thanks @nlarew and @Nathan_Contino - this is great info! I do wish there was a section in the docs that had some more general design suggestions and considerations like this. Thanks again!

2 Likes