作者:中国有程序猿 | 来源:互联网 | 2023-05-19 05:46
Mycurrentsituation:我目前的情况:IhavecreatedanapplicationusingReact,NodeJSandElectron.Most
My current situation:
我目前的情况:
I have created an application using React, NodeJS and Electron. Most of the users are a kind of offline users. They use my application offline.
我使用React,NodeJS和Electron创建了一个应用程序。大多数用户都是一种离线用户。他们离线使用我的应用程序
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. When he clicks on sync to firebase button
, the data in their local mongodb should syncronize with firebase.
由于他们的数据库处于脱机状态,我计划在桌面应用程序中为他们提供firebase按钮的同步。当他点击同步到firebase按钮时,他们本地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,反之亦然?
1 个解决方案
1
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
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. The oplog is the basic mechanism enabling replication in MongoDB.
一个上限集合,用于存储对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
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. Cloud Functions lets you run database operations with full administrative privileges, and ensures that each change to the database is processed individually.
借助云功能,您可以处理Firebase实时数据库中的事件,而无需更新客户端代码。云功能允许您以完全管理权限运行数据库操作,并确保单独处理对数据库的每个更改。
// 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);
});