PouchDB in Hindi Adding Attachment




PouchDB में putAttachment () Method का उपयोग करके एक Document से एक Binary Object को Engaged किया जा सकता है.

Syntax

putAttachment () एक Syntax है इस System में हमें Document के साथ Attachment id, MIME Type Attachment का साथ देना होता है . यह Method एक Callback Function को भी स्वीकार करता है.

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

Blob और Buffer Object को Document के साथ Attachment तैयार करने के लिए उपयोग किया जाता है.

Browser के साथ काम करते समय Blob का उपयोग किया जाता है और Node.js के साथ काम करते समय Buffer का उपयोग किया जाता है.

Adding Attachment Example

//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 JavaTpoint......'], {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")   
   }   
});  

अब हम एक Folder PouchDB_Examples के Name से बनाते है और उसमे " Add_Attachment.js" नामक File में Code को Save कर लेते है.

node Add_Attachment.js  

Output

C:\Users\Altamas Ali\Desktop\PouchDB_Examples>node Add_Attachment.js
Document (Batch) created Successfully

C:\Users\Altamas Ali\Desktop\PouchDB_Examples>

New_Database नामक Database को संयुक्त जोड़ते हुए रिक्त Document बनाएगा जो कि PouchDB में Store होता है.

Verification

//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);
 }
});

अब हम एक Folder PouchDB_Examples के name से बनाते है और उसमे " Read_Doc.js" नामक File में Code को Save कर लेते है.

node Read_Doc.js   

Output

Add Attachment to an Existing Document

हमारे पास एक Document है जो id "002" के साथ "Last_Database" नामक डेटाबेस में अपने Document के मान देख सकते हैं.

{ name: 'Altamas',  
  age: 21,  
  designation: 'Web developer',  
  _id: '002',  
  _rev: '1-06272bacc14146d68d8ee0c36dfa0cb9'   
}    

इस Document में इसकी _rev Vale का उपयोग करके एक Attachment को जोड़ा जा सकता है.

//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 wisdomjobs'], {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")
 }
});
  

अब हम एक Folder PouchDB_Examples के Name से बनाते है और उसमे "Add_Attachment2.js" नामक File में Code को Save कर लेते है.

C:\PouchDB_Examples >node Add_Attachment_to_doc.js  

और यह Referred Document को एक संयुक्त जोड़ता है और यह निम्न संदेश Displayed होता है.

Attachment added successfully 

अगर read_doc.js में आईडी मान को 002 में बदल दिया जाता और उसे Execute किया जाता है.

Output