Sync data from mongoDB to firebase and vice versa

Current situation:

I created an application using React, NodeJS, and Electron. Most users are a kind of offline user. They use my application offline.

The following plans:

Now I plan to create a mobile application for them. I plan to create this application using React-Native.

Since their database is disabled, I planned to give them sync to firebase buttonin a desktop application. When it clicks sync to firebase button, the data in their local mongodb should synchronize with firebase.

My thoughts:

  • when a new entry is added to mongodb, I will keep the new key with this entry, which will be as follows: new: true.
  • When updating the record, I will save the key with the name updated: true
  • similarly for deleting ...

And then, when the user clicks Sync to firebase, I will search for these entries and add / update / delete the corresponding entries in firebase, and then I will remove these keys from the mongodb database.

Problems fulfilling my thoughts:

It doesn't smell so good at first, because I think it takes a lot of time, because I will perform operations on firebase, as well as with mongodb.

Another problem with this approach is that if I think the other way around, when a user adds / updates / deletes a record from a React-Native application, firebase will have these lines of lines new / updated / deleted, and then when the user clicks sync in the desktop application, I have to do the same, but vice versa.

, , , ?

.

:

, . - , mongodb firebase ?

+4
1

- . , .

MongoDB

Oplog, , (//) firebase.

oplog

, MongoDB. - , MongoDB.

, .

(mongo-oplog)

import MongoOplog from 'mongo-oplog'
const oplog = MongoOplog('mongodb://127.0.0.1:27017/local', { ns: 'test.posts' })

oplog.tail();

oplog.on('op', data => {
  console.log(data);
});

oplog.on('insert', doc => {
  console.log(doc);
});

oplog.on('update', doc => {
  console.log(doc);
});

oplog.on('delete', doc => {
  console.log(doc.o._id);
});

Firebase

. , Firestore MongoDB.

Firebase Realtime . , .

// Listens for new messages added to /messages/:pushId/original and creates an
// uppercase version of the message to /messages/:pushId/uppercase
exports.makeUppercase = functions.database.ref('/messages/{pushId}/original').onWrite((event) => {
  // Grab the current value of what was written to the Realtime Database.
  const original = event.data.val();
  console.log('Uppercasing', event.params.pushId, original);
  const uppercase = original.toUpperCase();
  // You must return a Promise when performing asynchronous tasks inside a Functions such as
  // writing to the Firebase Realtime Database.
  // Setting an "uppercase" sibling in the Realtime Database returns a Promise.
  return event.data.ref.parent.child('uppercase').set(uppercase);
});
+1

Source: https://habr.com/ru/post/1695259/


All Articles