Search code examples
node.jsrealmrealm-mobile-platformrealm-list

How to write a RealmList in a node realm schema


I have a schema for OrderEntry which has a RealmList of another realm object ItemEntry:

export const OrderEntrySchema: Realm.ObjectSchema = {
 name: 'OrderEntry',
 primaryKey: '_id',
 properties: {
  ...
  items: ???
 }
}

export const ItemEntrySchema: Realm.ObjectSchema = {
 name: 'ItemEntry',
 primaryKey: 'id',
 properties: {
  ...
  }
}

How to define item as a RealmList<ItemEntry> as can be done in android?

Note: Both schemas are in separate files.


Solution

  • You can specify a list of ItemEntry like this

    items: {type: 'list', objectType: 'ItemEntry'}
    

    Here is an example with your schemas.

    const OrderEntrySchema = {
        name: 'OrderEntry',
        primaryKey: 'id',
        properties: {
            id: 'string',
            items: {type: 'list', objectType: 'ItemEntry'}
        }
    };
    
    const ItemEntrySchema = {
        name: 'ItemEntry',
        primaryKey: 'id',
        properties: {
            id: 'string'
        }
    };
    

    If you want to put them in separate files, you will need to import ItemEntry in the file with OrderEntry