Migration is required due to fields made optionals (but how to migrate?)


I’m using the node-js integration form Realm and I made two fields of my schema optionals.
Now I have the following error when I try to open a Realm instance:

Error: Migration is required due to the following errors:
- Property 'File.uploaded' has been made optional.
- Property 'File.status' has been made optional.
    at Realm.getSharedRealm``

But I can't find any way to actually use the onMigration callback to do this? Do you have any idea? Because all the examples in the documentation are bout the actual data and not how to make the properties required/optional.
And in the node integration there is no RealmObjectSchema.setNullable method like in the Java one for example.

Here is my code:

const REALM_CONFIG: Realm.Configuration = {
path: ${app.getPath('userData')}/data/dev.realm,
schemaVersion: 2,
onMigration: (oldRealm) => {
if (oldRealm.schemaVersion < 2) {
// ?

Realm.open({ …REALM_CONFIG, schema: [schema] }).then((realm) => {```

Thanks if you can help me.

I believe in this case it’s considered a destructive change so you’ll need to copy the data from the old object property to the new one

if(oldRealm.schemaVersion < 2) {
   const oldObjects = oldRealm.objects('File');
   const newObjects = newRealm.objects('File');

   // iterate over old objects and copy the old property value to the new property value
   for (const objectIndex in oldObjects) {
      const oldObject = oldObjects[objectIndex];
      const newObject = newObjects[objectIndex];
      newObject.uploaded = oldObject.uploaded
      newObject.status = oldObject.status

Let me know the result

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.