Pouchdb 简明教程

PouchDB - Replication

PouchDB 最重要的功能之一是复制,即你可以制作数据库副本。你可以复制存储在本地位置的 PouchDB 实例或存储在远程位置的 CouchDB 实例。

One of the most important features of PouchDB is replication, i.e. you can make a copy of a database. You can replicate either a PouchDB instance stored locally or a CouchDB instance stored remotely.

Syntax

以下是复制 PouchDB 中数据库的语法。此处, source database 的副本是目标。你可以直接以字符串格式向此方法传递源数据库和目标数据库的位置,或可以传递表示它们的项目。

Following is the syntax of replicating a database in PouchDB. Here, a copy of the source database is the target. To this method, you can directly pass the location of source and destination databases in String format, or you can pass objects representing them.

PouchDB.replicate(source, target, [options])

源和目标都可以是 PouchDB 实例或 CouchDB 实例。

Both the source and targets can be either PouchDB instances or CouchDB instances.

Replicating LocalDB to CouchDB

假设 PouchDB 中有一个名为 sample_database 的数据库,它包含 3 个文档 doc1、doc2 和 doc3,其内容如下所示。

Suppose there is a database with the name sample_database in PouchDB, and it contains 3 documents doc1, doc2, and doc3, having contents as shown below.

doc1 = {_id: '001', name: 'Ram', age: 23, Designation: 'Programmer'}
doc2 = {_id: '002', name: 'Robert', age: 24, Designation: 'Programmer'}
doc3 = {_id: '003', name: 'Rahim', age: 25, Designation: 'Programmer'}

这是一个创建存储在 CouchDB 中名为 sample_database 的数据库副本的示例。

Following is an example which makes a copy of the database named sample_database that is stored locally in CouchDB.

//Requiring the package
var PouchDB = require('PouchDB');

var localdb = 'sample_database';

//Creating remote database object
var remotedb = 'http://localhost:5984/sample_database';

//Replicating a local database to Remote
PouchDB.replicate(localDB, remoteDB);
console.log ("Database replicated successfully");

将上述代码保存到名为 Replication_example.js 的文件中。打开命令提示符并使用 node 执行 JavaScript 文件,如下所示。

Save the above code in a file with name Replication_example.js. Open the command prompt and execute the JavaScript file using node as shown below.

C:\PouchDB_Examples >node Replication_example.js

这会创建 CouchDB 实例中名为 sample_database 的数据库的副本,并在控制台中显示一条消息,如下所示。

This makes a copy of the database named sample_database in CouchDB instance and displays a message on the console as shown below.

Database replicated successfully

通过点击以下链接,您可以验证数据库在您的 CouchDB 实例中是否已复制: http://127.0.0.1:5984/_utils/index.html

You can verify whether the database is replicated in your CouchDB instance by clicking the following link http://127.0.0.1:5984/_utils/index.html.

单击时,您会看到 CouchDB 中的数据库列表。您还可观察到一个数据库副本 sample_database 创建在此处。

On clicking, you can see the list of databases in your CouchDB. You can also observe that a copy of the database sample_database is created here.

sample database

如果您选择复制的数据库,则您可以查看如下所示的内容。

If you select the replicated database, you can view its contents as shown below.

replicated database

Replicating CouchDB to PouchDB

假定在 CouchDB 中有一个名为 Remote_Database 的数据库,其中包含文档 doc1、doc2 和 doc3,内容如下所示。

Suppose there is a database with the name Remote_Database in CouchDB and it contains 3 documents, doc1, doc2, and doc3, having contents as shown below.

doc1 = {_id: '001', name: 'Geeta', age: 25, Designation: 'Programmer'}
doc2 = {_id: '002', name: 'Zara Ali', age: 24, Designation: 'Manager'}
doc3 = {_id: '003', name: 'Mary', age: 23, Designation: 'Admin'}

这是一个将 CouchDB 中名为 Remote_Database 的数据库的副本保存在本地存储中的示例。

Following is an example which makes a copy of the database named Remote_Database that is stored in CouchDB in the local storage.

//Requiring the package
var PouchDB = require('PouchDB');

var localdb = 'sample_database';

var remotedb = 'http://localhost:5984/sample_database1';

//Replicating a local database to Remote
PouchDB.replicate(remotedb, localdb);
console.log("Database replicated successfully");

使用 Replication_example2.js 将上述代码保存到文件中。打开命令窗口,并使用 node 执行 JavaScript 文件,如下所示。

Save the above code in a file with the name Replication_example2.js. Open the command prompt and execute the JavaScript file using node as shown below.

C:\PouchDB_Examples >node Replication_example2.js

这将对 PouchDB 实例中名为 remote_database 的数据库进行副本,并在控制台上显示如下所示的消息。

This makes a copy of the database named remote_database in PouchDB instance and displays a message on the console as shown below.

Database replicated successfully

您可以通过执行以下代码来验证数据库是否在您的 Pouch 实例中复制。

You can verify whether the database is replicated in your Pouch instance by executing the following code.

//Requiring the package
var PouchDB = require('PouchDB');

//Creating the database object
var db = new PouchDB('remote_database');

//Retrieving all the documents in PouchDB
db.allDocs({include_docs: true, attachments: true}, function(err, docs) {
   if (err) {
      return console.log(err);
   } else {
      console.log(docs.rows);
   }
});

如果数据库在执行上述代码后复制,您会得到复制的数据库内容,如下所示。

If the database is replicated on executing the above code, you will get the contents of the replicated database as shown below.

[
   {
      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-c4cce025dbd30d21e40882d41842d5a4' },
      doc: {
         name: 'Mary',
         age: 23,
         Designation: 'Admin',
         _id: '003',
         _rev: '1-c4cce025dbd30d21e40882d41842d5a4'
      }
   }
]