Hi everyone, I’m very confused about how to define schemas when writing in TypeScript (specifically React Native), especially how to write schemas such that embedded objects and arrays work in a Realm database.
How Should I Define Schemas?
The Node.js SDK and React Native SDK say there are two options for defining a realm object model:
JavaScript objects
const Car = {
  name: "Car",
  properties: {
    _id: "objectId",
    make: "string",
    model: "string",
    miles: "int?",
  },
};
JavaScript Classes
class Car extends Realm.Object {
  static schema = {
    name: "Car",
    properties: {
      _id: { type: 'objectId', default: () => new Realm.BSON.ObjectId() },
      make: "string",
      model: "string",
      miles: "int?",
    },
    primaryKey: '_id',
  };
}
- When should I define a Realm object using JavaScript classes/objects?
- When do I need to define a static schemaortype?
- When should I use a generatefunction vs passing in an object to theRealm.createfunction?
- How does one incorporate all of this with TypeScript?
Mapping Objects/Classes to Schemas
Then there’s a completely separate method of defining schemas, outlined in the Realm React documentation.
If one defines a Realm object like so:
type Label = {
  name: string;
  color: string;
}
export class Task extends Realm.Object<Task> {
  _id: Realm.BSON.UUID;
  userId!: string;
  name!: string;
  description: string;
  createdAt: Date = new Date();
  labels: Label[];
  static primaryKey = '_id';
How does this map to the schema defined in the Realm/Atlas schema UI, which uses JSON?
(Also note that, for me, Label[] does not work.)
Embedded Arrays
When working with embedded objects and arrays—trying to write non-primitives to my database—I run into a situation where the objects and arrays are simply ignored in the realm.write operation.
For example, suppose I have the following object:
const Task: Task = {
  name: 'Example Task',
  description: 'This is an example',
  createdAt: new Date(),
  author: {
    id: 'some_uuid',
    name: 'Author Name',
    authorIconUrl: 'some_url'
  },
  labels: [
     {
        'name': 'Important',
        'color': 'Red'
     },
     {
        'name': 'Family',
        'color': 'Gold'
     },
  ]
}
When I tried writing this in the past, the resultant “Task” in the database would contain everything except for the author and labels properties; it’s as though the realm.write function catches that author and labels are objects and ignores them without any warning/error message/feedback.
I changed my schema implementation (I honestly have no idea what I did, hence the questions about schema declarations above), and magically author and the embedded object showed up. However, the behavior with the array (realm.write quietly ignores it) still happens, and I don’t know how to solve this issue.
Please help! I think I have a barely functional knowledge of realm objects, classes, and schemas, especially in the context of TypeScript, and as such I have no idea how to write embedded objects and arrays of objects to Realm database documents.
- How does one get embedded objects to work in React Native with TypeScript?
- How does one write arrays of objects to documents in a Realm database in React Native with TypeScript?
TypeScript
Error:
Classes extending Realm.Object cannot define their own `schema` static, all properties must be defined using TypeScript syntax
Well… 

 . And assuming you want to create two CarOwners who own the same Car.
. And assuming you want to create two CarOwners who own the same Car.