Html 将本地 HTML5 数据库(WebSQL 存储、SQLite)与服务器同步的最佳方式(2 路同步)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1744522/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 01:20:21  来源:igfitidea点击:

Best way to synchronize local HTML5 DB (WebSQL Storage, SQLite) with a server (2 way sync)

htmlweb-applicationsstoragedata-synchronization

提问by Samuel

I am developing a mobile web application (for iPhone & Android) with a local database (using html5 webstorage) so my app is still usable when the user is offline.

我正在开发一个带有本地数据库(使用 html5 webstorage)的移动网络应用程序(适用于 iPhone 和 Android),因此当用户离线时我的应用程序仍然可用。

This is working perfectly, but I want to save the local data on a server. So I need to synchronize the local DB with a DB on a server. The synchronisation can only be one way, but in the future, I would like to synchronize it in the both way (server <-> local DB).

这工作完美,但我想将本地数据保存在服务器上。所以我需要将本地数据库与服务器上的数据库同步。同步只能是一种方式,但将来,我想以两种方式同步它(服务器<->本地数据库)。

This requierement looks very common (or will be common in the future for mobile web app), but I can't find a library doing that.

这个要求看起来很常见(或者在未来的移动网络应用程序中会很常见),但我找不到这样做的图书馆。

I know google is doing that in their mobile web app (ex. gmail), and I found the WSPL projecta google project but without source to download.

我知道谷歌正在他们的移动网络应用程序(例如 gmail)中这样做,我发现WSPL 项目是一个谷歌项目,但没有下载源。

If I can't find a solution, I will create a library to do that, as one way sync doesn't look difficult, but I wonder if there are other solutions.

如果我找不到解决方案,我将创建一个库来做到这一点,因为一种方式同步看起来并不困难,但我想知道是否还有其他解决方案。

采纳答案by Samuel

  • I created a small JS lib named WebSqlSyncto synchronize a local WebSql DB with a server (client <-> server). Very easy to use and to integrate in your code :
  • 我创建了一个名为WebSqlSync的小型 JS 库,用于将本地 WebSql DB 与服务器(客户端 <-> 服务器)同步。非常易于使用和集成到您的代码中:

https://github.com/orbitaloop/WebSqlSync

https://github.com/orbitaloop/WebSqlSync

  • The open source project QuickConnectcontains a JS library to synchronize the local HTML5 SQLite DB to a server DB (MySQL or other) :
  • 开源项目QuickConnect包含一个 JS 库,用于将本地 HTML5 SQLite DB 同步到服务器 DB(MySQL 或其他):

http://quickconnect.pbworks.com/Using-Enterprise-Synchronization

http://quickconnect.pbworks.com/Using-Enterprise-Synchronization

To use this lib, you need to use the DataAccessObject of the framework to access your DB. It works by storing all the SQL request applied to the DB (except select of course) , and sending them to the server. It's great to manage deletion, but it's a little heavy if you have a lot of updates, and the server need to use the same SQL language...

要使用此库,您需要使用框架的 DataAccessObject 来访问您的数据库。它的工作原理是存储应用于数据库的所有 SQL 请求(当然 select 除外),并将它们发送到服务器。管理删除很好,但是如果你有很多更新的话就有点重了,而且服务器需要使用相同的SQL语言......

  • Another project from QuickConnectis a native SQLite synch (in Objective C for iOS or Mac OS and in Java for Android) :
  • QuickConnect 的另一个项目是本机 SQLite 同步(在 iOS 或 Mac OS 的 Objective C 和 Android 的 Java 中):

http://www.quickconnectfamily.org/qcdbsync/(I think it store also the history of all the SQL requests)

http://www.quickconnectfamily.org/qcdbsync/(我认为它还存储了所有 SQL 请求的历史记录)

  • And i just found another promising JS library : persistenceJS
  • 我刚刚发现了另一个很有前途的 JS 库:persistenceJS

https://github.com/zefhemel/persistencejs

https://github.com/zefhemel/persistencejs

"persistence.js is an asynchronous Javascript object-relational mapper library. You can use it in the browser, as well on the server (and you can share data models between them)."

“persistence.js 是一个异步 Javascript 对象关系映射库。你可以在浏览器中使用它,也可以在服务器上使用它(并且你可以在它们之间共享数据模型)。​​”

They have a DB synch module: DOC of persistence.synch.js

他们有一个 DB 同步模块:persistence.synch.js 的 DOC

(works with HTML5 DB SQLite or Google Gears on the client, and MySQL on the server)

(在客户端使用 HTML5 DB SQLite 或 Google Gears,在服务器上使用 MySQL)

  • And there is also Impel.inTouch. It looks very easy to use (with php files included), but you must use the Mootools framework in the client side :
  • 还有Impel.inTouch。它看起来很容易使用(包含 php 文件),但是您必须在客户端使用 Mootools 框架:

http://impel.simulacre.org/api/Impel.inTouch

http://impel.simulacre.org/api/Impel.inTouch

  • Sencha has also a synchronisation service: Sencha.io. Looks great, but it's dependent of the Sencha Touch framework:
  • Sencha 还有一个同步服务:Sencha.io。看起来不错,但它依赖于 Sencha Touch 框架:

http://www.sencha.com/products/io/

http://www.sencha.com/products/io/

回答by Samuel

I have developed a generic sync solution called WebSqlSync.

我开发了一个名为WebSqlSync的通用同步解决方案。

It's not dependant of any framework. It's available here : https://github.com/orbitaloop/WebSqlSync

它不依赖于任何框架。它可以在这里找到:https: //github.com/orbitaloop/WebSqlSync

Extract of the README file :

README 文件的摘录:

WebSqlSync

数据库同步

Automatically synchronize a local WebSql database (SQLite in the navigator) to a server. (2 way sync : client <-> server)

自动将本地 WebSql 数据库(导航器中的 SQLite)同步到服务器。(2路同步:客户端<->服务器)

Very easy to integrate to your existing app and very easy to use (2 functions to call : initSync and syncNow)

非常容易集成到您现有的应用程序中并且非常容易使用(调用 2 个函数:initSync 和 syncNow)

Usage

用法

Initialize

初始化

You need to initialize the lib (at each startup for example).

您需要初始化库(例如在每次启动时)。

It will automatically create 2 tables (if they don't already exists, one to store all the new or modified elements (table new_elem) and one to store the date of the last sync (table sync_info). It will also create SQLite triggers in order to watch the INSERT or UPDATE on the tables you want to synchronize (to automatically insert the modified elements in the new_elem table):

它将自动创建 2 个表(如果它们尚不存在,一个存储所有新的或修改的元素(表 new_elem)和一个存储上次同步的日期(表 sync_info)。它还将创建 SQLite 触发器)为了在要同步的表上观看 INSERT 或 UPDATE(自动在 new_elem 表中插入修改后的元素):

DBSYNC.initSync(TABLES_TO_SYNC, webSqlDb, sync_info, 'http://www.myserver.com', callBackEndInit);

Where TABLES_TO_SYNC is the list of table that you want to sync with the server, ex :

其中 TABLES_TO_SYNC 是您要与服务器同步的表列表,例如:

TABLES_TO_SYNC = [
    {tableName : 'table1', idName : 'the_id'},
    {tableName : 'table2'} //if idName not specified, it will assume that it's "id"
];

Synchronize

同步

To start the synchronization, you need to call the syncNow function. You can call it every X seconds, or after some changes for example :

要开始同步,您需要调用 syncNow 函数。您可以每 X 秒调用一次,或者在一些更改后调用它,例如:

DBSYNC.syncNow(callBackSyncProgress, function(result) {
     if (result.syncOK === true) {
         //Synchronized successfully
     }
});

And that's all you need to do on the client. On the server side, you will need to code your own solution (but it's not complicated). And there are some example inPHP & Java. Again, contributions are welcome.

这就是您需要在客户端上执行的全部操作。在服务器端,您需要编写自己的解决方案(但并不复杂)。并且在 PHP 和 Java 中有一些示例。再次,欢迎贡献。