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

发布于 2024-08-11 11:52:09 字数 525 浏览 12 评论 0原文

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

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

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

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

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

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.

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 project a google project but without source to download.

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

握住你手 2024-08-18 11:52:09
  • 我创建了一个名为 WebSqlSync 的小型 JS 库,用于将本地 WebSql DB 与服务器(客户端 <-> 服务器)同步。非常易于使用并集成到您的代码中:

https://github.com/orbitaloop/WebSqlSync

  • 开源项目 QuickConnect 包含一个 JS 库,用于将本地 HTML5 SQLite DB 同步到服务器 DB(MySQL 或其他):

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

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

  • 来自 QuickConnect 的另一个项目是一个原生的SQLite 同步(iOS 或 Mac OS 的 Objective C 以及 Android 的 Java):

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

  • 而且我刚刚发现了另一个有前途的 JS 库: persistenceJS

https://github.com/zefhemel/persistencejs

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

他们有一个数据库同步模块:persistence.synch.js 的文档

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

  • 此外还有 Impel.inTouch。它看起来很容易使用(包含 php 文件),但您必须在客户端使用 Mootools 框架:

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

  • Sencha 还有一个同步服务:Sencha.io。看起来很棒,但它依赖于 Sencha Touch 框架:

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

  • I created a small JS lib named WebSqlSync to synchronize a local WebSql DB with a server (client <-> server). Very easy to use and to integrate in your code :

https://github.com/orbitaloop/WebSqlSync

  • The open source project QuickConnect contains a JS library to synchronize the local HTML5 SQLite DB to a server DB (MySQL or other) :

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...

  • Another project from QuickConnect is a native SQLite synch (in Objective C for iOS or Mac OS and in Java for Android) :

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

  • And i just found another promising JS library : 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)."

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

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

  • 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 :

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:

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

柒七 2024-08-18 11:52:09

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

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

自述文件摘录:

WebSqlSync

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

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

使用

初始化

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

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

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

其中 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

启动同步,您需要调用syncNow函数。您可以每隔X秒调用一次,或者在进行一些更改后调用它:

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

这就是您在服务器端需要做的所有事情,您需要编写自己的解决方案(但是)。它并不复杂),并且有一些 PHP 和 Java 的例子,欢迎贡献。

I have developed a generic sync solution called WebSqlSync.

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

Extract of the README file :

WebSqlSync

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

Very easy to integrate to your existing app and very easy to use (2 functions to call : initSync and 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):

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 = [
    {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 :

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文