realm-js icon indicating copy to clipboard operation
realm-js copied to clipboard

Extend `Realm.open` convert a local Realm to a synced Realm

Open kraenhansen opened this issue 3 years ago • 4 comments

Problem

Once https://github.com/realm/realm-js/pull/4331 is merged our users need to add a lot of boilerplate code to use the feature:

const app = new Realm.App("awesome-app");
const user = await app.logIn(Realm.Credentials.anonymous());

const localConfig = {
  path: "local.realm",
  schema: [Person, Dog],
};

// Copies a bundled realm to "local.realm"
if (!Realm.exists(localConfig)) {
  Realm.copyBundledRealmFiles();
}

const localRealm = new Realm(localConfig);

const remoteConfig = {
  schema: [Person, Dog],
  sync: {
    user,
    partitionValue,
  },
};

if (Realm.exists(remoteConfig)) {
  return Realm.open(remoteConfig);
} else {
  localRealm.writeCopyTo(remoteConfig);
  localRealm.close();
  // Open the new remote Realm
  const syncedRealm = await Realm.open(remoteConfig);
  // Delete the old realm
  Realm.deleteFile(localConfig);
  return syncedRealm;
}

Solution

// Copies a bundled realm to "local.realm"
if (!Realm.exists({ path: "local.realm" })) {
  Realm.copyBundledRealmFiles();
}

// Automagically copies the local Realm into a synced Realm as it's opened.
const syncedRealm = await Realm.open({
  path: "local.realm",
  sync: {
    user,
    partitionValue,
  },
});

How important is this improvement for you?

Would be a major improvement

kraenhansen avatar Feb 11 '22 10:02 kraenhansen

@kraenhansen Do you have any update on this? This feature will be very helpful. Also, I have a question.

  1. Local Realm doesn't require any authorization and partition. So, after writing local to sync, we have to loop all local collections and should add partition? or any other approach we should use?

saravanakumargn avatar May 04 '22 11:05 saravanakumargn

Thanks for your comment, it helps us prioritise this.

Do you have any update on this?

No update. This far you're the only user asking for the improvement. It's on our backlog and prioritise it alongside a heap of other potential improvements.

So, after writing local to sync, we have to loop all local collections and should add partition? or any other approach we should use?

I'll defer to @fronck who implemented the PR initially.

kraenhansen avatar May 04 '22 12:05 kraenhansen

@saravanakumargn The partition value should be picked up from the output Configuration object when you convert something to a synced realm. Is that not what you experience with, e.g., @kraenhansen's example above?

fronck avatar May 05 '22 10:05 fronck

@fronck @kraenhansen I am trying to implement this in FindOurDevices App. Locale config

const localConfig = {
  schema: [
    Device.schema,
    Location.schema,
    Group.schema, 
    GroupMember.schema, 
    Location.schema
  ],
};
const localRealm = new Realm(localConfig);

When I sync I need an open scheme in the provider and need to use a different partitionValue as below in the code https://github.com/realm/FindOurDevices/blob/main/app/providers/DevicesProvider.js#L55 https://github.com/realm/FindOurDevices/blob/main/app/providers/GroupProvider.js#L45

But, when I use writeCopyTo The destination file cannot already exist. So, I should open all schemas without providers? then how i should use partitionValue?

saravanakumargn avatar Jul 05 '22 17:07 saravanakumargn