- PouchDB 教程
- PouchDB - 首頁
- PouchDB - 概述
- PouchDB - 環境
- PouchDB - 建立資料庫
- PouchDB - 資料庫資訊
- PouchDB - 刪除資料庫
- PouchDB - 建立文件
- PouchDB - 讀取文件
- PouchDB - 更新文件
- PouchDB - 刪除文件
- PouchDB - 建立批處理
- PouchDB - 獲取批處理
- PouchDB - 更新批處理
- PouchDB - 刪除批處理
- PouchDB - 新增附件
- PouchDB - 檢索附件
- PouchDB - 刪除附件
- PouchDB - 複製
- PouchDB - 同步
- PouchDB - 其他
- PouchDB 有用資源
- PouchDB - 快速指南
- PouchDB - 有用資源
- PouchDB - 討論
PouchDB - 同步
您可以將儲存在 PouchDB 中的本地資料庫與儲存在 CouchDB 中的資料庫同步。在上一章中,我們已經瞭解瞭如何使用 PouchDB 複製資料庫。在那裡,我們使用了方法PouchDB.replicate(source, destination)。
除此之外,我們還可以使用replicate.to()和replicate.from()方法將資料從本地資料庫複製到遠端資料庫,以及從遠端資料庫複製到本地資料庫,如下所示。
//Replicating data from local database to remote database localDB.replicate.to(remoteDB); //Replicating data from remote database to local database localDB.replicate.from(remoteDB);
其中,localDB是儲存在 PouchDB 中的本地資料庫的物件,remoteDB是儲存在 CouchDB 中的資料庫的物件。
示例
假設在 PouchDB 中有一個名為local_database的資料庫,它包含 3 個文件,doc1、doc2 和 doc3,其內容如下所示。
doc1 = {_id: '003', name: 'Ram', age: 26, Designation: 'Programmer'}
doc2 = {_id: '004', name: 'Robert', age: 27, Designation: 'Programmer'}
doc3 = {_id: '005', name: 'Rahim', age: 28, Designation: 'Programmer'}
並且在 CouchDB 中有一個名為Remote_Database的資料庫,它包含 2 個文件 doc1、doc2,其內容如下所示。
doc1 = {_id: '001', name: 'Geeta', age: 25, Designation: 'Programmer'}
doc2 = {_id: '002', name: 'Zara Ali', age: 24, Designation: 'Manager'}
以下是如何使用replicate.to()和replicate.from()方法同步這兩個資料庫的示例,其中一個儲存在 PouchDB 中,另一個儲存在 CouchDB 中。
//Requiring the package
var PouchDB = require('PouchDB');
//Creating local database object
var localDB = new PouchDB('local_database');
//Creating remote database object
var remoteDB = new PouchDB('https://:5984/remote_database');
//Synchronising both databases
localDB.replicate.to(remoteDB);
remoteDB.replicate.from(localDB);
console.log("Databases synchronized successfully");
將以上程式碼儲存到名為Synchronising_databases.js的檔案中。開啟命令提示符並使用node執行 JavaScript 檔案,如下所示。
C:\PouchDB_Examples >node Synchronising_databases.js
這將同步兩個資料庫 remoteDB 和 localDB,並在控制檯上顯示如下訊息。
Databases synchronized successfully.
同步兩個資料庫後,訪問http://127.0.0.1:5984/_utils/index.html並選擇remote_database。您可以觀察到本地資料庫的文件(003、004、005)已複製到此資料庫中,如下所示。
同樣,如果您獲取儲存在 PouchDB 中的local_database的內容,您會發現儲存在 CouchDB 中的資料庫的文件已複製到這裡。
[
{
id: '001',
key: '001',
value: { rev: '1-23cf3767e32a682c247053b16caecedb' },
doc: {
name: 'Geeta',
age: 25,
Designation: 'Programmer',
_id: '001',
_rev: '1-23cf3767e32a682c247053b16caecedb'
}
},
{
id: '002',
key: '002',
value: { rev: '1-d5bcfafbd4d4fae92fd7fc4fdcaa3a79' },
doc: {
name: 'Zara Ali',
age: 24,
Designation: 'Manager',
_id: '002',
_rev: '1-d5bcfafbd4d4fae92fd7fc4fdcaa3a79'
}
},
{
id: '003',
key: '003',
value: { rev: '1-bf4619471ac346fdde46cfa8fbf3587f' },
doc: {
name: 'Ram',
age: 26,
Designation: 'Programmer',
_id: '003',
_rev: '1-bf4619471ac346fdde46cfa8fbf3587f'
}
},
{
id: '004',
key: '004',
value: { rev: '1-29b8f803958c994e3eb37912a45d869c' },
doc: {
name: 'Robert',
age: 27,
Designation: 'Programmer',
_id: '004',
_rev: '1-29b8f803958c994e3eb37912a45d869c'
}
},
{
id: '005',
key: '005',
value: { rev: '1-0eb89f71998ffa8430a640fdb081abd2' },
doc: {
name: 'Rahim',
age: 28,
Designation: 'Programmer',
_id: '005',
_rev: '1-0eb89f71998ffa8430a640fdb081abd2'
}
}
]
您可以使用 PouchDB 提供的sync()方法重寫上述程式,而不是使用replicate.to()和replicate.from()這兩個方法,如下所示。
//Requiring the package
var PouchDB = require('PouchDB');
//Creating local database object
var localDB = new PouchDB('local');
//Creating remote database object
var remoteDB = new PouchDB('https://:5984/remote_database');
//Synchronising Remote and local databases
localDB.sync(remoteDB, function(err, response) {
if (err) {
return console.log(err);
} else {
console.log(response);
}
});
執行上述程式後,它將同步兩個資料庫並顯示以下訊息。
{
push: {
ok: true,
start_time: Fri Mar 25 2016 15:54:37 GMT+0530 (India Standard Time),
docs_read: 6,
docs_written: 6,
doc_write_failures: 0,
errors: [],
last_seq: 10,
status: 'complete',
end_time: Fri Mar 25 2016 15:54:37 GMT+0530 (India Standard Time)
},
pull: {
ok: true,
start_time: Fri Mar 25 2016 15:54:37 GMT+0530 (India Standard Time),
docs_read: 0,
docs_written: 0,
doc_write_failures: 0,
errors: [],
last_seq: 2,
status: 'complete',
end_time: Fri Mar 25 2016 15:54:37 GMT+0530 (India Standard Time)
}
}