简体   繁体   English

将数据从mongoDB同步到firebase,反之亦然

[英]sync data from mongoDB to firebase and vice-versa

My current situation: 我目前的情况:

I have created an application using React, NodeJS and Electron. 我使用React,NodeJS和Electron创建了一个应用程序。 Most of the users are a kind of offline users. 大多数用户都是一种离线用户。 They use my application offline. 他们离线使用我的应用程序

Next plans: 下一步计划:

Now, I am planning to create a mobile application for them. 现在,我打算为他们创建一个移动应用程序。 I plan to create that application using React-Native. 我计划使用React-Native创建该应用程序。

Since their database is offline, I planned to give them a sync to firebase button in desktop application. 由于他们的数据库处于脱机状态,我计划在桌面应用程序中为他们提供sync to firebase buttonsync to firebase button When he clicks on sync to firebase button , the data in their local mongodb should syncronize with firebase. 当他点击sync to firebase button ,他们本地mongodb中的数据应该与firebase同步。

My thoughts: 我的想法:

  • when a new record is added to mongodb, I will store a new key with that record which will look like: new: true . 当一条新记录被添加到mongodb时,我将存储一条带有该记录的新密钥,如下所示: new: true
  • when a record is updated I will store a key named updated: true 当记录更新时,我将存储名为updated: true的密钥
  • similarly for delete... 类似的删除...

And then when user presses Sync to firebase , I will search for those records and add/update/delete respective records on firebase and then I will remove those keys from mongodb database. 然后当用户按下Sync to firebase ,我将搜索这些记录并在firebase上添加/更新/删除相应的记录,然后我将从mongodb数据库中删除这些键。

Problems in executing my thoughts: 执行思路时遇到的问题:

At first it does not smell me a good thing as I think it is time consuming because I will perform operations on firebase as well as mongodb. 起初它并没有让我觉得好事,因为我觉得这很费时间,因为我将在firebase和mongodb上进行操作。

Another problem with this approach is that if I think the other way round, that when user add/update/delete a record from React-Native app, firebase will have those keys line new/updated/deleted and then when user presses sync button in desktop application, I will have to do same thing but in reverse. 这种方法的另一个问题是,如果我认为相反,当用户从React-Native应用程序添加/更新/删除记录时,firebase会将这些键行换成新/更新/删除,然后当用户按下同步按钮时桌面应用程序,我将不得不做同样的事情,但相反。

Yet another problem is that if user accidently uninstalled my application and then reinstalls it, then what should I do? 另一个问题是,如果用户意外卸载了我的应用程序然后重新安装它,那我该怎么办?

And the biggest problem is managing all the things. 最大的问题是管理所有事情。

My Expectations: 我的期望:

So, I want a clean and maintainable approach. 所以,我想要一个干净,可维护的方法。 Does any one have any idea on how to sync data from mongodb to firebase and vice-versa? 有没有人知道如何将数据从mongodb同步到firebase,反之亦然?

Both database systems supports for some sort of operation log or trigger system. 两个数据库系统都支持某种操作日志或触发系统。 You can use these to live update changes to databases to sync them almost real time. 您可以使用这些对数据库进行实时更新更改,以便几乎实时同步它们。

For MongoDB 对于MongoDB

You can use Oplog to see what changes made to database (insert/update/delete) and run a suitable function to sync firebase. 您可以使用Oplog查看对数据库所做的更改(插入/更新/删除)并运行适当的函数来同步firebase。

oplog OPLOG

A capped collection that stores an ordered history of logical writes to a MongoDB database. 一个上限集合,用于存储对MongoDB数据库的逻辑写入的有序历史记录。 The oplog is the basic mechanism enabling replication in MongoDB. oplog是在MongoDB中启用复制的基本机制。

There are small libraries that help you easily subscribe to these events. 有一些小型库可以帮助您轻松订阅这些事件。

Example ( mongo-oplog ) 示例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);
});

For Firebase 适用于Firebase

You can use Cloud Functions . 您可以使用云功能 With Cloud Functions you can watch triggers like Cloud Firestore Triggers or Realtime Database Triggers and run a function to sync MongoDB database. 使用云功能,您可以查看Cloud Firestore触发器实时数据库触发器触发器,并运行同步MongoDB数据库的功能。

With Cloud Functions, you can handle events in the Firebase Realtime Database with no need to update client code. 借助云功能,您可以处理Firebase实时数据库中的事件,而无需更新客户端代码。 Cloud Functions lets you run database operations with full administrative privileges, and ensures that each change to the database is processed individually. 云功能允许您以完全管理权限运行数据库操作,并确保单独处理对数据库的每个更改。

// 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);
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM