Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Upsert embedded objects when updating parent with modified #6932

Open
tehvgg opened this issue Nov 13, 2024 · 1 comment
Open

Upsert embedded objects when updating parent with modified #6932

tehvgg opened this issue Nov 13, 2024 · 1 comment

Comments

@tehvgg
Copy link

tehvgg commented Nov 13, 2024

Problem

I have a primary object that contains an embedded object. I receive updates from the server and would like to simply call realm.create(MyObj, data, Realm.UpdateMode.Modified).

However, these payloads are large and while we receive them in full during initialization, updates often contain partials of the embedded objects (I do not have control over the server data or how it is provided).

Currently, when upserting the parent, the embedded children are re-created rather than themselves being upserted.

I'm wondering if there's some way for me to stick with a 1-line upsert to update my db, or if I'm going to have to write a bunch of manual updates for the embedded objects.

// objects
class MyObj extends Realm.Object<MyObj> {
  id: string;
  emb: MyEmb;

  static schema: Realm.ObjectSchema = {
    name: 'MyObj',
    primaryKey: 'id',
    properties: {
      id: 'string',
      emb: 'MyEmb',
    },
  }
}

class MyEmb extends Realm.Object<MyEmb> {
  str: string;
  boo: boolean;

  static schema: Realm.ObjectSchema = {
    name: 'MyEmb',
    embedded: true,
    properties: {
      str: 'string',
      boo: 'bool',
    },
  }
}

// init
realm.create(
  Primary,
  {
    id: 'id-1',
    emb: {
      str: 'a',
      boo: false,
    },
  },
  Realm.UpdateMode.Modified,
);

// update fail, MyEmb missing 'boo'
realm.create(
  Primary,
  {
    id: 'some-guid-1234',
    emb: {
      str: 'A',
    },
  },
  Realm.UpdateMode.Modified,
);

Solution

No response

Alternatives

  1. Manually handle updates in the form of obj.emb.str = data.emb.str, however given the scale of the data (many many many primary objects that contain embedded objects), I'd really prefer just having a one line upsert if possible.
  2. Convert embedded objects to primary, however many of these nested objects don't have properties that could be used as a primary key.

How important is this improvement for you?

Would be a major improvement

Feature would mainly be used with

Local Database only

Copy link

sync-by-unito bot commented Nov 13, 2024

➤ PM Bot commented:

Jira ticket: RJS-2920

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant