We had a production database that was compromised and need to move users to a new database. How can we take a current users, who is authenticated and connected to old Realm database, and sync their data with the new database?
I have tried following the following recommendations, but haven’t had success.
So if anyone else runs into this question/issue, here is what I did to resolve it:
Check to see if the user’s current Realm is not the same as the Realm you would like them to be in. if (!currentRealmInstance.syncSession.url.startsWith(desiredRealmURL))
If not, logged the user into the new Realm server. We use JWT, so this involved creating the credentials then logging in. const user = await Realm.Sync.User.login(ServerURL, credentials);
Opened a new instance of Realm with this user. const newRealm = new Realm(realmConfig);. We also are keeping a reference of the old Realm.
Copy the data over into the new Realm. We used an array of the Realm schema objects we wanted to copy over. In this code example it is just the User data.
const promises = ['User'].map(obj =>
const data = oldRealm.objects(schema);
if (data.length) {
data.forEach(el => {
newRealm.write(() => {
newRealm.create(schema, el, 'modified');
});
});
}
);
After all the data has copied over we saved the Realm instance.