热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

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

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


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

推荐阅读
  • 本文介绍了Java工具类库Hutool,该工具包封装了对文件、流、加密解密、转码、正则、线程、XML等JDK方法的封装,并提供了各种Util工具类。同时,还介绍了Hutool的组件,包括动态代理、布隆过滤、缓存、定时任务等功能。该工具包可以简化Java代码,提高开发效率。 ... [详细]
  • 从零基础到精通的前台学习路线
    随着互联网的发展,前台开发工程师成为市场上非常抢手的人才。本文介绍了从零基础到精通前台开发的学习路线,包括学习HTML、CSS、JavaScript等基础知识和常用工具的使用。通过循序渐进的学习,可以掌握前台开发的基本技能,并有能力找到一份月薪8000以上的工作。 ... [详细]
  • PatchODAX8: ... [详细]
  • 现在比较流行使用静态网站生成器来搭建网站,博客产品着陆页微信转发页面等。但每次都需要对服务器进行配置,也是一个重复但繁琐的工作。使用DockerWeb,只需5分钟就能搭建一个基于D ... [详细]
  • JavaScript和Python是用于构建各种应用程序的两种有影响力的编程语言。尽管JavaScript多年来一直是占主导地位的编程语言,但Python的迅猛发展有 ... [详细]
  • 基于PgpoolII的PostgreSQL集群安装与配置教程
    本文介绍了基于PgpoolII的PostgreSQL集群的安装与配置教程。Pgpool-II是一个位于PostgreSQL服务器和PostgreSQL数据库客户端之间的中间件,提供了连接池、复制、负载均衡、缓存、看门狗、限制链接等功能,可以用于搭建高可用的PostgreSQL集群。文章详细介绍了通过yum安装Pgpool-II的步骤,并提供了相关的官方参考地址。 ... [详细]
  • Linux服务器密码过期策略、登录次数限制、私钥登录等配置方法
    本文介绍了在Linux服务器上进行密码过期策略、登录次数限制、私钥登录等配置的方法。通过修改配置文件中的参数,可以设置密码的有效期、最小间隔时间、最小长度,并在密码过期前进行提示。同时还介绍了如何进行公钥登录和修改默认账户用户名的操作。详细步骤和注意事项可参考本文内容。 ... [详细]
  • ALTERTABLE通过更改、添加、除去列和约束,或者通过启用或禁用约束和触发器来更改表的定义。语法ALTERTABLEtable{[ALTERCOLUMNcolu ... [详细]
  • 原文地址http://balau82.wordpress.com/2010/02/28/hello-world-for-bare-metal-arm-using-qemu/最开始时 ... [详细]
  • hadoop1.2.1文档中这样写:Nowcheckthatyoucansshtothelocalhostwithoutapassphrase:$sshlocalhostIfyou ... [详细]
  • quartus管脚分配后需要保存吗_嵌入式必须会的一些硬件面试题,要试一试吗?你过来呀!...
    1、下面是一些基本的数字电路知识问题,请简要回答之。(1)什么是Setup和Hold时间?答:SetupHoldTime用于测试芯片对输入 ... [详细]
  • docker安装到基本使用
    记录docker概念,安装及入门日常使用Docker安装查看官方文档,在"Debian上安装Docker",其他平台在"这里查 ... [详细]
  • 加密、解密、揭秘
    谈PHP中信息加密技术同样是一道面试答错的问题,面试官问我非对称加密算法中有哪些经典的算法?当时我愣了一下,因为我把非对称加密与单项散列加 ... [详细]
  • 仅以博客形式记录linux所学,不足之处继续优化linux系统的常用命令格式基本上是以cmd(command)选项参数的形式书写例如screen、date、ifconfig等命令1 ... [详细]
  • MybatisPlus入门系列(13) MybatisPlus之自定义ID生成器
    数据库ID生成策略在数据库表设计时,主键ID是必不可少的字段,如何优雅的设计数据库ID,适应当前业务场景,需要根据需求选取 ... [详细]
author-avatar
中国有程序猿
这个家伙很懒,什么也没留下!
Tags | 热门标签
RankList | 热门文章
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有