PouchDB Add Attachment



PouchDB Add Attachment

  • putAttachment() - This method is used to add a binary object to a document in PouchDB. Here we have to pass the document id, attachment id, MIME type along with the attachment.
  • This method takes a callback function as a optional parameter.
 PouchDB Add Attachment

PouchDB Add Attachment

Syntax

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

Add Attachment Example

To create a document with an attachment in a newly created database "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 wikitechy......'], {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")   
   }   
});  
  • Save the file name as "Add_Attachment.js" within a folder name "PouchDB_Examples". Open the command prompt and execute the javascript file using node.
node Add_Attachment.js  
 PouchDB Add Attachment

PouchDB Add Attachment

Verification

//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);   
   }   
});  
  • Save the above code in a file named "read_doc_attach.js" within a folder name "PouchDB_Examples". Open the command prompt and execute the JavaScript file using node:
node read_doc_attach.js  
 PouchDB Add Attachment

PouchDB Add Attachment

Add attachment to an existing document

  • We have a document in database named "Last_Database" with id "002". You can see the value of the document:
{ name: 'nisha',  
  age: 21,  
  designation: 'Teacher',  
  _id: '002',  
  _rev: '1-06272bacc14146d68d8ee0c36dfa0cb9'   
}  
  • Now add an attachment to this document using its _rev value.

Example:

//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 wikitechy....'], {type: 'text/plain'});  
  
rev =’ 2-78dfc06e4badf7f2d52f3b7cff85973f’;   
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")   
   }   
});  

Save the file name as "Add_Attachment2.js" within a folder name "PouchDB_Examples". Open the command prompt and execute the javascript file using node.

node Add_Attachment2.js  

Output:

 PouchDB Add Attachment

PouchDB Add Attachment

Add Attachment to a Remote Database Example

  • Additionally we can add an attachment to a remotely stored server (CouchDB). For this purpose, You just have to pass the path to the database where you need to add your attachment in CouchDB.
  • We have a database name "employees" stored on the CouchDB server.
 PouchDB Add Attachment

PouchDB Add Attachment

Add an attachment to the document 001 stored in a database named "employees" 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/employees');  
  
//Adding attachment to existing document  
var my_attachment = new Buffer (['Hello wikitechy'], {type: 'text/plain'});  
  
rev = '2-7258b5c92fedd3894ca53f15eb647e39';  
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")  
   }  
});  

Save the above code in a file named "Add_Remote_Attachment.js" within a folder name "PouchDB_Examples". Open the command prompt and execute the JavaScript file using node:

 PouchDB Add Attachment

PouchDB Add Attachment

Verification

Go to CouchDB server and we can see that attachment is added with the document.

 PouchDB Add Attachment


Related Searches to PouchDB Add Attachment