PouchDB JavaScript में लिखा हुआ एक open-source, NoSQL, in-line database डेटाबेस है .इसे CouchDB के बाद तैयार किया गया है यह स्थानीय रूप से डेटा सहेज सकता है .
इसलिए यह online अच्छी तरह से offline और साथ ही काम करता है.
PouchDB का उपयोग करके users ऐसे applications बना सकता है जो offline और online काम कर सकते हैं .
PouchDB एक CouchDB के JavaScript कार्यान्वयन है जो ब्राउजर में Node.js में चलते हुए इसको मुख्य रूप से लगभग पूर्ण वफादारी के साथ CouchDB API का अनुकरण करने के लिए उपयोग किया जाता है .
Data को स्टोर करने के लिए PouchDB आंतरिक रूप से WebSQL और IndexedDB का उपयोग करता है .
PouchDB में, जब applications offline हो, तो डेटा स्थानीय रूप से ब्राउज़र में WebSQL और IndexedDB का उपयोग करके संग्रहीत होता है . और जब आवेदन online वापस आ जाता है तो इसे CouchDB और logical servers के साथ synchronize किया जाता है .
PouchDB मे निम्नलिखित सबसे महत्वपूर्ण features हैं .
PouchDB द्वारा दी गई API प्रत्येक वातावरण में एक ही काम कर सकती है ताकि आप different browsers में एक PouchDB application चला सकें .
PouchDB एक बहुत ही हल्की API है आप script tag का उपयोग करके इसे बहुत आसानी से शामिल कर सकते हैं .
PouchDB को सीखना बहुत आसान है, लेकिन आपके पास कुछ programming skills होने चाहिए .
PouchDB एक opensource है और यह Github पर उपलब्ध है .
Firefox 29+ (Including Firefox OS and Firefox for Android)
Chrome 30+
Safari 5+
Internet Explorer 10+
Opera 21+
Android 4.0+
iOS 7.1+
Windows Phone 8+
आप Nouch.js command prompt में PouchDB constructor का उपयोग करके PouchDB में एक database बना सकते हैं . इसके लिये आपको एक parameter के रूप में database का नाम देना होगा .
new PouchDB(Database_name)
सबसे पहले Node.js का उपयोग करके PouchDB में एक database बनाने के लिए, सबसे पहले, आपको require () विधि का उपयोग करके PouchDB पैकेज की आवश्यकता होती है और फिर आप एक database बना सकते हैं .
//Requiring the package
var PouchDB = require('PouchDB');
//Creating the database object
var db = new PouchDB('First_Database');
console.log ("Database created Successfully.");
DB.put () विधि PouchDB डेटाबेस में एक document बनाने के लिए प्रयोग किया जाता है .PouchDB database में बनाए गए document को एक variable में संग्रहीत किया जाता है.
इस system को parameter के रूप में पास किया जाता है। यह विधि एक callback function को parameter के रूप में भी स्वीकार कर सकता है .
db.put(document, callback)
PouchDB में put () विधि का उपयोग करके एक document बनाते हैं .
आप जो document बनाते हैं वह JSON जैसा होना चाहिए comma (,) द्वारा अलग-अलग key-value जोड़े का एक सेट और curly braces ({}) के अंदर स्थित होना चाहिए.
//Requiring the package
var PouchDB = require('PouchDB');
//Creating the database object
var db = new PouchDB('Second_Database');
//Preparing the document
doc = {
_id : '001',
name: 'Altamas',
age : 28,
designation : 'Developer'
}
//Inserting Document
db.put(doc, function(err, response) {
if (err) {
return console.log(err);
} else {
console.log("Document created Successfully");
}
});
आप अलग रूप से संग्रहीत database (CouchDB) में एक document भी include कर सकते हो . आपको बस database के way को पारित करना होगा जहां आप database नाम के बजाय CouchDB में documents बनाना चाहते हो .
//Requiring the package
var PouchDB = require('PouchDB');
//Creating the database object
var db = new PouchDB('http://localhost:5984/my_database');
//Preparing the document
doc = {
_id : '001',
name: 'Altamas',
age : 22,
designation : 'Developer'
}
//Inserting Document
db.put(doc, function(err, response) {
if (err) {
return console.log(err);
} else {
console.log("Document created Successfully");
}
});
PouchDB में आप named info() का उपयोग करके database के बारे में base जानकारी प्राप्त कर सकते हैं .
db.info([callback])
Info() का उपयोग करके database जानकारी को पुन प्राप्त करने का एक तरीका है. यहां हम my_database नामक database की जानकारी प्रदर्शित कर रहे हैं error के मामले में error console पर प्रदर्शित की जाएगी .
//Requiring the package
var PouchDB = require('PouchDB');
//Creating the database object
var db = new PouchDB('First_Database');
//Database information
db.info(function(err, info) {
if (err) {
return console.log(err);
} else {
console.log(info);
}
});
PouchDB आपको एक database की जानकारी प्राप्त करने की सुविधा देता है जो server पर distant रूप से स्थित है . CouchDB आपको बस database नाम के बजाय CouchDB में आवश्यक database के way को पास करना होता है .
//Requiring the package
var PouchDB = require('PouchDB');
//Creating the database object
var db = new PouchDB('http://localhost:5984/my_database');
//Database information
db.info(function(err, info) {
if (err) {
return console.log(err);
} else {
console.log(info);
}
});
PouchDB में एक database को हटाने के लिए db.destroy () विधि का उपयोग किया जाता है . यह विधि एक callback function को parameter के रूप में स्वीकार करता है .
db.destroy()
Destroy() method का उपयोग करते हुए PouchDB में First_Database नामक database को हटा देता है .
//Requiring the package
var PouchDB = require('PouchDB');
//Creating the database object
var db = new PouchDB('First_Database');
//deleting database
db.destroy(function (err, response) {
if (err) {
return console.log(err);
} else {
console.log ("Database Deleted");
}
});
CouchDB से आप उस database को हटा सकते हैं जो server पर दूरस्थ रूप से संग्रहीत है. बस आपको सिर्फ CouchDB database का path देना होगा, जिसे आप database नाम के बदले हटाना चाहते हैं .
ऐसा करने के लिए, database नाम के जगह में आपको database को path पास करना होगा जो CouchDB में हटाया जाना आवश्यक होता है .
//Requiring the package
var PouchDB = require('pouchdb');
//Creating the database object
var db = new PouchDB('http://localhost:5984/my_database');
//deleting database
db.destroy(function (err, response) {
if (err) {
return console.log(err);
} else {
console.log("Database Deleted");
}
});
Database में बनाए गए document को पढ़ने या Recover करने के लिए db.get () विधि का उपयोग किया जाता है . यह विधि document id और वैकल्पिक callback function भी स्वीकार करता है .
db.get(document, callback)
PouchDB में एक document की सामग्री को get () method का उपयोग करते हुए पढ़ने का एक उदाहरण है .
//Requiring the package
var PouchDB = require('PouchDB');
//Creating the database object
var db = new PouchDB('Second_Database');
//Reading the contents of a Document
db.get('001', function(err, doc) {
if (err) {
return console.log(err);
} else {
console.log(doc);
}
});
PouchDB सर्वर पर Second_Database में संग्रहीत document को पढ़ा जाता है .
{ name: 'Altamas',
age: 21,
designation: 'Web Developer',
_id: '001',
_rev: '1-99a7a80ec2a74959885037a16d57924f' }
किसी remote database (CouchDB) से कोई document को read और retrieve प्राप्त कर सकते हैं . इसके लिए, आपको CouchDB में database का path देना होगा, जिसमें उस document को शामिल किया गया है जिसे आप database नाम के बदले में, पढ़ना चाहते हो .
//Requiring the package
var PouchDB = require('PouchDB');
//Creating the database object
var db = new PouchDB('http://localhost:5984/my_database');
//Reading the contents of a document
db.get('001', function(err, doc) {
if (err) {
return console.log(err);
} else {
console.log(doc);
}
});
PouchDB में एक document (_rev) का उपयोग करके अपडेट किया जा सकता है. जब हम PouchDB में कोई document बनाते हैं तो एक _rev उत्पन्न होता है .
इसे revision मार्कर कहा जाता है _rev का मान एक unique random संख्या है . जब भी हम document में परिवर्तन करते हैं तो _rev का मान बदला जाता है .
किसी document को अपडेट करने के लिए, हमें document के _rev मान को पुनर्प्राप्त करना होता है जिसे हम अपडेट करना चाहते हैं .
Document की सामग्री को पुन प्राप्त करने के लिए _rev number का उपयोग किया जाता है .
{ _id: '001',
_rev: '1-99a7a80ec2a74959885037a16d57924f' }
name: 'Altamas',
age: 28,
designation: 'Project Manager' }
और अब _rev का उपयोग करके "Age" को अपडेट करते है .
//Requiring the package
var PouchDB = require('PouchDB');
//Creating the database object
var db = new PouchDB('Second_Database');
//Preparing the document for update
doc = {
age: 21,
_id: '001',
_rev: '1-99a7a80ec2a74959885037a16d57924f'
}
//Inserting Document
db.put(doc);
//Reading the contents of a Document
db.get('001', function(err, doc) {
if (err) {
return console.log(err);
} else {
console.log(doc);
}
});
PouchDB डेटाबेस से एक document को हटाने के लिए DB.remove () method का उपयोग किया जाता है . Document को हटाने के लिए आपको id और _rev मान देना होगा.
और यह method एक callback function को स्वीकार करता है . आप id और _rev के जगह में पूर्ण document भी पास कर सकते हैं .
db.remove( doc_Id, doc_Rev, [callback] )
सबसे पहले एक document की value को प्राप्त करें फिर जिसे आप Read Document method का उपयोग करके जिस document को delete करना चाहते हो उसको delete कर सकते हो .
{ age: 21,
_id: '001',
_rev: '2-b26971720f274f1ab7234b3a2be93c83' }
यह document PouchDB में "Second_Database" नामक database के रूप मे store होता है .
अब, _rev value के साथ remove() method का उपयोग करके और document की id को लेते है .
//Requiring the package
var PouchDB = require('PouchDB');
//Creating the database object
var db = new PouchDB('Second_Database');
//Deleting an existing document
db.remove('001', '2-b26971720f274f1ab7234b3a2be93c83', function(err) {
if (err) {
return console.log(err);
} else {
console.log("Document deleted successfully");
}
});
अब हम एक folder PouchDB_Examples के name से बनाते है और उसके "Delete_Document.js नामक file में code को save कर लेते है .
node Delete_Document.js
PouchDB में database से Multiple/bulk documents को allDocs() method से read और retrieved किया जा सकता है .
PouchDB के allDocs () method के लिए syntax नीचे दिया गया है यह एक callback function को इस method में recognized किया जाता है .
db.allDocs()
database में सभी documents को पुन प्राप्त करने के लिए my_database नामक स्थानीय रूप से store है db.allDocs () system का उपयोग करते है .
documents की सरणी वस्तुओं के रूप में होती है इस system का उपयोग करके पुनर्प्राप्त की जाती है और प्रत्येक documents के materials प्राप्त करने के लिए हमें docs.rows के रूप में call करने की आवश्यकता होती है .
//Requiring the package
var PouchDB = require('PouchDB');
//Creating the database object
var db = new PouchDB('my_database');
//Retrieving all the documents in PouchDB
db.allDocs(function(err, docs) {
if (err) {
return console.log(err);
} else {
console.log (docs.rows);
}
});
अब हम Read_All_Document.js नामक एक file को बनाते है और उसमें उपरोक्त code को save कर लेते है .
C:\PouchDB_Examples >node Read_All_Document.js
सभी documents जो स्थानीय तौर पर store है my_database नामक database मे उनको इस तरह द्वारा पढ़ा जाता है .
[
{
id: '001',
key: '001',
value: { rev: '1-9dc57f5faa7ea90eeec22eba8bfd05f5' }
},
{
id: '002',
key: '002',
value: { rev: '1-9bf80afcedb9f8b5b35567292affb254' }
},
{
id: '003',
key: '003',
value: { rev: '1-1204f108e41bf8baf867856d5da16c57' }
}
]
AllDocs () method का उपयोग करके, प्रत्येक documents के केवल _id, key और _rev फ़ील्ड को देखा जाता है .
//Requiring the package
var PouchDB = require('PouchDB');
//Creating the database object
var db = new PouchDB('my_database');
//Retrieving all the documents in PouchDB
db.allDocs({include_docs: true}, function(err, docs) {
if (err) {
return console.log(err);
} else {
console.log (docs.rows);
}
});
PouchDB में documents की एक array (batch) बनाई जा सकती है Db.bulkDocs () method का उपयोग करके . documents बनाने के लिए इस system का उपयोग करते समय, PouchDB bulk में सभी documents के लिए unique id बनाता है, यदि id values हमारे द्वारा नहीं दी गयी हो
आप सभी documents को संग्रहीत कर सकते हैं जिन्हें आप PouchDB में एक array में बनाना चाहते हो और इसे एक parameter के रूप में इस method में पास कर सकते हैं . यह method एक callback function को parameter के रूप में भी स्वीकार करता है .
db.bulkDocs(docs, [options], [callback])
PouchDB में एक से अधिक documents बनाते हैं DB.bulkDocs () method का उपयोग करते हुए . Documents JSON के प्रारूप में होना चाहिए जो comma (,) द्वारा अलग किए गए key-value जोड़े का एक सेट और curly braces ({}) के अंदर स्थित होना चाहिए .
//Requiring the package
var PouchDB = require('PouchDB');
//Creating the database object
var db = new PouchDB('my_database');
//Preparing the documents array
doc1 = {_id: '001', name: 'Ram', age: 23, Designation: 'Programmer'}
doc2 = {_id: '002', name: 'Robert', age: 24, Designation: 'Programmer'}
doc3 = {_id: '003', name: 'Rahim', age: 25, Designation: 'Programmer'}
docs = [doc1, doc2, doc3]
//Inserting Documents
db.bulkDocs(docs, function(err, response) {
if (err) {
return console.log(err);
} else {
console.log("Documents created Successfully");
}
});
अब हम एक folder PouchDB_Examples के name से बनाते है और उसमे "Create_Batch.js नामक file में code को save कर लेते है .
node Create_Batch.js
bulkDocs() method का उपयोग करके, PouchDB में documents से एक array को एक बार में अपडेट किया जा सकता है .
इसके लिए, documents को एक array को create करना चाहिए, जहां _id, _rev और value को प्रत्येक documents के innovation के लिए मालूम हो .
हम my_database नामक database को PouchDB में देखते हैं . जो कि स्थानीय रूप से जमा है . database में 3 documents doc1, doc2, doc3 होते है .
{ id: '001',
key: '001',
value: { rev: '4-f59034f061004dbca22da61662459a16' },
doc:
{ name: 'Altamas',
age: 21,
Designation: 'Project Engineer',
_id: '001',
_rev: '4-f59034f061004dbca22da61662459a16' } },
{ id: '002',
key: '002',
value: { rev: '1-0c0628a46e404d90870f4e892dc2d900' },
doc:
{ name: 'Arif',
age: 23,
Designation: 'Web Developer',
_id: '002',
_rev: '1-0c0628a46e404d90870f4e892dc2d900' } },
{ id: '003',
key: '003',
value: { rev: '1-1dc4fe229a61420db2b657e8fcbbfa7d' },
doc:
{ name: 'Waseem',
age: 24,
Designation: 'software Engineer',
_id: '003',
_rev: '1-1dc4fe229a61420db2b657e8fcbbfa7d' } } ]
अपने concerned_id और _rev मानों का उपयोग करते हुए documents को update करते हैं .
//Requiring the package
var PouchDB = require('PouchDB');
//Creating the database object
var db = new PouchDB('Second_Database');
//Preparing the document
docs = [{_id : '001', _rev:'4-f59034f061004dbca22da61662459a16', age : 19, name: 'Akib', },
{_id : '002', _rev: '1-0c0628a46e404d90870f4e892dc2d900', age : 21, name: 'Rihan', },
{_id : '003', _rev: '1-1dc4fe229a61420db2b657e8fcbbfa7d', age : 17, name: 'Amreen', }]
//Updating the documents in bulk
db.bulkDocs(docs, function(err, response) {
if (err) {
return console.log(err);
} else {
console.log("Documents Updated Successfully");
}
});
अब हम एक folder PouchDB_Examples के name से बनाते है और उसमे "Update_Batch2.js" नामक file में code को save कर लेते है .
node Update_Batch2.js
C:\Users\Altamas Ali\Desktop\PouchDB_Examples>node Update_Batch2.js
Document (Batch) created Successfully
C:\Users\Altamas Ali\Desktop\PouchDB_Examples>
[ { id: '001',
key: '001',
value: { rev: '5-f1b552f9901a408e98406c424f368a49' },
doc:
{ age: 19,
name: 'Akib',
_id: '001',
_rev: '5-f1b552f9901a408e98406c424f368a49' } },
{ id: '002',
key: '002',
value: { rev: '2-4ecc64cd994d4a9cb0e4376cc5636b37' },
doc:
{ age: 21,
name: 'Rihan',
_id: '002',
_rev: '2-4ecc64cd994d4a9cb0e4376cc5636b37' } },
{ id: '003',
key: '003',
value: { rev: '2-01a70d54a8eb4181b17f5f6c9c2a0756' },
doc:
{ age: 17,
name: 'Amreen',
_id: '003',
_rev: '2-01a70d54a8eb4181b17f5f6c9c2a0756' } } ]
PouchDB में bulkDocs() method का उपयोग करके documents से एक array को एक बार में हटाया जा सकता है . इसमें documents से एक array create और delete किया जाता है . अगर आपको डेटाबेस को हटाना चाहते हो तो documents के _id और _rev मानों को अभी जानना होगा .आपको एक अन्य महत्वपूर्ण-मूल्य जोड़ी _deleted सत्य है .
doc1 = {_id: '001', name: 'Altamas', age: 21, Designation: 'Programmer'}
doc2 = {_id: '002', name: 'Ali', age: 22, Designation: 'Programmer'}
doc3 = {_id: '003', name: 'Rihan', age: 23, Designation: 'Programmer'}
सभी तीन documents को हटाने के लिये . इसके लिए उनके _rev values आवश्यकता होती है .
//Requiring the package
var PouchDB = require('PouchDB');
//Creating the database object
var db = new PouchDB('my_database');
//Retrieving all the documents in PouchDB
db.allDocs({include_docs: true},function(err, docs) {
if (err) {
return console.log(err);
} else {
console.log(docs.rows);
}
});
इस कोड को bulk_fetch.js के रूप में save किया जाता है उपरोक्त program को निष्पादित करने पर, database में documents की _id और _rev मानों को नीचे दिखाया गया है .
[
{
id: '001',
key: '001',
value: { rev: '1-1604b0c3ff69dc1e261265fd60808404' }
},
{
id: '002',
key: '002',
value: { rev: '1-b5e49db7e984841bf12a13e3ee548125' }
},
{
id: '003',
key: '003',
value: { rev: '1-a7b342786ecc707aa91f3b321a177b51' }
}
]
अब, अपने संबंधित _id और _rev मूल्यों का उपयोग करके documents को हटाया जा सकता है .
//Requiring the package
var PouchDB = require('PouchDB');
//Creating the database object
var db = new PouchDB('my_database');
//Preparing the document
docs = [{_id : '001', _rev: '2-77f3a9974dd578d12f3f2a33aae64c8d', _deleted : true },
{_id : '002', _rev: '2-43966007568ce9567c96422195fcfa0d', _deleted : true },
{_id : '003', _rev: '2-6c5349652527f4f39583ff14f23cd677',_deleted : true }]
//Deleting Documents
db.bulkDocs(docs, function(err, response) {
if (err) {
return console.log(err);
} else {
console.log(response+"Documents deleted Successfully");
}
});
अब हम एक folder PouchDB_Examples के name से बनाते है और उसमे " Delete_All_Document.js" नामक file में code को save कर लेते है .
C:\PouchDB_Examples >node Delete_All_Document.js
Documents Deleted Successfully
PouchDB में putAttachment () method का उपयोग करके एक document से एक binary object को engaged किया जा सकता है .
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 का उपयोग किया जाता है .
//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
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 होता है .
//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
हमारे पास एक 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
हम GetAttachment () method का उपयोग करके PouchDB से adjunct प्राप्त कर सकते हैं और यह method हमेशा blob या buffer object देता है .
db.getAttachment( docId, attachmentId, [callback] );
Last_Database नामक database से "002" document से attachment Txt पुनर्प्राप्त करने के लिए getAttachment () का उपयोग करते है.
//Requiring the package
var PouchDB = require('PouchDB');
//Creating the database object
var db = new PouchDB('my_database');
//Retrieving an attachment from a document
db.getAttachment('001', 'att_1.txt', function(err, blob_buffer) {
if (err) {
return console.log(err);
} else {
console.log(blob_buffer);
}
});
हम एक folder PouchDB_Examples के name से बनाते है और उसमे "Read_Attachment.js" नामक file में code को save कर लेते है .
C:\PouchDB_Examples >node Retrieve_Attachment.js
<Buffer 00>
Delete किये गए attachment () का उपयोग करके PouchDB से एक attachment को हटाया जा सकता है .
removeAttachment() method एक syntax है . इस method के लिए हमें document id, attachment id, and _rev value को देना होता है . यह method एक alternative callback function को भी स्वीकार करता है .
db.removeAttachment ( docId, attachmentId, rev, [callback] );
हमारे पास id 002 के साथ PouchDB में एक document है, जिसमें attachment वाले एक कर्मचारी का id, name, age, पद शामिल है .
{ name: 'Altamas',
age: 22,
designation: 'Developer',
_attachments:
{ 'attachment1.txt':
{ content_type: 'text/plain',
revpos: 2,
digest: 'md5-k7iFrf4NoInN9jSQT9WfcQ==',
data: 'AA==' } },
_id: '002',
_rev: '2-388510d44393457cb06764dd89542ef3' }
अब हम removeAttachment() method का उपयोग करके attachment को हटाते है .
//Requiring the package
var PouchDB = require('PouchDB');
//Creating the database object
var db = new PouchDB('Last_Database');
db.removeAttachment('002', 'attachment_1.txt', '2-388510d44393457cb06764dd89542ef3',
function(err, res) {
if (err) {
return console.log(err);
} else {
console.log(res+"Attachment Deleted successfully")
}
});
अब हम एक folder PouchDB_Examples के name से बनाते है और उसमे "Delete_Attachment.js" नामक file में code को save कर लेते है .
node Delete_Attachment.js
आप इसको verify कर सकते हो read command का उपयोग करके की document से attachment को deleted कर दिया गया है .
C:\Users\Altamas Ali\Desktop\PouchDB_Examples>node Read_Document2.js
{ name: 'Altamas',
age: 22,
designation: 'Developer',
_id: '002',
_rev: '1-06272bacc14146d68d8ee0c36dfa0cb9'}
C:\Users\Altamas Ali\Desktop\PouchDB_Examples>
Replication PouchDB की सबसे महत्वपूर्ण विशेषताओं में से एक है . यह आपको डेटाबेस की duplicate copy बनाने की सुविधा प्रदान करता है .
इसे आप स्थानीय रूप से संग्रहीत PouchDB उदाहरण या किसी दूरस्थ CouchDB उदाहरण को दोहराने के लिए दूरस्थ रूप से store कर सकते हो.
PouchDB.replicate(source, target, [options])
Source और targets PouchDB का उदाहरण या CouchDB का उदाहरण हो सकते हैं .
PouchDB में नाम sample_database के साथ एक डेटाबेस होता है और इसमें 3 documents doc1, doc2, और doc3 शामिल होते है .
doc1 = {_id: '001', name: 'Ram', age: 23, Designation: 'Programmer'}
doc2 = {_id: '002', name: 'Robert', age: 24, Designation: 'Programmer'}
doc3 = {_id: '003', name: 'Rahim', age: 25, Designation: 'Programmer'}
sample_database नामक डेटाबेस की duplicate copy बनाता है जो कि स्थानीय स्तर पर CouchDB में Store है .
//Requiring the package
var PouchDB = require('PouchDB');
var localdb = 'sample_database';
//Creating remote database object
var remotedb = 'http://localhost:5984/sample_database';
//Replicating a local database to Remote
PouchDB.replicate(localDB, remoteDB);
console.log ("Database replicated successfully");
अब हम एक folder PouchDB_Examples के name से बनाते है और उसमे "Replication_example.js" नामक file में code को save कर लेते है .
C:\PouchDB_Examples >node Replication_example.js
C:\Users\Altamas Ali\Desktop\PouchDB_Examples>node Replication_example.js
Database replicated Successfully
C:\Users\Altamas Ali\Desktop\PouchDB_Examples>
CouchDB सर्वर पर "remotedb" नाम से एक डेटाबेस बनाते है जिसमें तीन documents doc1, doc2, और doc3, के साथ mentioned के रूप में सामग्री है .
doc1 = {_id: '001', name: 'Altamas', age: 20, Designation: 'Web Developer'}
doc2 = {_id: '002', name: 'Amreen', age: 22, Designation: 'Software Engineer'}
doc3 = {_id: '003', name: 'Naira', age: 28, Designation: 'Project Manager'}
CouchDB में संग्रहीत Remote_Database नाम से डेटाबेस की duplicate copy बनाने के लिए एक उदाहरण नीचे दिया गया है .
//Requiring the package
var PouchDB = require('PouchDB');
var localdb = 'sample_database';
var remotedb = 'http://localhost:5984/sample_database1';
//Replicating a local database to Remote
PouchDB.replicate(remotedb, localdb);
console.log("Database replicated successfully");
अब हम एक folder PouchDB_Examples के name से बनाते है और उसमे "Replication_example2.js" नामक file में code को save कर लेते है .
C:\PouchDB_Examples >node Replication_example2.js
Database replicated successfully
इस कोड को execute करके आप यह सत्यापित कर सकते हैं कि डेटाबेस को आपके पाच उदाहरण में दोहराया गया है या नहीं .
//Requiring the package
var PouchDB = require('PouchDB');
//Creating the database object
var db = new PouchDB('remote_database');
//Retrieving all the documents in PouchDB
db.allDocs({include_docs: true, attachments: true}, function(err, docs) {
if (err) {
return console.log(err);
} else {
console.log(docs.rows);
}
});
यदि उपरोक्त कोड execute करने पर डेटाबेस को दोहराया गया है तो आपको नीचे दिखाए अनुसार दोहराए गए डेटाबेस की सामग्री मिल जाएगी .
[
{
id: '001',
key: '001',
value: { rev: '1-23cf3767e32a682c247053b16caecedb' },
doc: {
name: 'Geeta',
age: 25,
Designation: 'Programmer',
_id: '001',
_rev: '1-23cf3767e32a682c247053b16caecedb'
}
},
{
id: '002',
key: '002',
value: { rev: '1-d5bcfafbd4d4fae92fd7fc4fdcaa3a79' },
doc: {
name: 'Zara Ali',
age: 24,
Designation: 'Manager',
_id: '002',
_rev: '1-d5bcfafbd4d4fae92fd7fc4fdcaa3a79'
}
},
{
id: '003',
key: '003',
value: { rev: '1-c4cce025dbd30d21e40882d41842d5a4' },
doc: {
name: 'Mary',
age: 23,
Designation: 'Admin',
_id: '003',
_rev: '1-c4cce025dbd30d21e40882d41842d5a4'
}
}
]