Add Attachment in PouchDB

PouchDB Add Attachment

To add a binary object to a PouchDB document, the putAttachment() method is used. Blob or buffer object also plays a vital role in this process. While working with browser Blob is preferred and while working with Node.js, a buffer object is preferred.

Syntax:

db.putAttachment( doc_Id, attachment_Id, attachment, type, callback );

Example:

  • Create a file named “AddAttach.js” within a folder named “Examples”.
  • Add the below code to the file.
    AddAttach.js:

    var PouchDB = require('PouchDB');
    var db = new PouchDB('Example_Database');
    var my_attachment = new Buffer(['Hello World!'], {type:'text/plain'});
    db.putAttachment('101', 'attach1.txt', example_attachment, 'text/plain', function(err, res) {
    if (err) {
    return console.log(err);
    } else {
    console.log("Attachment Successfully Added.")
    }
    });
  • Open the command prompt.
  • Execute the .js file.
    node AddAttach.js
    

    Console Output:

    Attachment Successfully Added.

Explanation:
An empty document with an added attachment will be created in the “Example_Database” database in PouchDB.

Verification:

  • Create a file named “ReadDocument.js” within a folder named “Examples”.
  • Add the below code to the file.
    ReadDocument.js:

    var PouchDB = require('PouchDB');
    var db = new PouchDB('Example_Database');
    db.get('101',{attachments: true}, function(err, doc) {
    if (err) {
    return console.log(err);
    } else {
    console.log(doc);
    }
    });
  • Open the command prompt.
  • Execute the .js file.
    node ReadDocument.js
    

 

To add an attachment to an existing document:

Example:

  • Retrieve the values of the specific document.
    Output:

    { name: 'Tom',
    salary: 25000,
    experience: 3,
    _id: '102',
    _rev: '1-f56789f432154adb782da98358921a14'
    }
  • Using the respective_rev value of the document to add an attachment to the document.
  • Create a file named “AddAttach2.js” within a folder named “Examples”.
  • Add the below code to the file.
    AddAttach2.js: 

    var PouchDB = require('PouchDB');
    var db = new PouchDB('Example_Database');
    var my_attachment = new Buffer (['Hello World!'], {type: 'text/plain'});
    rev = '1-f56789f432154adb782da98358921a14';
    db.putAttachment('102', 'attach1.txt', rev, example_attachment, 'text/plain', function(err, res) {
    if (err) {
    return console.log(err);
    } else {
    console.log ("Attachment Successfully Added.")
    }
    });
  • Open the command prompt.
  • Execute the .js file.
    node AddAttach2.js
    

    Console Output:

    Attachment Successfully Added.

Verification:

Read the document to verify the attachment.

Output:

{  name: 'Tom',
salary: 25000,
experience: 3,
_attachments:
{ 'attach1.txt':
{ content_type: 'text/plain',
revpos: 2,
digest: 'md5-k7iFrf4NoInN9jSQT9WfcQ==',
data: 'AA==' } },
_id: '102',
_rev: '2-fec1229a76543db973ace8fcbacd453' }

To add an Attachment to a Remote Database:

Instead of the database name, pass the path of the database to add an attachment in CouchDB or remote server.

Example:

  • Create a file named “AddRemoteAttach.js” within a folder named “Examples”.
  • Add the below code to the file.
    AddRemoteAttach.js:

    var PouchDB = require('PouchDB');
    var db = new PouchDB('http://localhost:5984/students');
    var my_attachment = new Buffer (['Hello World!'], {type: 'text/plain'});
    rev = '1-1a2c3b3a45ae5111d90870f4e92dc55543';
    db.putAttachment('101', 'attach_2.txt',rev, example_attachment, 'text/plain', function(err, res) {
    if (err) {
    return console.log(err);
    } else {
    console.log ("Attachment Successfully Added.")
    }
    });

    Console Output:

    Attachment Successfully Added.

Explanation:
An attachment is added to the document with ID 101 stored in the “students” database in the CouchDB server.

Verification:

Check the added attachment to the document in the CouchDB server.