Pouchdb 简明教程

PouchDB - Adding Attachment

可以使用 PouchDB 中的 putAttachment() 方法将二进制对象附加到文档。

You can attach a binary object to a document using the putAttachment() method in PouchDB.

Syntax

以下是如何 putAttachment() 。必须将文档 ID、附件 ID、MIME 类型以及附件传递给此方法。此方法还接受可选的回调函数。

Following is the syntax of the putAttachment(). To this method, we have to pass the document id, attachment id, MIME type along with the attachment. This method also accepts an optional callback function.

db.putAttachment( docId, attachmentId, attachment, type, [callback] );

我们可用 blob 或 buffer 对象准备附件,其中 blob 用在使用浏览器时,而 buffer 用在与 Node.js 合作时,由于我们在 Node.js 中演示程序,我们使用 buffer 对象来准备文档。

We can prepare attachment using blob or buffer objects, where blob is used while working with the browser and buffer is used while working with Node.js, since we are demonstrating our programs in Node.js, we use buffer objects to prepare documents.

Example

以下是如何在 PouchDB 中使用 putAttachment() 方法,在名为 my_database 的数据库中创建一个含附件的文档。

Following is an example of creating a document with an attachment, within a database named my_database in PouchDB using putAttachment() method.

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

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

//Preparing the attachment
var my_attachment = new Buffer(['Welcome to tutorialspoint'], {type: 'text/plain'});

//Adding attachment to a document
db.putAttachment('001', 'att_1.txt', my_attachment, 'text/plain', function(err, res) {
   if (err) {
      return console.log(err);
   } else {
      console.log(res+"Attachment added successfully")
   }
});

将上述代码另存为 Add_Attachment.js 文件名。打开命令提示符,然后使用 node 执行 JavaScript 文件,如下所示。

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

C:\PouchDB_Examples >node Add_Attachment.js

这将创建一个空文档,并将附件添加到存储在 PouchDB 中的、名为 my_database 的数据库中,并显示以下消息。

This creates an empty document adding an attachment to it, in the database named my_database which is stored in PouchDB, and displays the following message.

Attachment added successfully

可以使用以下代码读取文档,以验证是否添加了附件。

You can verify whether the attachment is added by reading the document using the following code.

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

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

//Reading the Document
db.get('001',{attachments: true}, function(err, doc) {
   if (err) {
      return console.log(err);
   } else {
      console.log(doc);
   }
});

将上述代码另存为 read_doc.js 并执行它。执行此程序,可以看到以下文档内容。

Save the above code as read_doc.js and execute it. Executing this program, you can see the following contents of the document.

{
   _attachments: {
      att_1.txt: {
         content_type: 'text/plain',
         digest: 'md5-k7iFrf4NoInN9jSQT9WfcQ==',
         data: 'AA=='
      }
   },
   _id: '001',
   _rev: '1-620fd5f41d3328fcbf9ce7504338a51d'
}

Adding Attachment to an Existing Document

假设在 PouchDB 的 my_database 数据库中有一个文档,其 ID 为“ 002 ”。可以通过将 ID 值更改为 002 来执行 read_doc.js ,以获取其内容,如下所示。

Suppose, there is a document in a database by the name my_database PouchDB with id ‘002’. You can get the contents of it by executing the read_doc.js by changing the id value to 002, as shown below.

{
   name: 'Raju',
   age: 23,
   designation: 'Designer',
   _id: '002',
   _rev: '1-05ca7b5f3f4762a9fb2d119cd34c8d40'
}

现在,可以使用其 _rev 值将附件添加此文档。

Now, you can add an attachment to this document using its _rev value.

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

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

//Adding attachment to existing document
var my_attachment = new Buffer (['Welcome to tutorialspoint'], {type: 'text/plain'});

rev = '1-05ca7b5f3f4762a9fb2d119cd34c8d40';
db.putAttachment('002', 'att_1.txt', rev, my_attachment, 'text/plain', function(err, res) {
   if (err) {
      return console.log(err);
   } else {
      console.log (res + "Attachment added successfully")
   }
});

将上述代码另存为 Add_Attachment_to_doc.js 文件名。打开命令提示符,然后使用 node 执行 JavaScript 文件,如下所示。

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

C:\PouchDB_Examples >node Add_Attachment_to_doc.js

这将向指定文档添加附件,并显示以下消息。

This adds an attachment to the specified document displaying the following message.

Attachment added successfully

如果将 read_doc.js 中的 ID 值更改为 002 并执行它,则会得到以下输出。

If you change the id value in read_doc.js to 002 and execute it, you will get the following output.

{
   name: 'Raju',
   age: 23,
   designation: 'Designer',
   _attachments: {
      att_1: {
         content_type: 'text/plain',
         digest: 'md5-k7iFrf4NoInN9jSQT9WfcQ==',
         data: 'AA=='
      }
   },
   _id: '002',
   _rev: '2-3bb4891b954699bce28346723cc7a709'
}

Adding Attachment to a Remote Document

你甚至可以添加到存储在服务器 (CouchDB) 上的远程数据库中的文档中添加附件。

You can even add an attachment to the document existing in a database that is stored remotely on the server (CouchDB).

要执行此操作,您需要传入 CouchDB 中的数据库路径而不是数据库名称,该路径包含要读取的文档。

To do so, instead of a database name, you need to pass the path to the database in CouchDB, which contains the document that is to be read.

Example

假设 CouchDB 服务器中有一个名为 my_database 的数据库。然后,如果您使用 URL http://127.0.0.1:5984/_utils/index.html 验证 CouchDB 中的数据库列表,您将获得以下屏幕截图。

Suppose there is a database named my_database in the CouchDB server. Then, if you verify the list of databases in CouchDB using the URL http://127.0.0.1:5984/_utils/index.html you will get the following screenshot.

adding attachment to remote database

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

And if you select the database named my_database, you can view its contents as shown below.

adding attachment

以下是如何向存储在名为 my_database 的数据库中的文档 001 添加附件的示例(该数据库存储在 CouchDB 服务器中)。

Following is an example of adding an attachment to the document 001 stored in a database named my_database which is stored in the CouchDB server.

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

//Creating the database object
var db = new PouchDB('http://localhost:5984/my_database');

//Adding attachment to existing document
var my_attachment = new Buffer (['Welcome to tutorialspoint'], {type: 'text/plain'});

rev = '1-36c34fdcf29a652876219065f9681602';
db.putAttachment('001', 'att_1.txt',rev, my_attachment, 'text/plain', function(err, res) {
   if (err) {
      return console.log(err);
   } else {
      console.log (res+ "Attachment added successfully")
   }
});

将上述代码另存为 Remote_Add_Attachment.js 文件名。打开命令提示符,然后使用 node 执行 JavaScript 文件,如下所示。

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

C:\PouchDB_Examples >node Remote_Add_Attachment.js

这将向指定文档添加附件,并显示以下消息。

This adds an attachment to the specified document displaying the following message.

Attachment added successfully

现在,如果你验证该文档,你会发现如以下屏幕截图所示,附件已添加到该文档。

Now, if you verify the document, you can observe the attachment added to it as shown in the following screenshot.

adding attachment verification