PouchDB Delete Attachment




PouchDB Delete Attachment

  • removeAttachment() - This method is used to delete an attachment from PouchDB. To delete an attachment, We have to pass the document_id, attachment_id and _rev value.
  • This method takes an callback function as a optional parameter.
 PouchDB Delete Attachment

PouchDB Delete Attachment

Syntax:

db.removeAttachment ( doc_Id, attachment_Id, rev, [callback] ); 

Example

We have a document in PouchDB with id 002, which contains id, name, age, designation of an employee with attachment.

{
  "_id": "001",
  "name": "admin",
  "age": "28",
  "_attachments": {
    "att_1.txt": {
      "digest": "md5-k7iFrf4NoInN9jSQT9WfcQ==",
      "content_type": "text/plain",
      "length": 1,
      "revpos": 3,
      "stub": true
    }
  },
  "_rev": "4-97990b4971035c69450d0f1cf4b6a6f5"
}
  • Let's remove the attachment by using removeAttachment() method.
//Requiring the package  
var PouchDB = require('PouchDB');  
//Creating the database object  
var db = new PouchDB('Last_Database');  
db.removeAttachment('002', 'attachment_1.txt', '4-97990b4971035c69450d0f1cf4b6a6f5',  
function(err, res) {  
if (err) {  
  return console.log(err);  
} else {  
  console.log(res+"document Deleted successfully")  
}  
});  

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

node Del_Attachment.js  

Output

 PouchDB Delete Attachment

PouchDB Delete Attachment

  • Attachment is deleted successfully.

Verification

Using read command to verify that the attachment is deleted from the document.

 PouchDB Delete Attachment

PouchDB Delete Attachment

Delete Attachment from a Remote Database

Additionally we can delete an attachment in a database that is stored remotely on a server (CouchDB). For this purpose, You just have to pass the path to the database where you want to delete the attachment in CouchDB.

Example

  • We have a database name "employees" stored on the CouchDB server
 PouchDB Delete Attachment

PouchDB Delete Attachment

  • The database "employees" has a document having id "001".
 PouchDB Delete Attachment

PouchDB Delete Attachment

  • Let's delete the attachment.
//Requiring the package   
var PouchDB = require('PouchDB');  
//Creating the database object   
var db = new PouchDB('http://localhost:5984/employees');  
  
db.removeAttachment('001', 'att_1.txt', '4-97990b4971035c69450d0f1cf4b6a6f5',   
   function(err, res) {   
   if (err) {   
      return console.log(err);   
   } else {   
      console.log(res+"Attachment Deleted successfully")   
   }   
});  

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

node Delete_Remote_Attachment.js   

Output:

 PouchDB Delete Attachment

PouchDB Delete Attachment



Related Searches to PouchDB Delete Attachment