Firmansyah
Firmansyah

Reputation: 166

Is To-One Relationships in Realm JavaScript only for one schema?

I am trying to insert my nested object to Realm with To-One Relationships method, but I got an unexpected result where all value of my nested object is the same thing as the value from the first of my nested object that has been Relationship

This is my schema looks like

const PhotoSchema = {
  name: 'CUSTOMER_PHOTOS',
  properties: {
    base64: 'string'
  }
};

const TimeSchema = {
  name: 'CUSTOMER_TIMES',
  properties: {
    warranty: 'float',
    finish: 'float'
  }
};

const MainSchema = {
  name: 'CUSTOMERS',
  primaryKey: 'id',
  properties: {
    id: 'int',
    name: 'string',
    photo: {type: 'CUSTOMER_PHOTOS'},
    time: {type: 'CUSTOMER_TIMES'},
  }
};

And try to insert some data like this

import Realm from 'realm';

Realm.open({
  path: 'mydb.realm',
  schema: [PhotoSchema, TimeSchema, MainSchema]
})
.then((realm) => {

  realm.write(() => {
    realm.create('CUSTOMERS', {
      id: Date.now(),
      name: 'John',
      photo: {
        base64: 'ImageBase64'
      },
      time: {
        warranty: 31,
        finish: 7
      }
    })
  })

})
.catch((error) => {
  console.error(error)
});

The process of inserting data is successfully BUT I got unexpected result when successfully get that data from Realm

Unexpected Result in console.log()

{
  id: 1601335000882,
  name: "John",
  photo: {
    base64: "ImageBase64"
  },
  // This value is the same as PhotoSchema
  time: {
    base64: "ImageBase64"
  }
}

I want to the actual result like this

{
  id: 1601335000882,
  name: "John",
  photo: {
    base64: "ImageBase64"
  },
  time: {
    warranty: 21
    finish: 7
  }
}

I there anything wrong with my code? The Documentation is not too detail about the method, the explanation and the example is just like one word

UPDATE:

I got an unexpected result only in the console.log() and if I try to access the property directly like MY_DATA.time.warranty the result is what I expected

The Answer is: No

To-One Relationships method is not only for one Schema, and Thanks to Angular San for showing an example of Inverse Relationships method.

Upvotes: 3

Views: 1408

Answers (1)

Angular San
Angular San

Reputation: 356

Try Inverse Relationships

I got an expected result with Inverse Relationships method. In this method, you have to add one property that connected to Main Schema and I want to call this a combiner property

const PhotoSchema = {
  name: 'CUSTOMER_PHOTOS',
  properties: {
    base64: 'string',
    combiner: {type: 'linkingObjects', objectType: 'CUSTOMERS', property: 'photo'}
  }
};

const TimeSchema = {
  name: 'CUSTOMER_TIMES',
  properties: {
    warranty: 'float',
    finish: 'float',
    combiner: {type: 'linkingObjects', objectType: 'CUSTOMERS', property: 'time'}
  }
};

const MainSchema = {
  name: 'CUSTOMERS',
  primaryKey: 'id',
  properties: {
    id: 'int',
    name: 'string',
    photo: 'CUSTOMER_PHOTOS',
    time: 'CUSTOMER_TIMES',
  }
};

Upvotes: 2

Related Questions