Typescript schema models for RN Expo

I can’t figure out how to do models for realm device sync even if my life depended on it!

I am using

npx create-expo-app MyAwesomeRealmApp --template @realm/expo-template

The docs and the example template give 3 different styles of defining models and none of them work. One of the objectives is to nest a GeoPoint object.

  1. The template has defined the models this way:
export class Task extends Realm.Object {
  _id: BSON.ObjectId = new BSON.ObjectId();
  description!: string;
  isComplete: boolean = false;
  createdAt: Date = new Date();
  userId!: string;

  static primaryKey = '_id';
}

the problem with this style is I can’t make it create a collection that’s named tasks instead the development mode creates a collection named Task. I am also unsuccessful in nesting a GeoPoint object. Also don’t see how to define an embedded object this way.

  1. The modeling suggestions from the app service are like this:
export type Task = {
  _id: Realm.BSON.ObjectId;
  createdAt: Date;
  description: string;
  isComplete: boolean;
  userId: string;
};
export const TaskSchema = {
  name: 'Task',
  properties: {
    _id: 'objectId',
    createdAt: 'date',
    description: 'string',
    isComplete: 'bool',
    userId: 'string',
  },
  primaryKey: '_id',
};

This fixes the collection name problem, but I still am unsuccessful at nesting a GeoPoint object. The Realm.List seems to be the problem, even tho I can create such an object it fails to insert it. Doesn’t even throw an error ( I have checked all the rules and so on).

  1. The app service device sync examples also show this way:
export class Dog extends Realm.Object<Dog> {
  _id!: BSON.ObjectId;
  name!: string;
  age!: number;
  static schema: ObjectSchema = {
    name: "Dog",
    primaryKey: "_id",
    properties: {
      _id: { type: "objectId", default: () => new BSON.ObjectId() },
      name: "string",
      age: "int",
    },
  };
}

I couldn’t even get the app to run with this style, it throws an error about how it’s not allowed to define a static schema property.

The bottom line is I would like to be able to create something like this:

export class GeoPoint extends Realm.Object {
    type: string = "point";
    coordinates: Realm.List<number>;
}

export class Task extends Realm.Object {
  _id: BSON.ObjectId = new BSON.ObjectId();
  description!: string;
  isComplete: boolean = false;
  createdAt: Date = new Date();
  userId!: string;
  point: GeoPoint;

  static primaryKey = '_id';
}

and be able to create, write, and sync Task objects. Configuring the name of the collection to be ‘tasks’ would also be nice. An example of how to use mixed dictionaries would also be nice.

@Andrew_Meyer , could you please help?

Hi,

To nest an object, you can refer to Embedded objects in MongoDB Realm Docs. However, once again you will see that it uses a different way of declaring the schema than in the starter app.

The starter app uses @realm/babel-plugin to simplify schema declaration for Realm objects and does not seem to fully support embedded objects properly. It’s experimental and does not seem to be high priority for the Mongo team. So, I would suggest to:

  1. Uninstall @realm/babel-plugin
  2. Remove the plugin accordingly from your babel config file
  3. Use the suggested schema declaration format that you see in App Services or the Docs.

I can share some models from my project where TripDestination is embedded in Trip and contains floating point values

Parent class — Trip

Embedded class — TripDestination (ignore the comments)

Note that in embedded objects, they are using name field to reference:
Another model from MongoDB

Hope this helps!

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.