Realm-js: Request for import/export of data

Created on 14 Jan 2018  路  17Comments  路  Source: realm/realm-js

@realm:
Hi, is there a way to import and export realm data to React-native like json or csv?
It would be very important to make data backups.

T-Help

Most helpful comment

You can always do JSON.stringify(realm.objects('Person')) to export Person objects and all the objects they are linked to.

All 17 comments

I would suggest you copy the realm file instead, that would be much more efficient. Alternatively to use the realm-object-server, as it effectively only replicates changes.

@bmunkholm: Can you address me on some guide? Where do I usually find the realm file, on what path?

[Realm.path](https://realm.io/docs/javascript/latest/api/Realm.html#path) points you to your Realm file. Since Realm files aremmap`ed, it is important that a Realm is closed before copying or restoring it.

@kneth :
I do not understand what you mean by: Since Realm files aremmap`ed, it is important that to Realm is closed before copying or restoring it.
You could be clearer.
Do you give some examples of what you mean?

@bmunkholm: Have you ever done it, make a backup of the data on Realm?

No I never did, I would use realm-object-server for backup :-)

@Angelk90 Reading and writing files with mmap is different than reading and writing files with ordinary file methods.You can easily corrupt memory (the Realm instances) if you overwrite a mmaped file.

Kenneth is talking about restoring the file. In case you want to copy in a backup of a realm file, you need to ensure that the existing file that you want to overwrite is closed, i.e. no instance in your app has the file open.

So, can not I export my realm database? It is true or not ? @kneth @bmunkholm

You can always do JSON.stringify(realm.objects('Person')) to export Person objects and all the objects they are linked to.

Thank you @kneth

@kneth : What solution would you adopt?
@ozsirma : what solution are you adopting?

Exporting JSON is the solution for my problem.

If Realm Platform isn't an option, I would use JSON as an intermediate format.

@ozsirma : For how do you import?

@Angelk90 Converting JSON to JavaScript objects and insert them into a Realm?

@kneth :
Can I ask you some tips?

To save information use: react-native-fs
I do this:

exportPref(){
  return new Promise(async (resolve,reject) =>{
    try{
      var currentdate = new Date();
      var datetime = currentdate.getDate()+"-"+(currentdate.getMonth()+1)+"-"+currentdate.getFullYear()+"_"+
                     currentdate.getHours()+"-"+currentdate.getMinutes()+"-"+currentdate.getSeconds();
      RNFS.writeFile(RNFS.ExternalDirectoryPath+'/Name_'+datetime+'.txt',JSON.stringify(realm.objects('Library')),'utf8')
      .then((success) => {
        resolve("Save.");
      }).catch((err) => {
        resolve(err.message);
      });
    }catch(e){ resolve("Error."); }
  });
}

When I have to add a new element I do this:

addLibrary(id,title,poster_path,backdrop_path,type){
  try{
    realm.write(() => {
      realm.create('Library',{id:id,title:title,poster_path:poster_path,backdrop_path:backdrop_path,type:type});
    });
    return true;
  }catch(e){ return false; }
}

What you are advising me, and to do a for loop for each element and recall the function by passing the data or exists in "Realm" a way by passing all the file.txt to add it to the library?

@Angelk90 I think you will get a much better advice if you ask the question at Stack Overflow.

Was this page helpful?
0 / 5 - 0 ratings