You can join a paired item to an archive utilizing the putAttachment() technique in PouchDB.
Syntax
Following is the grammar of the putAttachment(). To this technique, we need to pass the archive id, connection id, MIME type alongside the connection. This strategy additionally acknowledges a discretionary callback work.
db.putAttachment( docId, attachmentId, attachment, type, [callback] );
We can get ready connection utilizing mass or cushion objects, where mass is utilized while working with the program and support is utilized while working with Node.js, since we are showing our projects in Node.js, we utilize cradle objects to plan reports.
Example
Following is an illustration of making a report with a connection, inside a data set named my_database in PouchDB utilizing putAttachment() strategy.
//Requiring the package
var PouchDB = require('PouchDB');
//Creating the database object var db = new PouchDB('my_database');
//Preparing the attachment
var my_attachment = new Buffer(['Welcome to tutorialspoint'], {type: 'text/plain'});
//Adding attachment to a document
db.putAttachment('001', 'att_1.txt', 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 document with name Add_Attachment.js. Open the order incite and execute the JavaScript record utilizing hub as demonstrated beneath.
C:\PouchDB_Examples >node Add_Attachment.js
This makes a vacant record adding a connection to it, in the data set named my_database which is put away in PouchDB, and presentations the accompanying message.
Attachment added successfully
You can check whether the connection is added by perusing the report utilizing the accompanying code.
//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);
}
});
Save the above code as read_doc.js and execute it. Executing this program, you can see the accompanying substance of the archive.
{
_attachments: {
att_1.txt: {
content_type: 'text/plain',
digest: 'md5-k7iFrf4NoInN9jSQT9WfcQ==',
data: 'AA=='
}
},
_id: '001',
_rev: '1-620fd5f41d3328fcbf9ce7504338a51d'
}
Adding Attachment to an Existing Document
Assume, there is a report in an information base by the name my_database PouchDB with id '002'. You can get the substance of it by executing the read_doc.js by changing the id incentive to 002, as demonstrated beneath.
{
name: 'Raju',
age: 23,
designation: 'Designer',
_id: '002',
_rev: '1-05ca7b5f3f4762a9fb2d119cd34c8d40'
}
Presently, you can add a connection to this archive utilizing its _rev esteem.
//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 tutorialspoint'], {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")
}
});
Save the above code in a document with the name Add_Attachment_to_doc.js. Open the order provoke and execute the JavaScript record utilizing hub as demonstrated beneath.
C:\PouchDB_Examples >node Add_Attachment_to_doc.js
This adds a connection to the predetermined report showing the accompanying message.
Attachment added successfully
On the off chance that you change the id esteem in read_doc.js to 002 and execute it, you will get the accompanying yield.
{
name: 'Raju',
age: 23,
designation: 'Designer',
_attachments: {
att_1: {
content_type: 'text/plain',
digest: 'md5-k7iFrf4NoInN9jSQT9WfcQ==',
data: 'AA=='
}
},
_id: '002',
_rev: '2-3bb4891b954699bce28346723cc7a709'
}
Adding Attachment to a Remote Document
You can even add a connection to the archive existing in an information base that is put away distantly on the worker (CouchDB).
To do as such, rather than a data set name, you need to pass the way to the data set in CouchDB, which contains the archive that will be perused.
Example
Assume there is an information base named my_database in the CouchDB worker. At that point, in the event that you confirm the rundown of data sets in CouchDB utilizing the URL http://127.0.0.1:5984/_utils/index.html you will get the accompanying screen capture.
Also, on the off chance that you select the information base named my_database, you can see its substance as demonstrated beneath.
Following is an illustration of adding a connection to the report 001 put away in an information base named my_database which is put away in the CouchDB worker.
//Requiring the package
var PouchDB = require('PouchDB');
//Creating the database object
var db = new PouchDB('http://localhost:5984/my_database');
//Adding attachment to existing document
var my_attachment = new Buffer (['Welcome to tutorialspoint'], {type: 'text/plain'});
rev = '1-36c34fdcf29a652876219065f9681602';
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 document with the name Remote_Add_Attachment.js. Open the order provoke and execute the JavaScript record utilizing hub as demonstrated beneath.
C:\PouchDB_Examples >node Remote_Add_Attachment.js
This adds a connection to the predefined archive showing the accompanying message.
Attachment added successfully
Presently, in the event that you check the archive, you can notice the connection added to it as demonstrated in the accompanying screen capture.
