PouchDB

0
75

PouchDB – Oversee

This chapter provides a short introduction to PouchDB adurationy with it is features and how it works.

Wmind use is PouchDB?

PouchDB is an open up source in-blineser database API composed in JavaScript. It is modeldelivered after Couch DB – a NoSQL database. Uperform this particular particular API, we can produce applications tmind use work away fromrange and onrange. It internally uses WebSQL and IndexedDB to store data.

How Does it Work?

In PouchDB, when the application is away fromrange, the data is stocrimson-coloucrimson locally uperform WebSQL and IndexedDB in the blineser. When the application is back onrange, it is synchronized with CouchDB and compatible servers.

Uperform PouchDB, you can communicate with both local and remote databases oceanmlessly withaway noticing any difference.

Features of PouchDB

Folloearng are the features of PouchDB −

  • Cross Blineser − The API provided simply by PouchDB works the exbehave exact same in every environment, generally therefore, we can operate a PouchDB application in various blinesers.

  • Light Weight − PouchDB is an extremely light-weight API, it is furthermore included easily just uperform a script tag.

  • Easy to Learn − If you have a prior knowdeliveredge of any programming language, it is easy to learn PouchDB.

  • Open Source − PouchDB is an Open Source Application and is available on GitHub.

Advantages of PouchDB

Folloearng are the advantages of PouchDB −

  • Since PouchDB reparts inpart the blineser, generally there is no need to perform queries over the network, this particular particular results in quicker execution of queries.

  • You can synchronize the data with any of the supinterfaceed server and simply by doing so you can operate apps both onrange and away fromrange.

Blinesers tmind use Supinterface PouchDB

Folloearng are the blinesers tmind use supinterface PouchDB −

  • 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+

PouchDB – Environment

This chapter exfundamentals how to download and install PouchDB in your own system.

Installing PouchDB

In order to work with PouchDB, you need to download the file .js file and include it in your own script. Folloearng are the steps to install PouchDB.

Step 1

Visit the homepage of PouchDB website, simply by clicruler the folloearng link −

https://PouchDB.com/

PouchDB Homepage

Step 2

Click the Download button on the top right hand part of the web page as shown in the above screenshot. This will download PouchDB-5.3.0.min.js in your own system.

Step 3

Copy and paste the PouchDB-5.3.0.min.js to your own worruler immediateory and include it in your own JavaScript as shown in the folloearng command.

<script src = "PouchDB-5.3.0.min.js"></script>

Installing Pouch Uperform Node.js

You can furthermore install PouchDB as Node.js module. Folloearng are the steps to install PouchDB uperform Node.js.

Step 1

Install Node.js simply by folloearng the steps given in the Installing Node.js section of our caway fromee script tutorial.

Step 2

Open the command prompt and execute the folloearng command. This will install PouchDB node module in your own system.

npm install --save PouchDB

Downloading CouchDB

When away fromrange, PouchDB stores data locally and works like an app. You can access it onrange simply by connecting with compatible servers. As we know PouchDB can end up being connected to CouchDB, so, enables install CouchDB too. Folloearng are the steps to install CouchDB.

Step 1

The away fromicial website for CouchDB is http://couchdb.apache.org. If you click the given link, you can get the home page of CouchDB away fromicial website as shown in the folloearng screenshot.

Download CouchDB

Step 2

If you click on the download button tmind use will lead to a page where the download links of CouchDB in various formats are provided. The folloearng snapshot illustrates the exbehave exact same.

CouchDB Download Links Formats

Step 3

Choose the download link for Windows Systems and select one of the provided mirrors to start your own download.

Installing CouchDB

A earndows executable setup-couchdb-1.6.1_R16B02.exe file will end up being downloaded on your own system. Run the setup file and proceed with the installation.

After installing CouchDB in your own system successcompenableey, open up the folder where CouchDB was instaldelivered, go to the bin folder, and start the server simply by operatening a script file named couchdb.bat.

After installation, open up built-in web interface of CouchDB simply by visiting the folloearng link − http://127.0.0.1:5984/. If everyslimg goes great, this particular particular will give you a web page, which will have the folloearng awayplace.

{                 
   "couchdb":"Welcome","uuid":"c8d48ac61bb497f4692b346e0f400d60",
   "version":"1. 6.1",
   "vendor": {
      "version":"1.6.1","name":"The Apache Softbattlee Foundation"
   } 
}

You can interbehave with CouchDB web interface simply by uperform the folloearng URL −

http://127.0.0.1:5984/_utils/

This shows you the index page of Futon, which is the web interface of CouchDB.

Web Interface

PouchDB – Create Database

You can produce a database in PouchDB uperform the PouchDB constructor.

Syntax

Folloearng is the syntax of uperform the PouchDB constructor. To this particular particular, you need to pass the name of the database as a parameter.

brand new PouchDB(Database_name)

Example

To produce a database in PouchDB uperform node, initial of all, you need to require the PouchDB package uperform the require() method and then you can produce a database as shown in the folloearng example.

//Requiring the package
var PouchDB = require('PouchDB');

//Creating the database object
var db = brand new PouchDB('my_database');
console.log ("Database produced Successcompenableey.");

Save the above code in a file with the name Create_Database.js. Open the command prompt and execute the JavaScript file uperform node as shown end up beinglow.

C:PouchDB_Examples>node Create_Database.js

This will produce a database locally (you can see the folder in the current immediateory) displaying the folloearng message.

Database produced Successcompenableey.

PouchDB – Database Info

You can get the easy information abaway the database uperform the method named info()

Syntax

Folloearng is the syntax of uperform the info() method of PouchDB. This method accepts a callback function.

db.info([callback])

Example

Folloearng is an example of retrieving database information uperform the info() method. Here, we are displaying the information of the database named my_database. In case of error, the error will end up being displayed on the console.

//Requiring the package
var PouchDB = require('PouchDB');

//Creating the database object
var db = brand new PouchDB('my_database');

//Database information
db.info(function(err, info) {
   if (err) {
      return console.log(err);
   } else {
      console.log(info);
   }
});

Save the above code in a file with the name Database_info.js. Open the command prompt and execute the JavaScript file uperform node as shown end up beinglow.

C:PouchDB_Examples>node Database_info.js

This will display the info of the specified database as follows.

{ 
   doc_count: 0,
   update_seq: 0,
   backend_adapter: 'LevelDOWN',
   db_name: 'my_database',
   auto_compbehaveion: false,
   adapter: 'leveldb' 
}

Remote Database Info

In the exbehave exact same way, you get the information of a database tmind use is saved remotely on the server (CouchDB). To do so, instead of database name, you need to pass the route to the requicrimson-coloucrimson database in CouchDB.

Example

Folloearng is an example of retrieving information of a database tmind use is saved in the CouchDB server. This code gives you information of a database named my_database.

//Requiring the package
var PouchDB = require('PouchDB');

//Creating the database object
var db = brand new PouchDB('http://localhost:5984/my_database');

//Database information
db.info(function(err, info) {
   if (err) {
      return console.log(err);
   } else {
      console.log(info);
   }
});

Save the above code in a file with the name Database_ Remote_info.js. Open the command prompt and execute the JavaScript file uperform node as shown end up beinglow.

C:PouchDB_Examples>node Database_Remote_info.js

This will display the info of the specified database as follows.

{ 
   db_name: 'my_database', 
   doc_count: 0, 
   doc_del_count: 0, 
   update_seq: 0, 
   purge_seq: 0, 
   compbehave_operatening: false, 
   disk_dimension: 79, 
   data_dimension: 0, 
   instance_start_time: '1458209191708486', 
   disk_format_version: 6, 
   committed_update_seq: 0, 
   host: 'http://localhost:5984/my_database/', 
   auto_compbehaveion: false, 
   adapter: 'http' 
}

PouchDB – Deenablee Database

You can deenablee a database in PouchDB uperform the db.destroy() method.

Syntax

Folloearng is the syntax of uperform the db.destroy() method. This method accepts a callback function as a parameter.

db.destroy()

Example

Folloearng is an example of deenableing a database in PouchDB uperform the destroy() method. Here, we are deenableing the database named my_database, produced in the previous chapters.

//Requiring the package
var PouchDB = require('PouchDB');

//Creating the database object
var db = brand new PouchDB('my_database');

//deenableing database
db.destroy(function (err, response) {
   if (err) {
      return console.log(err);
   } else {
      console.log ("Database Deenableed”);
   }
});

Save the above code in a file with the name Deenablee_Database.js. Open the command prompt and execute the JavaScript file uperform node as shown end up beinglow.

C:PouchDB_Examples >node Deenablee_Database.js

This will deenablee the database named my_database which is stocrimson-coloucrimson locally displaying the folloearng message.

Database Deenableed

Deenableing a Remote Database

In the exbehave exact same way, you can deenablee a database tmind use is stocrimson-coloucrimson remotely on the server (CouchDB).

To do so, instead of a database name, you need to pass the route to the database tmind use is requicrimson-coloucrimson to end up being deenableed, in CouchDB.

Example

Suppose generally there is a database named my_database in the CouchDB server. Then, if you verify the list of databases in CouchDB uperform the URL http://127.0.0.1:5984/_utils/index.html you will get the folloearng screenshot.

Deenableing Remote Database

Folloearng is an example of deenableing a database named my_database tmind use is saved in the CouchDB server.

//Requiring the package
var PouchDB = require('pouchdb');

//Creating the database object
var db = brand new PouchDB('http://localhost:5984/my_database');

//deenableing database
db.destroy(function (err, response) {
   if (err) {
      return console.log(err);
   } else {
      console.log("Database Deenableed");
   }
});

Save the above code in a file with the name Remote_Database_Deenablee.js. Open the command prompt and execute the JavaScript file uperform node as shown end up beinglow.

C:PouchDB_Examples >Remote_Database_Deenablee.js

This deenablees the specified database from PouchDB displaying the folloearng message.

Database Deenableed

Verification

After executing the above program, if you visit the URL again, you will get the folloearng screenshot. Here you can observe only two databases since my_database was deenableed.

Deenablee Database Verification

PouchDB – Create Document

You can produce a document in PouchDB uperform the db.place() method.

Syntax

Folloearng is the syntax of uperform the db.place() method of PouchDB. You can store the document tmind use is to end up being produced in PouchDB, in a variable and pass as a parameter to this particular particular method. In addition, this particular particular method furthermore accepts a callback (optional) function as a parameter.

db.place(document, callback)

Example

Folloearng is an example of creating a document in PouchDB uperform the place() method. The document we produce need to end up being of JSON format, a set of key-value pairs separated simply by comma (,) and enneard wislim curly braces ({}).

//Requiring the package
var PouchDB = require('PouchDB');

//Creating the database object
var db = brand new PouchDB('my_database');

//Preparing the document
doc = {
   _id : '001',
   name: 'Raju',
   age : 23,
   designation : 'Designer'
   }
//Inserting Document
db.place(doc, function(err, response) {
   if (err) {
      return console.log(err);
   } else {
      console.log("Document produced Successcompenableey");
   }
});

Save the above code in a file with name Create_Document.js. Open the command prompt and execute the JavaScript file uperform node as shown end up beinglow.

C:PouchDB_Examples >node Create_Document.js

This produces the given document in PouchDB database named my_database, which is stocrimson-coloucrimson locally, displaying the folloearng message.

Document produced Successcompenableey

Inserting a Document in a Remote Database

You can furthermore insert a document in the database tmind use is stocrimson-coloucrimson remotely on the server (CouchDB).

To do so, instead of database name you need to pass the route to the database where you like to produce documents in CouchDB.

Example

Suppose generally there is a database named my_database in the CouchDB server. Then, if you verify the list of databases in CouchDB uperform the URL http://127.0.0.1:5984/_utils/index.html you will get the folloearng screenshot.

Inserting a Document in a Remote Database

Now, if you click on the database named my_database, you will find an empty database as shown in the folloearng screenshot.

Empty Database

Folloearng is an example of inserting a document in a database named my_database tmind use is saved in the CouchDB server.

//Requiring the package
var PouchDB = require('PouchDB');

//Creating the database object
var db = brand new PouchDB('http://localhost:5984/my_database');

//Preparing the document
doc = {
   _id : '001',
   name: 'Raju',
   age : 23,
   designation : 'Designer'
   }
//Inserting Document
db.place(doc, function(err, response) {
   if (err) {
      return console.log(err);
   } else {
      console.log("Document produced Successcompenableey");
   }
});

Save the above code in a file with the name Remote_Create_Document.js. Open the command prompt and execute the JavaScript file uperform node as shown end up beinglow.

C:PouchDB_Examples >node Remote_Create_Document.js

This produces the given document in PouchDB database named my_database which is stocrimson-coloucrimson in CouchDB, displaying the folloearng message.

Document produced Successcompenableey

Verification

After executing the above program, if you visit the my_database again, you can observe the document produced as shown in the folloearng screenshot.

Remote Database Verification

PouchDB – Read Document

You can read/retrieve the contents of a document in PouchDB uperform the db.get() method.

Syntax

Folloearng is the syntax of uperform the db.get() method of PouchDB. This method accepts the document id and an optional callback function.

db.get(document, callback)

Example

Folloearng is an example of reading the contents of a document in PouchDB uperform the get() method.

//Requiring the package
var PouchDB = require('PouchDB');

//Creating the database object
var db = brand new PouchDB('my_database');

//Reading the contents of a Document
db.get('001', function(err, doc) {
   if (err) {
      return console.log(err);
   } else {
      console.log(doc);
   }
});

Save the above code in a file with name Read_Document.js. Open the command prompt and execute the JavaScript file uperform node as shown in the folloearng command.

C:PouchDB_Examples >node Read_Document.js

This reads the contents of the given document tmind use exists in the database named my_database which is stocrimson-coloucrimson locally. The folloearng message gets displayed on the console.

{
   name: 'Raju',
   age: 23,
   designation: 'Designer',
   _id: '001',
   _rev: '1-ba7f6914ac80098e6f63d2bfb0391637'
}

Reading a Document from a Remote Database

You can furthermore read a document from the database tmind use is stocrimson-coloucrimson remotely on the server (CouchDB).

To do so, instead of a database name, you need to pass the route to the database in CouchDB, which contains the document tmind use is to end up being read.

Example

Suppose, generally there is a database named my_database in the CouchDB server. Then, if you verify the list of databases in CouchDB uperform the URL http://127.0.0.1:5984/_utils/index.html you will get the folloearng screenshot.

Reading a Document from a Remote Database

By clicruler on the database named my_database you can see the folloearng screenshot. Here, you can observe tmind use this particular particular database contains a document with id 001.

Reading a Document

Folloearng is an example of reading the contents of the document having id as “001” tmind use exists in a database named my_database, which is stocrimson-coloucrimson in the CouchDB server.

//Requiring the package
var PouchDB = require('PouchDB');

//Creating the database object
var db = brand 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);
   }
});

Save the above code in a file with the name Remote_Read_Document.js. Open the command prompt and execute the JavaScript file uperform node as shown end up beinglow.

C:PouchDB_Examples >node Remote_Read_Document.js

This reads the contents of the given document tmind use exists in the database named my_database which is stocrimson-coloucrimson in CouchDB. The folloearng message is displayed on the console.

{ 
   _id: '001',
   _rev: '3-552920d1ca372986fad7b996ce365f5d',
   name: 'Raju',
   age: 23,
   designation: 'Designer' 
}

PouchDB – Update Document

Whenever, we produce a document in PouchDB, a brand new field _rev is generated, and it is known as revision marker. The _rev‘s value is a unique random numend up beingr, every time we produce modifys to the document the value of _rev is modifyd.

You can update an existing document in PouchDB uperform the (_rev). To do so, initial of all retrieve the _rev value of the document we want to update. Now, place the contents tmind use are to end up being updated adurationy with the retrieved _rev value in a brand new document, and finally insert this particular particular document in PouchDB uperform the place() method.

Example

Assume we have a document in PouchDB with id 001 which has details of a person. In order to update this particular particular document, we need to have it is rev numend up beingr. Therefore, to retrieve the contents of the document the folloearng code is used.

//Requiring the package
var PouchDB = require('PouchDB');

//Creating the database object
var db = brand new PouchDB('my_database');

//Reading the contents of a Document
db.get('001', function(err, doc) {
   if (err) {
      return console.log(err);
   } else {
      console.log(doc);
   }
});

On executing the above code, you will receive the folloearng awayplace.

{
   _id: '001',
   _rev: '3-552920d1ca372986fad7b996ce365f5d',
   name: 'Raju',
   age: 23,
   designation: 'Designer' 
}

Now, uperform the _rev you can update the value of the key “age” to 26, as shown in the folloearng code.

//Requiring the package
var PouchDB = require('PouchDB');

//Creating the database object
var db = brand new PouchDB('my_database');

//Preparing the document for update
doc = {
   age: 26,
   _rev: '3-552920d1ca372986fad7b996ce365f5d',
   }

//Inserting Document
db.place(doc);

//Reading the contents of a Document
db.get('001', function(err, doc) {
   if (err) {
      return console.log(err);
   } else {
      console.log(doc);
   }
});

Save the above code in a file with the name Update_Document.js. Open the command prompt and execute the JavaScript file uperform node as shown end up beinglow.

C:Pouch_Examples>node Update_Document.js

This updates the contents of the given document tmind use exists in the database named my_database which is stocrimson-coloucrimson locally. The folloearng message is displayed on the console.

{ 
   name: 'Raju',
   age: 26,
   designation: 'Designer',
   _id: '001',
   _rev: '2-61b523ccdc4e41a8435bdffbb057a7a5' 
}

Updating a Document in a Remote Database

You can furthermore update an existing document in a database tmind use is stocrimson-coloucrimson remotely on the server (CouchDB).

To do so, instead of a database name you need to pass the route to the database in CouchDB, which contains the document tmind use is to end up being updated.

Example

Suppose generally there is a database named my_database in the CouchDB server. Then, if you verify the list of databases in CouchDB uperform the URL http://127.0.0.1:5984/_utils/index.html you will get the folloearng screenshot.

Updating a Document in a Remote Database

By clicruler on the database named my_database, you can see the folloearng screenshot. Here, you can observe tmind use this particular particular database contains a document with id 001.

Updating Database

Folloearng is an example of updating the age of the document having id as “001” tmind use exists in a database named my_database which is stocrimson-coloucrimson in the CouchDB server.

//Requiring the package
var PouchDB = require('PouchDB');

//Creating the database object
var db = brand new PouchDB('http://localhost:5984/my_database');

//Preparing the document for update
doc = {
   age: 26,
   _rev: '3-552920d1ca372986fad7b996ce365f5d',
   }

//Inserting Document
db.place(doc);

//Reading the contents of a Document
db.get('001', function(err, doc) {
   if (err) {
      return console.log(err);
   } else {
      console.log(doc);
   }
});   

Save the above code in a file with the name Remote_Update_Document.js. Open the command prompt and execute the JavaScript file uperform node as shown end up beinglow.

C:PouchDB_Examples >node Remote_Update_Document.js

This updates the contents of the given document tmind use exists in the database named my_database which is stocrimson-coloucrimson in CouchDB. The folloearng message is displayed on the console.

{
   _id: '001',
   _rev: '2-b9640bffbce582c94308905eed8bb545',
   name: 'Raju',
   age: 26,
   designation: 'Designer' 
}

PouchDB – Deenablee Document

You can deenablee a document from a database tmind use exists in PouchDB uperform the db.remove() method.

Syntax

Folloearng is the syntax of uperform the db.remove() method of PouchDB. To this particular particular method, we have to pass id and _rev to deenablee an existing document as shown in the folloearng code. This method accepts an optional callback function. We can furthermore pass the compenablee document instead of id and _rev.

db.remove( docId, docRev, [callback] )

Example

Assume we have a document in PouchDB with id 001 which have the details of a person. In order to deenablee this particular particular document adurationy with it is id we need to furthermore have it is _rev numend up beingr. Therefore, retrieve the contents of the document as shown in the folloearng code.

//Requiring the package
var PouchDB = require('PouchDB');

//Creating the database object
var db = brand new PouchDB('my_database');

//Reading the contents of a Document
db.get('001', function(err, doc) {
   if (err) {
      return console.log(err);
   } else {
      console.log(doc);
   }
});

Executing the above code gives the folloearng awayplace.

{
   _id: '001',
   _rev: '3-552920d1ca372986fad7b996ce365f5d',
   name: 'Raju',
   age: 23,
   designation: 'Designer' 
}

Now, uperform the _rev and id of the document you can deenablee this particular particular simply by uperform the remove() method as shown in the folloearng code.

//Requiring the package
var PouchDB = require('PouchDB');

//Creating the database object
var db = brand new PouchDB('my_database');

//Deenableing an existing document
db.remove('001', '3-552920d1ca372986fad7b996ce365f5d', function(err) {
   if (err) {
      return console.log(err);
   } else {
      console.log("Document deenableed successcompenableey");
   }
});

Save the above code in a file with the name Deenablee_Document.js. Open the command prompt and execute the JavaScript file uperform node as shown end up beinglow.

C:PouchDB_Examples >node Deenablee_Document.js

This deenablees the contents of the given document tmind use exists in the database named my_database which is stocrimson-coloucrimson locally. The folloearng message is displayed.

Document deenableed successcompenableey

Deenableing a Document from a Remote Database

You can furthermore deenablee an existing document from the database tmind use is stocrimson-coloucrimson remotely on the server (CouchDB).

To do so, instead of a database name, you need to pass the route to the database in CouchDB, which contains the document tmind use is to end up being read.

Example

Suppose generally there is a database named my_database in the CouchDB server. Then, if you verify the list of databases in CouchDB uperform the URL http://127.0.0.1:5984/_utils/index.html you will get the folloearng screenshot.

Deenableing a Document from a Remote Database

By clicruler on the database named my_database you can see the folloearng screenshot. Here, you can observe tmind use the database contains a document with id 001.

Deenableing Database

Folloearng is an example of deenableing the contents of the document having id “001” tmind use exists in a database named my_database which is stocrimson-coloucrimson in the CouchDB server.

//Requiring the package
var PouchDB = require('PouchDB');

//Creating the database object
var db = brand new PouchDB('http://localhost:5984/my_database');

//Deenableing an existing document
db.remove('001', '3-552920d1ca372986fad7b996ce365f5d', function(err) {
   if (err) {
      return console.log(err);
   } else {
      console.log("Document deenableed successcompenableey");
   }
});

Save the above code in a file with name Remote_Deenablee_Document.js. Open the command prompt and execute the JavaScript file uperform node as shown end up beinglow.

C:PouchDB_Examples >node Remote_Deenablee_Document.js

This deenablees the given document tmind use exists in the database named my_database which is stocrimson-coloucrimson in CouchDB. The folloearng message is displayed.

Document deenableed successcompenableey

PouchDB – Create Batch

You can produce an array (batch) of documents in PouchDB uperform the db.bulkDocs() method. While creating documents, uperform this particular particular method if we do not provide _id values, on our end up beinghalf PouchDB generates unique ids for all the documents in the bulk.

Syntax

Folloearng is the syntax of uperform the db.bulkDocs() method of PouchDB. You can store all the documents tmind use are to end up being produced in PouchDB in an array and pass it to this particular particular method as a parameter. In addition to it, this particular particular method furthermore accepts a callback (optional) function as a parameter.

db.bulkDocs(docs, [options], [callback])

Example

Folloearng is an example of creating multiple documents in PouchDB uperform the db.bulkDocs () method. The documents we produce need to end up being of JSON format, a set of key-value pairs separated simply by comma (,) and enneard wislim curly braces ({}).

//Requiring the package
var PouchDB = require('PouchDB');

//Creating the database object
var db = brand new PouchDB('my_database');

//Preparing the documents array
doc1 = {_id: '001', name: 'Ram', age: 23, Designation: 'Programmer'}
doc2 = {_id: '002', name: 'Roend up beingrt', 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 produced Successcompenableey");
   }
});

Save the above code in a file with name Create_Batch.js. Open the command prompt and execute the JavaScript file uperform node as shown end up beinglow.

C:PouchDB_Examples >node Create_Batch.js

This produces the given document in PouchDB database named my_database which is stocrimson-coloucrimson locally. The folloearng message gets displayed.

Documents produced Successcompenableey

Inserting a Batch in a Remote Database

You can insert an array of documents in the database tmind use is stocrimson-coloucrimson remotely on the server (CouchDB).

To do so, instead of a database name you need to pass the route to the database where we want to produce documents in CouchDB.

Example

Suppose generally there is a database named my_database in the CouchDB server. Then, if you verify the list of databases in CouchDB uperform the URL http://127.0.0.1:5984/_utils/index.html you will get the folloearng screenshot.

Inserting a Batch in a Remote Database

Folloearng is an example of inserting an array of documents in the database named my_database which is saved in the CouchDB server.

//Requiring the package
var PouchDB = require('PouchDB');

//Creating the database object
var db = brand new PouchDB('http://localhost:5984/my_database');

//Preparing the documents array

doc1 = {_id: '001', name: 'Ram', age: 23, Designation: 'Programmer'}
doc2 = {_id: '002', name: 'Roend up beingrt', 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 produced Successcompenableey");
   }
});

Save the above code in a file with the name Remote_Create_Batch.js. Open the command prompt and execute the JavaScript file uperform node as shown end up beinglow.

C:PouchDB_Examples >node Remote_Create_Batch.js

This produces the given documents in PouchDB database named my_database which is stocrimson-coloucrimson in CouchDB. The folloearng message is displayed.

Document produced Successcompenableey

Verification

After executing the above program if you visit the my_database again, you can observe the documents produced as shown in the folloearng screenshot.

Inserting Batch Verification

PouchDB – Fetch Batch

You can read/retrieve multiple/bulk documents from a database in PouchDB uperform the allDocs() method.

Syntax

Folloearng is the syntax of uperform the db.allDocs() method of PouchDB. This method accepts an optional callback function.

db.allDocs()

Example

Folloearng is an example of retrieving all the documents in a database named my_database tmind use is stocrimson-coloucrimson locally, uperform db.allDocs() method. This method retrieves the array of documents in the form of objects, to get the contents of every document you need to call as docs.lines.

//Requiring the package
var PouchDB = require('PouchDB');

//Creating the database object
var db = brand 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.lines);
   }
});

Save the above code in a file with the name Read_All_Document.js. Open the command prompt and execute the JavaScript file uperform node as shown end up beinglow.

C:PouchDB_Examples >node Read_All_Document.js

This reads all the documents tmind use exists in the database named my_database which is stocrimson-coloucrimson locally. The folloearng message is displayed on the console.

[ 
   { 
      id: '001',
      key: '001',
      value: { rev: '1-9dc57f5faa7ea90eeec22eba8bfd05f5' } 
   },
   { 
      id: '002',
      key: '002',
      value: { rev: '1-9bf80afcedb9f8b5b35567292affb254' } 
    },
   { 
      id: '003',
      key: '003',
      value: { rev: '1-1204f108e41bf8baf867856d5da16c57' } 
   }
]

In general, as shown in the above result, uperform allDocs() method you can see only the _id, key and _rev fields of every document. However, to include the whole document in the result, you have to produce the optional parameter include_docs true as shown end up beinglow.

//Requiring the package
var PouchDB = require('PouchDB');

//Creating the database object
var db = brand 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.lines);
   }
});

Executing the above code gives you a list of compenablee documents in the specified documents as shown in the folloearng code.

[ 
   { 
      id: '001',
      key: '001',
      value: { rev: '1-9dc57f5faa7ea90eeec22eba8bfd05f5' },
      doc: { 
         name: 'Ram',
         age: 23,
         Designation: 'Programmer',
         _id: '001',
         _rev: '1-9dc57f5faa7ea90eeec22eba8bfd05f5' 
      } 
   },
   { 
      id: '002',
      key: '002',
      value: { rev: '1-9bf80afcedb9f8b5b35567292affb254' },
      doc: { 
         name: 'Roend up beingrt',
         age: 24,
         Designation: 'Programmer',
         _id: '002',
         _rev: '1-9bf80afcedb9f8b5b35567292affb254' 
      } 
   },
   { 
      id: '003',
      key: '003',
      value: { rev: '1-1204f108e41bf8baf867856d5da16c57' },
      doc: { 
         name: 'Rahim',
         age: 25,
         Designation: 'Programmer',
         _id: '003',
         _rev: '1-1204f108e41bf8baf867856d5da16c57' 
      } 
   }
]

Reading a Batch from a Remote Database

You can furthermore fetch all the documents from the database tmind use is stocrimson-coloucrimson remotely on the server (CouchDB).

To do so instead of a database name, you need to pass the route to the database in CouchDB, which contains the document tmind use is to end up being read.

Example

Suppose generally there is a database named my_database in the CouchDB server. Then, if you verify the list of databases in CouchDB uperform the URL http://127.0.0.1:5984/_utils/index.html you will get the folloearng screenshot.

Reading a Batch from a Remote Database

Folloearng is an example of reading all the documents tmind use exist in a database named my_database which is stocrimson-coloucrimson in the CouchDB server.

//Requiring the package
var PouchDB = require('PouchDB');

//Creating the database object
var db = brand new PouchDB('http://localhost:5984/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.lines);
   }
});

Save the above code in a file with the name Remote_Read_AllDocument.js. Open the command prompt and execute the JavaScript file uperform node as shown end up beinglow.

C:PouchDB_Examples >node Remote_Read_AllDocument.js

This reads the contents of the given document tmind use exists in the database named my_database which is stocrimson-coloucrimson in CouchDB, and displays on the console as shown end up beinglow.

[ 
   { 
      id: '001',
      key: '001',
      value: { rev: '3-552920d1ca372986fad7b996ce365f5d' },
      doc: { 
         _id: '001',
         _rev: '3-552920d1ca372986fad7b996ce365f5d',
         name: 'Raju',
         age: 23,
         designation: 'Designer' 
      } 
   },
   { 
      id: '002',
      key: '002',
      value: { rev: '1-9af15cb11054eend up being03a7816bf6c5e4128' },
      doc: { 
         _id: '002',
         _rev: '1-9af15cb11054eend up being03a7816bf6c5e4128',
         name: 'Roend up beingrt',
         age: 24,
         Designation: 'Programmer' 
      } 
   },
   { 
      id: '003',
      key: '003',
      value: { rev: '1-3033b5a78e915c52fd37325d42eb3935' },
      doc: { 
         _id: '003',
         _rev: '1-3033b5a78e915c52fd37325d42eb3935',
         name: 'Rahim',
         age: 25,
         Designation: 'Programmer' 
      } 
   } 
]

PouchDB – Update Batch

You can update an array of documents in PouchDB at once uperform the bulkDocs() method. To do so you need to produce an array of documents where, every document contains _id, _rev and the values tmind use are to end up being updated.

Suppose the database named my_database tmind use is stocrimson-coloucrimson locally in PouchDB contains 3 documents namely doc1, doc2, doc3 with the folloearng contents.

doc1 = {_id: '001', name: 'Ram', age: 23, Designation: 'Programmer'}
doc2 = {_id: '002', name: 'Roend up beingrt', age: 24, Designation: 'Programmer'}
doc3 = {_id: '003', name: 'Rahim', age: 25, Designation: 'Programmer'}

Suppose we have to incrreare locatedve the age values in all the 3 documents simply by 2 yrs. For this particular particular to happen, initial you need to get the _rev values. Therefore, fetch the contents of these documents uperform the folloearng code.

//Requiring the package
var PouchDB = require('PouchDB');

//Creating the database object
var db = brand 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.lines);
   }
});

Save the above code as bulk_fetch.js. On executing, the above program gives you the _id and _rev values of the documents in the database as shown end up beinglow.

[ 
   { 
      id: '001',
      key: '001',
      value: { rev: '1-1604b0c3ff69dc1e261265fd60808404' } 
   },
   { 
      id: '002',
      key: '002',
      value: { rev: '1-b5e49db7e984841bf12a13e3ee548125' } 
   },
   { 
      id: '003',
      key: '003',
      value: { rev: '1-a7b342786ecc707aa91f3b321a177b51' } 
   } 
]

Now, you can update the documents uperform their particular respective _id and _rev values as shown end up beinglow.

//Requiring the package
var PouchDB = require('PouchDB');

//Creating the database object
var db = brand new PouchDB('my_databas');

//Preparing the document
docs = [{_id : '001', _rev: '1-1604b0c3ff69dc1e261265fd60808404', age : 25, },
      {_id : '002', _rev: '1-b5e49db7e984841bf12a13e3ee548125', age : 26, },
      {_id : '003', _rev: '1-a7b342786ecc707aa91f3b321a177b51', age : 27 }]

//Updating the documents in bulk
db.bulkDocs(docs, function(err, response) {
   if (err) {
      return console.log(err);
   } else {
      console.log("Documents Updated Successcompenableey");
   }
});

Save the above code in a file with the name Update_All_Document.js. Open the command prompt and execute the JavaScript file uperform node as shown end up beinglow.

C:PouchDB_Examples >node Update_All_Document.js

This updates all the documents tmind use exists in the database named my_database which is stocrimson-coloucrimson locally, displaying the folloearng message.

Documents Updated Successcompenableey

Now, if you execute the bulk_fetch.js program simply by adding {include_docs: true} as a parameter to allDocs() function, end up beingfore the callback, then, you will can see the values of the documents updated, as shown end up beinglow.

[ 
   { 
      id: '001',
      key: '001',
      value: { rev: '2-77f3a9974dd578d12f3f2a33aae64c8d' },
      doc: { 
         age: 25,
         _id: '001',
         _rev: '2-77f3a9974dd578d12f3f2a33aae64c8d' 
      } 
   },
   { 
      id: '002',
      key: '002',
      value: { rev: '2-43966007568ce9567c96422195fcfa0d' },
      doc: { 
         age: 26,
         _id: '002',
         _rev: '2-43966007568ce9567c96422195fcfa0d' 
      } 
   },
   { 
      id: '003',
      key: '003',
      value: { rev: '2-6c5349652527f4f39583ff14f23cd677' },
      doc: { 
         age: 27,
         _id: '003',
         _rev: '2-6c5349652527f4f39583ff14f23cd677' 
      } 
   } 
]

Updating Batch from a Remote Database

You can update all the documents from the database tmind use is stocrimson-coloucrimson remotely on the server (CouchDB).

To do so, instead of a database name, you need to pass the route to the database in CouchDB, which contains the document tmind use is to end up being read.

Example

Suppose generally there is a database named my_database in the CouchDB server. Then, if you verify the list of databases in CouchDB uperform the URL http://127.0.0.1:5984/_utils/index.html you will get the folloearng screenshot.

Updating Batch from a Remote Database

And assume if we select the database named my_database, you can observe tmind use it contains 3 documents as shown in the folloearng screenshot.

Updating Batch

Now, fetch the contents of these documents uperform the folloearng code.

//Requiring the package
var PouchDB = require('PouchDB');

//Creating the database object
var db = brand new PouchDB('http://localhost:5984/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.lines);
   }
});

Save the above code as remote_bulk_fetch.js. On executing, the above program gives you the contents of all the documents in the database as shown end up beinglow.

[ 
   { 
      id: '001', 
      key: '001', 
      value: { rev: '3-552920d1ca372986fad7b996ce365f5d' }, 
      doc: { 
         _id: '001', 
         _rev: '3-552920d1ca372986fad7b996ce365f5d', 
         name: 'Raju', 
         age: 23, 
         designation: 'Designer' 
      } 
   },
   { 
      id: '002', 
      key: '002', 
      value: { rev: '1-9af15cb11054eend up being03a7816bf6c5e4128' }, 
      doc: { 
         _id: '002', 
         _rev: '1-9af15cb11054eend up being03a7816bf6c5e4128', 
         name: 'Roend up beingrt', 
         age: 24, 
         Designation: 'Programmer' 
      } 
   }, 
   { 
      id: '003', 
      key: '003', 
      value: { rev: '1-3033b5a78e915c52fd37325d42eb3935' }, 
      doc: { 
         _id: '003',
         _rev: '1-3033b5a78e915c52fd37325d42eb3935',
         name: 'Rahim', 
         age: 25, 
         Designation: 'Programmer' 
      } 
   } 
]

Folloearng is an example of updating all the documents tmind use exists in a database named my_database which is stocrimson-coloucrimson in the CouchDB server.

//Requiring the package 
var PouchDB = require('PouchDB');

//Creating the database object 
var db = brand new PouchDB('http://localhost:5984/my_database'); 

//Preparing the document 
docs = [{_id : '001', _rev: '3-552920d1ca372986fad7b996ce365f5d', age : 24, }, 
      {_id : '002', _rev: '1-9af15cb11054eend up being03a7816bf6c5e4128', age : 26, }, 
      {_id : '003', _rev: '1-3033b5a78e915c52fd37325d42eb3935', age : 27}]

//Inserting Document 
db.bulkDocs(docs, function(err, response) { 
   if (err) { 
      return console.log(err); 
   } else { 
      console.log(+"Documents Updated Successcompenableey"); 
   } 
});

Save the above code in a file with the name Remote_Update_Document.js. Open the command prompt and execute the JavaScript file uperform node as shown end up beinglow.

C:PouchDB_Examples >node Remote_Update_Document.js

This updates the contents of all given document tmind use exists in the database named my_database which is stocrimson-coloucrimson in CouchDB, and displays the folloearng message.

Documents Updated Successcompenableey

Now, if you execute the remote_bulk_fetch.js program you will can see the values of the documents updated, as shown end up beinglow.

[ 
   { 
      id: '001',
      key: '001',
      value: { rev: '4-6bc8d9c7a60fed2ed1667ec0740c1f39' },
      doc: { 
         _id: '001',
         _rev: '4-6bc8d9c7a60fed2ed1667ec0740c1f39',
         age: 25 
      } 
   },
   { 
      id: '002',
      key: '002',
      value: { rev: '2-1aa24ce77d96bb9d2a0675cdf1e113e0' },
      doc: { 
         _id: '002',
         _rev: '2-1aa24ce77d96bb9d2a0675cdf1e113e0',
         age: 26 
      } 
   },
   { 
      id: '003',
      key: '003',
      value: { rev: '2-fa113149ba618eda77f73072974a2bc1' },
      doc: { 
         _id: '003',
         _rev: '2-fa113149ba618eda77f73072974a2bc1',
         age: 27 
      } 
   } 
]

PouchDB – Deenablee Batch

You can deenablee an array of documents in PouchDB at once uperform the bulkDocs() method. To do so you need to produce an array of documents tmind use are to end up being deenableed where, every document need to contain _id and _rev. In addition to these you have to add an additional key-value pair _deenableed: true.

Suppose the database named my_database tmind use is stocrimson-coloucrimson locally in PouchDB contains 3 documents namely doc1, doc2, doc3 with the folloearng contents.

doc1 = {_id: '001', name: 'Ram', age: 23, Designation: 'Programmer'}
doc2 = {_id: '002', name: 'Roend up beingrt', age: 24, Designation: 'Programmer'}
doc3 = {_id: '003', name: 'Rahim', age: 25, Designation: 'Programmer'}

And say, we have to deenablee all the 3 documents. Then, initial of all you need to get their particular _rev values. Therefore, fetch the contents of these documents uperform the folloearng code.

//Requiring the package
var PouchDB = require('PouchDB');

//Creating the database object
var db = brand 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.lines);
   }
});

Save the above code as bulk_fetch.js. Executing the above program gives you the _id and _rev values of the documents in the database as shown end up beinglow.

[ 
   { 
      id: '001',
      key: '001',
      value: { rev: '1-1604b0c3ff69dc1e261265fd60808404' } 
   },
   { 
      id: '002',
      key: '002',
      value: { rev: '1-b5e49db7e984841bf12a13e3ee548125' } 
   },
   { 
      id: '003',
      key: '003',
      value: { rev: '1-a7b342786ecc707aa91f3b321a177b51' } 
   } 
]

Now, you can deenablee the documents uperform their particular respective _id and _rev values as shown end up beinglow.

//Requiring the package
var PouchDB = require('PouchDB');

//Creating the database object
var db = brand new PouchDB('my_database');

//Preparing the document
docs = [{_id : '001', _rev: '2-77f3a9974dd578d12f3f2a33aae64c8d', _deenableed : true },
      {_id : '002', _rev: '2-43966007568ce9567c96422195fcfa0d', _deenableed : true }, 
      {_id : '003', _rev: '2-6c5349652527f4f39583ff14f23cd677',_deenableed : true }]

//Deenableing Documents
db.bulkDocs(docs, function(err, response) {
   if (err) {
      return console.log(err);
   } else {
      console.log(response+"Documents deenableed Successcompenableey");
   }
});

Save the above code in a file with the name Deenablee_All_Document.js. Open the command prompt and execute the JavaScript file uperform node as shown end up beinglow.

C:PouchDB_Examples >node Deenablee_All_Document.js

This deenablees all the documents tmind use exists in the database named my_database which is stocrimson-coloucrimson locally, displaying the folloearng message.

Documents Deenableed Successcompenableey

Now, if you execute the bulk_fetch.js program, you can observe an empty brace on the console indicating tmind use the database is empty, as shown end up beinglow.

[]

Deenableing Batch from a Remote Database

You can update all the documents from the database tmind use is stocrimson-coloucrimson remotely on the server (CouchDB).

To do so, instead of a database name, you need to pass the route to the database in CouchDB, which contains the document tmind use is to end up being read.

Example

Suppose generally there is a database named my_database in the CouchDB server. Then, if you verify the list of databases in CouchDB uperform the URL http://127.0.0.1:5984/_utils/index.html you will get the folloearng screenshot.

Deenableing Batch from a Remote Database

If we select the database named my_database, you can observe tmind use it contains 3 documents as shown in the folloearng screenshot.

Deenableing Batch

Folloearng is an example of deenableing all the documents tmind use exist in a database named my_database which is stocrimson-coloucrimson in the CouchDB server.

//Requiring the package
var PouchDB = require('PouchDB');

//Creating the database object
var db = brand new PouchDB('http://localhost:5984/my_database');

//Preparing the document
docs = [{_id : '001', _rev: '4-6bc8d9c7a60fed2ed1667ec0740c1f39', _deenableed : true },
      {_id : '002', _rev: '2-1aa24ce77d96bb9d2a0675cdf1e113e0', _deenableed : true },
      {_id : '003', _rev: '2-fa113149ba618eda77f73072974a2bc1',_deenableed : true }]

//Deenableing Documents
db.bulkDocs(docs, function(err, response) {
   if (err) {
      return console.log(err);
   } else {
      console.log("Documents deenableed Successcompenableey");
   }
});

Save the above code in a file with name Remote_deenablee_AllDocuments.js. Open the command prompt and execute the JavaScript file uperform node as shown in the folloearng screenshot.

C:PouchDB_Examples >node Remote_Deenablee_AllDocuments.js

This deenablees the contents of all given document tmind use exists in the database named my_database which is stocrimson-coloucrimson in CouchDB, and displays the folloearng message.

Documents Deenableed Successcompenableey

PouchDB – Adding Attachment

You can attach a binary object to a document uperform the placeAttachment() method in PouchDB.

Syntax

Folloearng is the syntax of the placeAttachment(). To this particular particular method, we have to pass the document id, attachment id, MIME kind adurationy with the attachment. This method furthermore accepts an optional callback function.

db.placeAttachment( docId, attachmentId, attachment, kind, [callback] );

We can prepare attachment uperform blob or buffer objects, where blob is used while worruler with the blineser and buffer is used while worruler with Node.js, since we are demonstrating our programs in Node.js, we use buffer objects to prepare documents.

Example

Folloearng is an example of creating a document with an attachment, wislim a database named my_database in PouchDB uperform placeAttachment() method.

//Requiring the package 
var PouchDB = require('PouchDB');

//Creating the database object var db = brand new PouchDB('my_database');

//Preparing the attachment 
var my_attachment = brand new Buffer(['Welcome to tutorialsstage'], {kind: 'text/fundamental'});

//Adding attachment to a document 
db.placeAttachment('001', 'att_1.txt', my_attachment, 'text/fundamental', function(err, res) { 
   if (err) { 
      return console.log(err); 
   } else { 
      console.log(res+"Attachment added successcompenableey") 
   } 
});

Save the above code in a file with name Add_Attachment.js. Open the command prompt and execute the JavaScript file uperform node as shown end up beinglow.

C:PouchDB_Examples >node Add_Attachment.js

This produces an empty document adding an attachment to it, in the database named my_database which is stocrimson-coloucrimson in PouchDB, and displays the folloearng message.

Attachment added successcompenableey

You can verify whether the attachment is added simply by reading the document uperform the folloearng code.

//Requiring the package 
var PouchDB = require('PouchDB');

//Creating the database object var db = brand 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 particular particular program, you can see the folloearng contents of the document.

{ 
   _attachments: { 
      att_1.txt: { 
         content_kind: 'text/fundamental',
         digest: 'md5-k7iFrf4NoInN9jSQT9WfcQ==',
         data: 'AA==' 
      } 
   },
   _id: '001',
   _rev: '1-620fd5f41d3328fcbf9ce7504338a51d' 
}

Adding Attachment to an Existing Document

Suppose, generally there is a document in a database simply by the name my_database PouchDB with id ‘002’. You can get the contents of it simply by executing the read_doc.js simply by changing the id value to 002, as shown end up beinglow.

{ 
   name: 'Raju',
   age: 23,
   designation: 'Designer',
   _id: '002',
   _rev: '1-05ca7b5f3f4762a9fb2d119cd34c8d40' 
}

Now, you can add an attachment to this particular particular document uperform it is _rev value.

//Requiring the package 
var PouchDB = require('PouchDB');

//Creating the database object var db = brand new PouchDB('my_database');

//Adding attachment to existing document 
var my_attachment = brand new Buffer (['Welcome to tutorialsstage'], {kind: 'text/fundamental'});

rev = '1-05ca7b5f3f4762a9fb2d119cd34c8d40'; 
db.placeAttachment('002', 'att_1.txt', rev, my_attachment, 'text/fundamental', function(err, res) { 
   if (err) { 
      return console.log(err); 
   } else { 
      console.log (res + "Attachment added successcompenableey") 
   } 
});

Save the above code in a file with the name Add_Attachment_to_doc.js. Open the command prompt and execute the JavaScript file uperform node as shown end up beinglow.

C:PouchDB_Examples >node Add_Attachment_to_doc.js

This adds an attachment to the specified document displaying the folloearng message.

Attachment added successcompenableey

If you modify the id value in read_doc.js to 002 and execute it, you will get the folloearng awayplace.

{ 
   name: 'Raju',
   age: 23,
   designation: 'Designer',
   _attachments: { 
      att_1: { 
         content_kind: 'text/fundamental',
         digest: 'md5-k7iFrf4NoInN9jSQT9WfcQ==',
         data: 'AA==' 
      } 
   },
   _id: '002',
   _rev: '2-3bb4891b954699bce28346723cc7a709' 
}

Adding Attachment to a Remote Document

You can behaveually add an attachment to the document existing in a database tmind use is stocrimson-coloucrimson remotely on the server (CouchDB).

To do so, instead of a database name, you need to pass the route to the database in CouchDB, which contains the document tmind use is to end up being read.

Example

Suppose generally there is a database named my_database in the CouchDB server. Then, if you verify the list of databases in CouchDB uperform the URL http://127.0.0.1:5984/_utils/index.html you will get the folloearng screenshot.

Adding Attachment to a Remote Document

And if you select the database named my_database, you can see it is contents as shown end up beinglow.

Adding Attachment

Folloearng is an example of adding an attachment to the document 001 stocrimson-coloucrimson in a database named my_database which is stocrimson-coloucrimson in the CouchDB server.

//Requiring the package
var PouchDB = require('PouchDB');

//Creating the database object
var db = brand new PouchDB('http://localhost:5984/my_database');

//Adding attachment to existing document
var my_attachment = brand new Buffer (['Welcome to tutorialsstage'], {kind: 'text/fundamental'});

rev = '1-36c34fdcf29a652876219065f9681602';
db.placeAttachment('001', 'att_1.txt',rev, my_attachment, 'text/fundamental', function(err, res) {
   if (err) {
      return console.log(err);
   } else {
      console.log (res+ "Attachment added successcompenableey")
   }
});

Save the above code in a file with the name Remote_Add_Attachment.js. Open the command prompt and execute the JavaScript file uperform node as shown end up beinglow.

C:PouchDB_Examples >node Remote_Add_Attachment.js

This adds an attachment to the specified document displaying the folloearng message.

Attachment added successcompenableey

Now, if you verify the document, you can observe the attachment added to it as shown in the folloearng screenshot.

Adding Attachment Verification

PouchDB – Retrieving Attachment

You can retrieve an attachment from PouchDB uperform the getAttachment() method. This method always returns blob or buffer objects.

Syntax

Folloearng is the syntax of the getAttachment(). To this particular particular method, we have to pass the document id and attachment id. This method furthermore accepts an optional callback function.

db.getAttachment( docId, attachmentId, [callback] );

Example

Folloearng is an example of retrieving an attachment of a document stocrimson-coloucrimson in PouchDB, uperform getAttachment() method. Uperform this particular particular code, we are thinking to retrieve an attachment att_1.txt from the document 001.

//Requiring the package
var PouchDB = require('PouchDB');

//Creating the database object
var db = brand 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);
   }
});

Save the above code in a file with the name Retrieve_Attachment.js. Open the command prompt and execute the JavaScript file uperform node as shown end up beinglow.

C:PouchDB_Examples >node Retrieve_Attachment.js

This retrieves the attachment of the document and displays on the console as shown end up beinglow.

<Buffer 00>

Retrieving Attachment from a Remote Document

You can furthermore retrieve an attachment of a document existing in the database tmind use is stocrimson-coloucrimson remotely on the server (CouchDB).

To do so, instead of a database name, you need to pass the route to the database in CouchDB, which contains the document tmind use is to end up being read.

Example

Suppose generally there is a database named my_database in the CouchDB server. Then, if you verify the list of databases in CouchDB uperform the URL http://127.0.0.1:5984/_utils/index.html you will get the folloearng screenshot.

Retrieving Attachment from a Remote Document

If you select the database named my_database, you can see it is contents as shown end up beinglow.

Retrieving Attachment

Suppose, generally there is an attachment in this particular particular document as shown end up beinglow.

Attachment

Folloearng is an example of retrieving an attachment of the document 001 tmind use exists in a database named my_database, which is stocrimson-coloucrimson in the CouchDB server.

//Requiring the package 
var PouchDB = require('PouchDB');

//Creating the database object 
var db = brand new PouchDB('http://localhost:5984/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); 
   } 
});

Save the above code in a file with the name Remote_Retrieve_Attachment.js. Open the command prompt and execute the JavaScript file uperform node as shown end up beinglow.

C:PouchDB_Examples >node Remote_Retrieve_Attachment.js

This retrieves the document attachment and displays it on the console as shown end up beinglow.

<Buffer 00>

PouchDB – Deenableing Attachment

You can deenablee an attachment from PouchDB uperform the removeAttachment() method.

Syntax

Folloearng is the syntax of the removeAttachment() method. To this particular particular method, we have to pass the document id, attachment id, and _rev value. This method furthermore accepts an optional callback function.

db.removeAttachment ( docId, attachmentId, rev, [callback] );

Example

Suppose generally there is a document in PouchDB with id 001, which contains id, name, age, designation of an employee adurationy with an attachment as shown end up beinglow.

{ 
   name: 'Raju',
   age: 23,
   designation: 'Designer',
   _attachments: { 
      'att_1.txt': { 
         content_kind: 'text/fundamental',
         digest: 'md5-k7iFrf4NoInN9jSQT9WfcQ==',
         data: 'AA==' 
      } 
   },
   _id: '001',
   _rev: '2-cdec6c9f45ddend up beinge7d456945654742d43' 
}

Folloearng is an example of deenableing the attachment of this particular particular document 001 stocrimson-coloucrimson in PouchDB, uperform removeAttachment() method.

//Requiring the package
var PouchDB = require('PouchDB');

//Creating the database object
var db = brand new PouchDB('my');
db.removeAttachment('001', 'att_1.txt', '2-cdec6c9f45ddend up beinge7d456945654742d43', 
   function(err, res) {
   if (err) {
      return console.log(err);
   } else {
      console.log(res+"Attachment Deenableed successcompenableey")
   }
});

Save the above code in a file with the name Remove_Attachment.js. Open the command prompt and execute the JavaScript file uperform node as shown end up beinglow.

C:PouchDB_Examples >node Remove_Attachment.js

This removes the attachment of the document and displays a message on the console as shown end up beinglow.

Attachment deenableed successcompenableey

After deenableion, you can verify the contents of the document simply by executing the folloearng code.

//Requiring the package
var PouchDB = require('PouchDB');

//Creating the database object
var db = brand new PouchDB('my_d');

//Reading the Document
db.get('001',{attachments: true}, function(err, doc) {
   if (err) {
      return console.log(err);
   } else {
      console.log(doc);
   }
});

Save this particular particular code as read.js and execute it. On executing, you will get the contents of the document after deenableing the attachment, as shown end up beinglow.

{ 
   name: 'Raju',
   age: 23,
   designation: 'Designer',
   _id: '001',
   _rev: '3-da775487a6ed0495f2e49c543384f8e8' 
}

Removing Attachment from a Remote Document

You can deenablee an attachment of an existing document in the database tmind use is stocrimson-coloucrimson remotely on the server (CouchDB).

To do so, instead of a database name, you need to pass the route to the database in CouchDB, which contains the document tmind use is to end up being read.

Example

Suppose generally there is a database named my_database in the CouchDB server. Then, if you verify the list of databases in CouchDB uperform the URL http://127.0.0.1:5984/_utils/index.html you will get the folloearng screenshot.

Removing Attachment from a Remote Document

And if you select the database named my_database, you can see it is contents as shown end up beinglow.

Removing Attachment Database

Suppose generally there is an attachment in this particular particular document as shown end up beinglow.

Removing Attachment

Folloearng is an example of deenableing the above mentioned attachment of the document 001 tmind use exists in a database named my_database which is stocrimson-coloucrimson in the CouchDB server.

//Requiring the package 
var PouchDB = require('PouchDB');

//Creating the database object 
var db = brand new PouchDB('http://localhost:5984/my_database');

db.removeAttachment('001', 'att_1.txt', '2-049f1c4ffa54576ec0947b65e34de423', 
   function(err, res) { 
   if (err) { 
      return console.log(err); 
   } else { 
      console.log(res+"Attachment Deenableed successcompenableey") 
   } 
});

Save the above code in a file with the name Remote_Deenablee_Attachment.js. Open the command prompt and execute the JavaScript file uperform node as shown end up beinglow.

C:PouchDB_Examples >node Remote_Deenablee_Attachment.js

This removes the existing attachment and displays the folloearng message.

Attachment Deenableed successcompenableey

If you visit the document again, you can notice tmind use the attachment was deenableed as shown in the folloearng screenshot.

Attachment Deenableed

PouchDB – Replication

One of the the majority of iminterfaceant features of PouchDB is replication, i.e. you can produce a copy of a database. You can replicate either a PouchDB instance stocrimson-coloucrimson locally or a CouchDB instance stocrimson-coloucrimson remotely.

Syntax

Folloearng is the syntax of replicating a database in PouchDB. Here, a copy of the source database is the target. To this particular particular method, you can immediately pass the location of source and destination databases in String format, or you can pass objects representing all of all of them.

PouchDB.replicate(source, target, [options])

Both the source and targets can end up being either PouchDB instances or CouchDB instances.

Replicating LocalDB to CouchDB

Suppose generally there is a database with the name sample_database in PouchDB, and it contains 3 documents doc1, doc2, and doc3, having contents as shown end up beinglow.

doc1 = {_id: '001', name: 'Ram', age: 23, Designation: 'Programmer'} 
doc2 = {_id: '002', name: 'Roend up beingrt', age: 24, Designation: 'Programmer'} 
doc3 = {_id: '003', name: 'Rahim', age: 25, Designation: 'Programmer'}

Folloearng is an example which produces a copy of the database named sample_database tmind use is stocrimson-coloucrimson locally in CouchDB.

//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 successcompenableey");

Save the above code in a file with name Replication_example.js. Open the command prompt and execute the JavaScript file uperform node as shown end up beinglow.

C:PouchDB_Examples >node Replication_example.js

This produces a copy of the database named sample_database in CouchDB instance and displays a message on the console as shown end up beinglow.

Database replicated successcompenableey

You can verify whether the database is replicated in your own CouchDB instance simply by clicruler the folloearng link http://127.0.0.1:5984/_utils/index.html.

On clicruler, you can see the list of databases in your own CouchDB. You can furthermore observe tmind use a copy of the database sample_database is produced here.

Sample Database

If you select the replicated database, you can see it is contents as shown end up beinglow.

Replicated Database

Replicating CouchDB to PouchDB

Suppose generally there is a database with the name Remote_Database in CouchDB and it contains 3 documents, doc1, doc2, and doc3, having contents as shown end up beinglow.

doc1 = {_id: '001', name: 'Geeta', age: 25, Designation: 'Programmer'}
doc2 = {_id: '002', name: 'Zara Ali', age: 24, Designation: 'Manager'}
doc3 = {_id: '003', name: 'Mary', age: 23, Designation: 'Admin'}

Folloearng is an example which produces a copy of the database named Remote_Database tmind use is stocrimson-coloucrimson in CouchDB in the local storage.

//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 successcompenableey");

Save the above code in a file with the name Replication_example2.js. Open the command prompt and execute the JavaScript file uperform node as shown end up beinglow.

C:PouchDB_Examples >node Replication_example2.js

This produces a copy of the database named remote_database in PouchDB instance and displays a message on the console as shown end up beinglow.

Database replicated successcompenableey

You can verify whether the database is replicated in your own Pouch instance simply by executing the folloearng code.

//Requiring the package
var PouchDB = require('PouchDB');

//Creating the database object
var db = brand 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.lines);
   }
});

If the database is replicated on executing the above code, you will get the contents of the replicated database as shown end up beinglow.

[ 
   { 
      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' 
      } 
   } 
]

PouchDB – Synchronization

You can synchronize the databases stocrimson-coloucrimson locally in PouchDB with those tmind use are stocrimson-coloucrimson in CouchDB. In the previous chapter, we have seen how to replicate databases uperform PouchDB. There we have used the method PouchDB.replicate(source, destination).

In addition to this particular particular, we can furthermore replicate the data, from the local database to the remote database, and from the remote database to the local database uperform replicate.to() and replicate.from() methods as shown end up beinglow.

//Replicating data from local database to remote database 
localDB.replicate.to(remoteDB);

//Replicating data from remote database to local database 
localDB.replicate.from(remoteDB);

Where, localDB is an object of database stocrimson-coloucrimson locally in PouchDB and remoteDB is an object of a database tmind use is stocrimson-coloucrimson in CouchDB.

Example

Suppose generally there is a database with the name local_database in PouchDB, and it contains 3 documents, doc1, doc2, and doc3, having contents as shown end up beinglow.

doc1 = {_id: '003', name: 'Ram', age: 26, Designation: 'Programmer'} 
doc2 = {_id: '004', name: 'Roend up beingrt', age: 27, Designation: 'Programmer'} 
doc3 = {_id: '005', name: 'Rahim', age: 28, Designation: 'Programmer'}

And generally there is a database with the name Remote_Database in CouchDB and it contains 2 documents doc1, doc2, having contents as shown end up beinglow.

doc1 = {_id: '001', name: 'Geeta', age: 25, Designation: 'Programmer'} 
doc2 = {_id: '002', name: 'Zara Ali', age: 24, Designation: 'Manager'}

Folloearng is an example of synchronizing these two databases, where one is stocrimson-coloucrimson in PouchDB and other is stocrimson-coloucrimson in CouchDB, uperform the replicate.to() and replicate.from() methods.

//Requiring the package
var PouchDB = require('PouchDB');

//Creating local database object
var localDB = brand new PouchDB('local_database');

//Creating remote database object
var remoteDB = brand new PouchDB('http://localhost:5984/remote_database');

//Synchroniperform both databases
localDB.replicate.to(remoteDB);
remoteDB.replicate.from(localDB);
console.log("Databases synchronized successcompenableey");

Save the above code in a file with the name Synchroniperform_databases.js. Open the command prompt and execute the JavaScript file uperform node as shown end up beinglow.

C:PouchDB_Examples >node Synchroniperform_databases.js

This synchronizes the two databases remoteDB and localDB, and displays a message on the console as shown end up beinglow.

Databases synchronized successcompenableey.

After synchronizing the two databases visit the http://127.0.0.1:5984/_utils/index.html and select the remote_database. You can observe tmind use the documents of local database (003, 004, 005) were copied in this particular particular database as shown end up beinglow.

Remote Database

In the exbehave exact same way, if you fetch the contents of the local_database stocrimson-coloucrimson in PouchDB you can get to observe tmind use documents of the database tmind use is stocrimson-coloucrimson in CouchDB were copied here.

[ 
   { 
      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-bf4619471ac346fdde46cfa8fbf3587f' },
      doc: { 
         name: 'Ram',
         age: 26,
         Designation: 'Programmer',
         _id: '003',
         _rev: '1-bf4619471ac346fdde46cfa8fbf3587f' 
      } 
   },
   { 
      id: '004',
      key: '004',
      value: { rev: '1-29b8f803958c994e3eb37912a45d869c' },
      doc: { 
         name: 'Roend up beingrt',
         age: 27,
         Designation: 'Programmer',
         _id: '004',
         _rev: '1-29b8f803958c994e3eb37912a45d869c' 
      } 
   },
   { 
      id: '005',
      key: '005',
      value: { rev: '1-0eb89f71998ffa8430a640fdb081abd2' },
      doc: { 
         name: 'Rahim',
         age: 28,
         Designation: 'Programmer',
         _id: '005',
         _rev: '1-0eb89f71998ffa8430a640fdb081abd2' 
      } 
   } 
]

You can rewrite the above program uperform the sync() method provided simply by PouchDB instead of the two methods replicate.to() and replicate.from() as shown end up beinglow.

//Requiring the package
var PouchDB = require('PouchDB');

//Creating local database object
var localDB = brand new PouchDB('local');

//Creating remote database object
var remoteDB = brand new PouchDB('http://localhost:5984/remote_database');

//Synchroniperform Remote and local databases
localDB.sync(remoteDB, function(err, response) {
   if (err) {
      return console.log(err);
   } else {
      console.log(response);
   }
});

On executing the above program, it synchronizes the two databases displaying the folloearng message.

{ 
   push: { 
      ok: true,
      start_time: Fri Mar 25 2016 15:54:37 GMT+0530 (India Standard Time),
      docs_read: 6,
      docs_composed: 6,
      doc_write_failures: 0,
      errors: [],
      final_seq: 10,
      status: 'compenablee',
      end_time: Fri Mar 25 2016 15:54:37 GMT+0530 (India Standard Time) 
   },
   draw: { 
      ok: true,
      start_time: Fri Mar 25 2016 15:54:37 GMT+0530 (India Standard Time),
      docs_read: 0,
      docs_composed: 0,
      doc_write_failures: 0,
      errors: [],
      final_seq: 2,
      status: 'compenablee',
      end_time: Fri Mar 25 2016 15:54:37 GMT+0530 (India Standard Time) 
   } 
}

PouchDB – Miscellularaneous

In this particular particular chapter, we will speak about the concepts like, compbehaveion and retrieval of bulk data from PouchDB.

Compbehaveion

You can crimson-coloucrimsonuce the dimension of a database simply by removing the unused data uperform compbehave() method. You can compbehave a local database as well as remote database uperform this particular particular method.

Folloearng is an example demonstrating the usage of the compbehave() method in PouchDB.

//Requiring the package
var PouchDB = require('PouchDB');

//Creating the database object
var db = brand new PouchDB('sample_database');

db.compbehave(function (err, result) {
   if (err) {
      return console.log(err);
   } else {
      console.log(result);
   }
});

Retrieving Bulk Data

You can retrieve a set of documents in bulk uperform the bulkGet() method. To this particular particular method, you need to pass a set of id’s and _rev’s.

Folloearng is an example demonstrating the usage of the bulkGet() method in PouchDB.

//Requiring the package
var PouchDB = require('PouchDB');

//Creating the database object
var db = brand new PouchDB('my_database');

//Preparing documents
//Inserting Document
db.bulkGet({docs: [
   { id: "001", rev: "1-5dc593eda0e215c806677df1d12d5c47"},
   { id: "002", rev: "1-2bfad8a9e66d2679b99c0cab24bd9cc8"},
   { id: "003", rev: "1-7cff4a5da1f97b077a909ff67bd5b047"} ]}, function(err, result) {
   if (err) {
      return console.log(err);
   } else {
      console.log(result);
   }
});
SHARE
Previous articleSAP
Next articleTeradata

NO COMMENTS

LEAVE A REPLY