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

Adblocker detected! Please consider reading this notice.

We've detected that you are using AdBlock Plus or some other adblocking software which is preventing the page from fully loading.

We don't have any banner, Flash, animation, obnoxious sound, or popup ad. We do not implement these annoying types of ads!

We need money to operate the site, and almost all of it comes from our online advertising.

Please add wikitechy.com to your ad blocking whitelist or disable your adblocking software.

×