PouchDB教程

PouchDB 添加附件

putAttachment()方法用于将二进制对象添加到PouchDB中的文档。要使用此方法,您必须将文档ID,附件ID,MIME类型以及附件一起传递。此方法还接受可选的回调函数。
语法:
db.putAttachment( doc_Id, attachment_Id, attachment, type, [callback] );
Blob或缓冲区对象用于准备文档附件。使用Blob时使用浏览器,使用缓冲区时使用Node.js。
在这里,我们使用Node.js控制台,因此我们将使用缓冲区对象。

添加附件示例

让我们以一个示例为例,在新创建的数据库" New_Database"中创建带有附件的文档。
//Requiring the package 
var PouchDB = require('PouchDB');
//Creating the database object 
var db = new PouchDB('New_Database');
//Preparing the attachment 
var my_attachment = new Buffer(['Hello lidihuo......'], {type:'text/plain'});
//Adding attachment to a document 
db.putAttachment('001', 'attachment1.txt', my_attachment, 'text/plain', function(err, res) { 
   if (err) { 
      return console.log(err); 
   } else { 
      console.log(res+"Attachment added successfully") 
   } 
});
将以上代码保存在名为" PouchDB_Examples"的文件夹中的名为" Add_Attachment.js"的文件中。打开命令提示符,并使用node执行JavaScript文件:
node Add_Attachment.js
输出:
PouchDB添加附件1
上面的示例将创建一个空文档向存储在PouchDB中的名为" New_Database"的数据库添加附件。

验证

使用以下代码验证是否添加了附件。
//Requiring the package 
var PouchDB = require('PouchDB');
//Creating the database object 
var db = new PouchDB('New_Database');
//Reading the Document 
db.get('001',{attachments: true}, function(err, doc) { 
   if (err) { 
      return console.log(err); 
   } else { 
      console.log(doc); 
   } 
});
将以上代码保存在名为" PouchDB_Examples"的文件夹中的名为" Read_Doc.js"的文件中。打开命令提示符,并使用node执行JavaScript文件:
node Read_Doc.js
输出:
PouchDB添加附件2

将附件添加到现有文档中

我们在数据库" Last_Database"中有一个ID为" 002"的文档。您可以看到文档的值:
{ name: 'Aryan',
  age: 21,
  designation: 'Teacher',
  _id: '002',
  _rev: '1-06272bacc14146d68d8ee0c36dfa0cb9' 
}
PouchDB添加附件3
现在使用_rev值向此文档添加附件。
示例:
//Requiring the package 
var PouchDB = require('PouchDB');
//Creating the database object 
var db = new PouchDB('Last_Database');
//Adding attachment to existing document 
var my_attachment = new Buffer (['Hello lidihuo....'], {type: 'text/plain'});
rev = '1-06272bacc14146d68d8ee0c36dfa0cb9'; 
db.putAttachment('002', 'attachment1.txt', rev, my_attachment, 'text/plain', function(err, res) { 
   if (err) { 
      return console.log(err); 
   } else { 
      console.log (res + "Attachment added successfully") 
   } 
});
将以上代码保存在名为" PouchDB_Examples"的文件夹中的名为" Add_Attachment2.js"的文件中。打开命令提示符,并使用node执行JavaScript文件:
node Add_Attachment2.js
输出:
PouchDB添加附件4

验证

您可以使用读取命令来验证添加的附件:
PouchDB添加附件5
{ name: 'Aryan',
  age: 21,
  designation: 'Teacher',
  _attachments:
   { 'attachment1.txt':
      { content_type: 'text/plain',
        revpos: 2,
        digest: 'md5-k7iFrf4NoInN9jSQT9WfcQ==',
        data: 'AA==' } },
  _id: '002',
  _rev: '2-388510d44393457cb06764dd89542ef3' }

将附件添加到远程数据库

您还可以将附件添加到远程存储的服务器(CouchDB)。您只需要将路径传递到CouchDB中的数据库,其中包含您要添加附件的文档。

示例

我们有一个数据库名称存储在CouchDB服务器上的"员工"。
PouchDB添加附件6
数据库中有一个文档,其ID为001、
PouchDB添加附件7
将附件添加到存储在名为"员工"的数据库中的文档001存储在CouchDB服务器中。
//Requiring the package
var PouchDB = require('PouchDB');
//Creating the database object
var db = new PouchDB('http://localhost:5984/employees');
//Adding attachment to existing document
var my_attachment = new Buffer (['Hello lidihuo...'], {type: 'text/plain'});
rev = '9-92dc28669aea902edc4cf0d69c0eaebb';
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")
   }
});
将以上代码保存在名为" PouchDB_Examples"的文件夹中的名为" Add_Remote_Attachment.js"的文件中。打开命令提示符,并使用node执行JavaScript文件:
node Add_Remote_Attachment.js
输出:
PouchDB添加附件8

验证

转到CouchDB服务器,您可以看到该附件随文档一起添加。
PouchDB添加附件9
昵称: 邮箱:
Copyright © 2022 立地货 All Rights Reserved.
备案号:京ICP备14037608号-4