Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 600
Firmansyah Avatar asked Dec 10 '25 00:12

Firmansyah


1 Answers

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',
  }
};
like image 60
Angular San Avatar answered Dec 11 '25 12:12

Angular San



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!