Author: alien

  • Khóa học miễn phí PouchDB – Adding Attachment nhận dự án làm có lương

    PouchDB – Adding Attachment



    You can attach a binary object to a document using the putAttachment() method in PouchDB.

    Syntax

    Following is the syntax of the putAttachment(). To this method, we have to pass the document id, attachment id, MIME type along with the attachment. This method also accepts an optional callback function.

    db.putAttachment( docId, attachmentId, attachment, type, [callback] );
    

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

    Example

    Following is an example of creating a document with an attachment, within a database named my_database in PouchDB using putAttachment() method.

    //Requiring the package
    var PouchDB = require(''PouchDB'');
    
    //Creating the database object var db = new PouchDB(''my_database'');
    
    //Preparing the attachment
    var my_attachment = new Buffer([''Welcome to tutorialspoint''], {type: ''text/plain''});
    
    //Adding attachment to a document
    db.putAttachment(''001'', ''att_1.txt'', my_attachment, ''text/plain'', function(err, res) {
       if (err) {
          return console.log(err);
       } else {
          console.log(res+"Attachment added successfully")
       }
    });
    

    Save the above code in a file with name Add_Attachment.js. Open the command prompt and execute the JavaScript file using node as shown below.

    C:PouchDB_Examples >node Add_Attachment.js
    

    This creates an empty document adding an attachment to it, in the database named my_database which is stored in PouchDB, and displays the following message.

    Attachment added successfully
    

    You can verify whether the attachment is added by reading the document using the following code.

    //Requiring the package
    var PouchDB = require(''PouchDB'');
    
    //Creating the database object var db = new PouchDB(''my_database'');
    
    //Reading the Document
    db.get(''001'',{attachments: true}, function(err, doc) {
       if (err) {
          return console.log(err);
       } else {
          console.log(doc);
       }
    });
    

    Save the above code as read_doc.js and execute it. Executing this program, you can see the following contents of the document.

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

    Adding Attachment to an Existing Document

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

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

    Now, you can add an attachment to this document using its _rev value.

    //Requiring the package
    var PouchDB = require(''PouchDB'');
    
    //Creating the database object var db = new PouchDB(''my_database'');
    
    //Adding attachment to existing document
    var my_attachment = new Buffer ([''Welcome to tutorialspoint''], {type: ''text/plain''});
    
    rev = ''1-05ca7b5f3f4762a9fb2d119cd34c8d40
    db.putAttachment(''002'', ''att_1.txt'', rev, my_attachment, ''text/plain'', function(err, res) {
       if (err) {
          return console.log(err);
       } else {
          console.log (res + "Attachment added successfully")
       }
    });
    

    Save the above code in a file with the name Add_Attachment_to_doc.js. Open the command prompt and execute the JavaScript file using node as shown below.

    C:PouchDB_Examples >node Add_Attachment_to_doc.js
    

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

    Attachment added successfully
    

    If you change the id value in read_doc.js to 002 and execute it, you will get the following output.

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

    Adding Attachment to a Remote Document

    You can even add an attachment to the document existing in a database that is stored remotely on the server (CouchDB).

    To do so, instead of a database name, you need to pass the path to the database in CouchDB, which contains the document that is to be read.

    Example

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

    Adding Attachment to a Remote Document

    And if you select the database named my_database, you can view its contents as shown below.

    Adding Attachment

    Following is an example of adding an attachment to the document 001 stored in a database named my_database which is stored in the CouchDB server.

    //Requiring the package
    var PouchDB = require(''PouchDB'');
    
    //Creating the database object
    var db = new PouchDB(''http://localhost:5984/my_database'');
    
    //Adding attachment to existing document
    var my_attachment = new Buffer ([''Welcome to tutorialspoint''], {type: ''text/plain''});
    
    rev = ''1-36c34fdcf29a652876219065f9681602
    db.putAttachment(''001'', ''att_1.txt'',rev, my_attachment, ''text/plain'', function(err, res) {
       if (err) {
          return console.log(err);
       } else {
          console.log (res+ "Attachment added successfully")
       }
    });
    

    Save the above code in a file with the name Remote_Add_Attachment.js. Open the command prompt and execute the JavaScript file using node as shown below.

    C:PouchDB_Examples >node Remote_Add_Attachment.js
    

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

    Attachment added successfully
    

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

    Adding Attachment Verification

    Khóa học lập trình tại Toidayhoc vừa học vừa làm dự án vừa nhận lương: Khóa học lập trình nhận lương tại trung tâm Toidayhoc

  • Khóa học miễn phí PouchDB – Replication nhận dự án làm có lương

    PouchDB – Replication



    One of the most important features of PouchDB is replication, i.e. you can make a copy of a database. You can replicate either a PouchDB instance stored locally or a CouchDB instance stored remotely.

    Syntax

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

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

    Both the source and targets can be either PouchDB instances or CouchDB instances.

    Replicating LocalDB to CouchDB

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

    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''}
    

    Following is an example which makes a copy of the database named sample_database that is stored 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 successfully");
    

    Save the above code in a file with name Replication_example.js. Open the command prompt and execute the JavaScript file using node as shown below.

    C:PouchDB_Examples >node Replication_example.js
    

    This makes a copy of the database named sample_database in CouchDB instance and displays a message on the console as shown below.

    Database replicated successfully
    

    You can verify whether the database is replicated in your CouchDB instance by clicking the following link http://127.0.0.1:5984/_utils/index.html.

    On clicking, you can see the list of databases in your CouchDB. You can also observe that a copy of the database sample_database is created here.

    Sample Database

    If you select the replicated database, you can view its contents as shown below.

    Replicated Database

    Replicating CouchDB to PouchDB

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

    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''}
    

    Following is an example which makes a copy of the database named Remote_Database that is stored 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 successfully");
    

    Save the above code in a file with the name Replication_example2.js. Open the command prompt and execute the JavaScript file using node as shown below.

    C:PouchDB_Examples >node Replication_example2.js
    

    This makes a copy of the database named remote_database in PouchDB instance and displays a message on the console as shown below.

    Database replicated successfully
    

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

    //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);
       }
    });
    

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

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

    Khóa học lập trình tại Toidayhoc vừa học vừa làm dự án vừa nhận lương: Khóa học lập trình nhận lương tại trung tâm Toidayhoc

  • Khóa học miễn phí PouchDB – Delete Batch nhận dự án làm có lương

    PouchDB – Delete Batch



    You can delete an array of documents in PouchDB at once using the bulkDocs() method. To do so you need to create an array of documents that are to be deleted where, each document should contain _id and _rev. In addition to these you have to add another key-value pair _deleted: true.

    Suppose the database named my_database that is stored locally in PouchDB contains 3 documents namely doc1, doc2, doc3 with the following contents.

    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''}
    

    And say, we have to delete all the three documents. Then, first of all you need to get their _rev values. Therefore, fetch the contents of these documents using the following code.

    //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);
       }
    });
    

    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 below.

    [
       {
          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 delete the documents using their respective _id and _rev values as shown below.

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

    Save the above code in a file with the name Delete_All_Document.js. Open the command prompt and execute the JavaScript file using node as shown below.

    C:PouchDB_Examples >node Delete_All_Document.js
    

    This deletes all the documents that exists in the database named my_database which is stored locally, displaying the following message.

    Documents Deleted Successfully
    

    Now, if you execute the bulk_fetch.js program, you can observe an empty brace on the console indicating that the database is empty, as shown below.

    []
    

    Deleting Batch from a Remote Database

    You can update all the documents from the database that is stored remotely on the server (CouchDB).

    To do so, instead of a database name, you need to pass the path to the database in CouchDB, which contains the document that is to be read.

    Example

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

    Deleting Batch from a Remote Database

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

    Deleting Batch

    Following is an example of deleting all the documents that exist in a database named my_database which is stored in the CouchDB server.

    //Requiring the package
    var PouchDB = require(''PouchDB'');
    
    //Creating the database object
    var db = new PouchDB(''http://localhost:5984/my_database'');
    
    //Preparing the document
    docs = [{_id : ''001'', _rev: ''4-6bc8d9c7a60fed2ed1667ec0740c1f39'', _deleted : true },
          {_id : ''002'', _rev: ''2-1aa24ce77d96bb9d2a0675cdf1e113e0'', _deleted : true },
          {_id : ''003'', _rev: ''2-fa113149ba618eda77f73072974a2bc1'',_deleted : true }]
    
    //Deleting Documents
    db.bulkDocs(docs, function(err, response) {
       if (err) {
          return console.log(err);
       } else {
          console.log("Documents deleted Successfully");
       }
    });
    

    Save the above code in a file with name Remote_delete_AllDocuments.js. Open the command prompt and execute the JavaScript file using node as shown below.

    C:PouchDB_Examples >node Remote_Delete_AllDocuments.js
    

    This deletes the contents of all given document that exists in the database named my_database which is stored in CouchDB, and displays the following message.

    Documents Deleted Successfully
    

    Khóa học lập trình tại Toidayhoc vừa học vừa làm dự án vừa nhận lương: Khóa học lập trình nhận lương tại trung tâm Toidayhoc

  • Khóa học miễn phí PouchDB – Update Batch nhận dự án làm có lương

    PouchDB – Update Batch



    You can update an array of documents in PouchDB at once using the bulkDocs() method. To do so you need to create an array of documents where, each document contains _id, _rev and the values that are to be updated.

    Suppose the database named my_database that is stored locally in PouchDB contains 3 documents namely doc1, doc2, doc3 with the following contents.

    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''}
    

    Suppose we have to increase the age values in all the 3 documents by 2 years. For this to happen, first you need to get the _rev values. Therefore, fetch the contents of these documents using the following code.

    //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);
       }
    });
    

    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 below.

    [
       {
          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 using their respective _id and _rev values as shown below.

    //Requiring the package
    var PouchDB = require(''PouchDB'');
    
    //Creating the database object
    var db = 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 Successfully");
       }
    });
    

    Save the above code in a file with the name Update_All_Document.js. Open the command prompt and execute the JavaScript file using node as shown below.

    C:PouchDB_Examples >node Update_All_Document.js
    

    This updates all the documents that exists in the database named my_database which is stored locally, displaying the following message.

    Documents Updated Successfully
    

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

    [
       {
          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 that is stored remotely on the server (CouchDB).

    To do so, instead of a database name, you need to pass the path to the database in CouchDB, which contains the document that is to be read.

    Example

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

    Updating Batch from a Remote Database

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

    Updating Batch

    Now, fetch the contents of these documents using the following code.

    //Requiring the package
    var PouchDB = require(''PouchDB'');
    
    //Creating the database object
    var db = 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.rows);
       }
    });
    

    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 below.

    [
       {
          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-9af15cb11054ebe03a7816bf6c5e4128'' },
          doc: {
             _id: ''002'',
             _rev: ''1-9af15cb11054ebe03a7816bf6c5e4128'',
             name: ''Robert'',
             age: 24,
             Designation: ''Programmer''
          }
       },
       {
          id: ''003'',
          key: ''003'',
          value: { rev: ''1-3033b5a78e915c52fd37325d42eb3935'' },
          doc: {
             _id: ''003'',
             _rev: ''1-3033b5a78e915c52fd37325d42eb3935'',
             name: ''Rahim'',
             age: 25,
             Designation: ''Programmer''
          }
       }
    ]
    

    Following is an example of updating all the documents that exists in a database named my_database which is stored in the CouchDB server.

    //Requiring the package
    var PouchDB = require(''PouchDB'');
    
    //Creating the database object
    var db = new PouchDB(''http://localhost:5984/my_database'');
    
    //Preparing the document
    docs = [{_id : ''001'', _rev: ''3-552920d1ca372986fad7b996ce365f5d'', age : 24, },
          {_id : ''002'', _rev: ''1-9af15cb11054ebe03a7816bf6c5e4128'', 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 Successfully");
       }
    });
    

    Save the above code in a file with the name Remote_Update_Document.js. Open the command prompt and execute the JavaScript file using node as shown below.

    C:PouchDB_Examples >node Remote_Update_Document.js
    

    This updates the contents of all given document that exists in the database named my_database which is stored in CouchDB, and displays the following message.

    Documents Updated Successfully
    

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

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

    Khóa học lập trình tại Toidayhoc vừa học vừa làm dự án vừa nhận lương: Khóa học lập trình nhận lương tại trung tâm Toidayhoc

  • Khóa học miễn phí PouchDB – Fetch Batch nhận dự án làm có lương

    PouchDB – Fetch Batch



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

    Syntax

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

    db.allDocs()
    

    Example

    Following is an example of retrieving all the documents in a database named my_database that is stored locally, using db.allDocs() method. This method retrieves the array of documents in the form of objects, to get the contents of each document you need to call as docs.rows.

    //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);
       }
    });
    

    Save the above code in a file with the name Read_All_Document.js. Open the command prompt and execute the JavaScript file using node as shown below.

    C:PouchDB_Examples >node Read_All_Document.js
    

    This reads all the documents that exists in the database named my_database which is stored locally. The following 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, using allDocs() method you can see only the _id, key and _rev fields of each document. However, to include the whole document in the result, you have to make the optional parameter include_docs true as shown below.

    //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);
       }
    });
    

    Executing the above code gives you a list of complete documents in the specified documents as shown in the following 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: ''Robert'',
             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 also fetch all the documents from the database that is stored remotely on the server (CouchDB).

    To do so instead of a database name, you need to pass the path to the database in CouchDB, which contains the document that is to be read.

    Example

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

    Reading a Batch from a Remote Database

    Following is an example of reading all the documents that exist in a database named my_database which is stored in the CouchDB server.

    //Requiring the package
    var PouchDB = require(''PouchDB'');
    
    //Creating the database object
    var db = 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.rows);
       }
    });
    

    Save the above code in a file with the name Remote_Read_AllDocument.js. Open the command prompt and execute the JavaScript file using node as shown below.

    C:PouchDB_Examples >node Remote_Read_AllDocument.js
    

    This reads the contents of the given document that exists in the database named my_database which is stored in CouchDB, and displays on the console as shown below.

    [
       {
          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-9af15cb11054ebe03a7816bf6c5e4128'' },
          doc: {
             _id: ''002'',
             _rev: ''1-9af15cb11054ebe03a7816bf6c5e4128'',
             name: ''Robert'',
             age: 24,
             Designation: ''Programmer''
          }
       },
       {
          id: ''003'',
          key: ''003'',
          value: { rev: ''1-3033b5a78e915c52fd37325d42eb3935'' },
          doc: {
             _id: ''003'',
             _rev: ''1-3033b5a78e915c52fd37325d42eb3935'',
             name: ''Rahim'',
             age: 25,
             Designation: ''Programmer''
          }
       }
    ]
    

    Khóa học lập trình tại Toidayhoc vừa học vừa làm dự án vừa nhận lương: Khóa học lập trình nhận lương tại trung tâm Toidayhoc

  • Khóa học miễn phí PouchDB – Create Batch nhận dự án làm có lương

    PouchDB – Create Batch



    You can create an array (batch) of documents in PouchDB using the db.bulkDocs() method. While creating documents, using this method if we do not provide _id values, on our behalf PouchDB generates unique ids for all the documents in the bulk.

    Syntax

    Following is the syntax of using the db.bulkDocs() method of PouchDB. You can store all the documents that are to be created in PouchDB in an array and pass it to this method as a parameter. In addition to it, this method also accepts a callback (optional) function as a parameter.

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

    Example

    Following is an example of creating multiple documents in PouchDB using the db.bulkDocs () method. The documents we create should be of JSON format, a set of key-value pairs separated by comma (,) and enclosed within 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");
       }
    });
    

    Save the above code in a file with name Create_Batch.js. Open the command prompt and execute the JavaScript file using node as shown below.

    C:PouchDB_Examples >node Create_Batch.js
    

    This creates the given document in PouchDB database named my_database which is stored locally. The following message gets displayed.

    Documents created Successfully
    

    Inserting a Batch in a Remote Database

    You can insert an array of documents in the database that is stored remotely on the server (CouchDB).

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

    Example

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

    Inserting a Batch in a Remote Database

    Following 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 = new PouchDB(''http://localhost:5984/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");
       }
    });
    

    Save the above code in a file with the name Remote_Create_Batch.js. Open the command prompt and execute the JavaScript file using node as shown below.

    C:PouchDB_Examples >node Remote_Create_Batch.js
    

    This creates the given documents in PouchDB database named my_database which is stored in CouchDB. The following message is displayed.

    Document created Successfully
    

    Verification

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

    Inserting Batch Verification

    Khóa học lập trình tại Toidayhoc vừa học vừa làm dự án vừa nhận lương: Khóa học lập trình nhận lương tại trung tâm Toidayhoc

  • Khóa học miễn phí PouchDB – Read Document nhận dự án làm có lương

    PouchDB – Read Document



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

    Syntax

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

    db.get(document, callback)
    

    Example

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

    //Requiring the package
    var PouchDB = require(''PouchDB'');
    
    //Creating the database object
    var db = 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 using node as shown below.

    C:PouchDB_Examples >node Read_Document.js
    

    This reads the contents of the given document that exists in the database named my_database which is stored locally. The following 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 also read a document from the database that is stored remotely on the server (CouchDB).

    To do so, instead of a database name, you need to pass the path to the database in CouchDB, which contains the document that is to be read.

    Example

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

    Reading a Document from a Remote Database

    By clicking on the database named my_database you can see the following screenshot. Here, you can observe that this database contains a document with id 001.

    Reading a Document

    Following is an example of reading the contents of the document having id as “001” that exists in a database named my_database, which is stored in the CouchDB server.

    //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);
       }
    });
    

    Save the above code in a file with the name Remote_Read_Document.js. Open the command prompt and execute the JavaScript file using node as shown below.

    C:PouchDB_Examples >node Remote_Read_Document.js
    

    This reads the contents of the given document that exists in the database named my_database which is stored in CouchDB. The following message is displayed on the console.

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

    Khóa học lập trình tại Toidayhoc vừa học vừa làm dự án vừa nhận lương: Khóa học lập trình nhận lương tại trung tâm Toidayhoc

  • Khóa học miễn phí PouchDB – Delete Document nhận dự án làm có lương

    PouchDB – Delete Document



    You can delete a document from a database that exists in PouchDB using the db.remove() method.

    Syntax

    Following is the syntax of using the db.remove() method of PouchDB. To this method, we have to pass id and _rev to delete an existing document as shown in the following code. This method accepts an optional callback function. We can also pass the complete document instead of id and _rev.

    db. get ( docId, docRev, [callback] )
    or
    db. get ( docId, docRev, [callback] )
    

    Example

    Assume we have a document in PouchDB with id 001 which have the details of a person. In order to delete this document along with its id we should also have its _rev number. Therefore, retrieve the contents of the document as shown in the following code.

    //Requiring the package
    var PouchDB = require(''PouchDB'');
    
    //Creating the database object
    var db = 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 following output.

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

    Now, using the _rev and id of the document you can delete this by using the remove() method as shown in the following code.

    //Requiring the package
    var PouchDB = require(''PouchDB'');
    
    //Creating the database object
    var db = new PouchDB(''my_database'');
    
    //Deleting an existing document
    db.remove(''001'', ''3-552920d1ca372986fad7b996ce365f5d'', function(err) {
       if (err) {
          return console.log(err);
       } else {
          console.log("Document deleted successfully");
       }
    });
    

    Save the above code in a file with the name Delete_Document.js. Open the command prompt and execute the JavaScript file using node as shown below.

    C:PouchDB_Examples >node Delete_Document.js
    

    This deletes the contents of the given document that exists in the database named my_database which is stored locally. The following message is displayed.

    Document deleted successfully
    

    Deleting a Document from a Remote Database

    You can also delete an existing document from the database that is stored remotely on the server (CouchDB).

    To do so, instead of a database name, you need to pass the path to the database in CouchDB, which contains the document that is to be read.

    Example

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

    Deleting a Document from a Remote Database

    By clicking on the database named my_database you can see the following screenshot. Here, you can observe that the database contains a document with id 001.

    Deleting Database

    Following is an example of deleting the contents of the document having id “001” that exists in a database named my_database which is stored in the CouchDB server.

    //Requiring the package
    var PouchDB = require(''PouchDB'');
    
    //Creating the database object
    var db = new PouchDB(''http://localhost:5984/my_database'');
    
    //Deleting an existing document
    db.remove(''001'', ''3-552920d1ca372986fad7b996ce365f5d'', function(err) {
       if (err) {
          return console.log(err);
       } else {
          console.log("Document deleted successfully");
       }
    });
    

    Save the above code in a file with name Remote_Delete_Document.js. Open the command prompt and execute the JavaScript file using node as shown below.

    C:PouchDB_Examples >node Remote_Delete_Document.js
    

    This deletes the given document that exists in the database named my_database which is stored in CouchDB. The following message is displayed.

    Document deleted successfully
    

    Khóa học lập trình tại Toidayhoc vừa học vừa làm dự án vừa nhận lương: Khóa học lập trình nhận lương tại trung tâm Toidayhoc

  • Khóa học miễn phí PouchDB – Update Document nhận dự án làm có lương

    PouchDB – Update Document



    Whenever, we create a document in PouchDB, a new field _rev is generated, and it is known as revision marker. The _rev‘s value is a unique random number, each time we make changes to the document the value of _rev is changed.

    You can update an existing document in PouchDB using the (_rev). To do so, first of all retrieve the _rev value of the document we want to update. Now, place the contents that are to be updated along with the retrieved _rev value in a new document, and finally insert this document in PouchDB using the put() method.

    Example

    Assume we have a document in PouchDB with id 001 which has details of a person. In order to update this document, we should have its rev number. Therefore, to retrieve the contents of the document the following code is used.

    //Requiring the package
    var PouchDB = require(''PouchDB'');
    
    //Creating the database object
    var db = 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 following output.

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

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

    //Requiring the package
    var PouchDB = require(''PouchDB'');
    
    //Creating the database object
    var db = new PouchDB(''my_database'');
    
    //Preparing the document for update
    doc = {
       age: 26,
       _rev: ''3-552920d1ca372986fad7b996ce365f5d'',
       }
    
    //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);
       }
    });
    

    Save the above code in a file with the name Update_Document.js. Open the command prompt and execute the JavaScript file using node as shown below.

    C:Pouch_Examples>node Update_Document.js
    

    This updates the contents of the given document that exists in the database named my_database which is stored locally. The following 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 also update an existing document in a database that is stored remotely on the server (CouchDB).

    To do so, instead of a database name you need to pass the path to the database in CouchDB, which contains the document that is to be updated.

    Example

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

    Updating a Document in a Remote Database

    By clicking on the database named my_database, you can see the following screenshot. Here, you can observe that this database contains a document with id 001.

    Updating Database

    Following is an example of updating the age of the document having id as “001” that exists in a database named my_database which is stored in the CouchDB server.

    //Requiring the package
    var PouchDB = require(''PouchDB'');
    
    //Creating the database object
    var db = new PouchDB(''http://localhost:5984/my_database'');
    
    //Preparing the document for update
    doc = {
       age: 26,
       _rev: ''3-552920d1ca372986fad7b996ce365f5d'',
       }
    
    //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);
       }
    });
    

    Save the above code in a file with the name Remote_Update_Document.js. Open the command prompt and execute the JavaScript file using node as shown below.

    C:PouchDB_Examples >node Remote_Update_Document.js
    

    This updates the contents of the given document that exists in the database named my_database which is stored in CouchDB. The following message is displayed on the console.

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

    Khóa học lập trình tại Toidayhoc vừa học vừa làm dự án vừa nhận lương: Khóa học lập trình nhận lương tại trung tâm Toidayhoc

  • Khóa học miễn phí PouchDB – Database Info nhận dự án làm có lương

    PouchDB – Database Info



    You can get the basic information about the database using the method named info()

    Syntax

    Following is the syntax of using the info() method of PouchDB. This method accepts a callback function.

    db.info([callback])
    

    Example

    Following is an example of retrieving database information using the info() method. Here, we are displaying the information of the database named my_database. In case of error, the error will be displayed on the console.

    //Requiring the package
    var PouchDB = require(''PouchDB'');
    
    //Creating the database object
    var db = 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 using node as shown below.

    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_compaction: false,
       adapter: ''leveldb''
    }
    

    Remote Database Info

    In the same way, you get the information of a database that is saved remotely on the server (CouchDB). To do so, instead of database name, you need to pass the path to the required database in CouchDB.

    Example

    Following is an example of retrieving information of a database that 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 = 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 using node as shown below.

    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,
       compact_running: false,
       disk_size: 79,
       data_size: 0,
       instance_start_time: ''1458209191708486'',
       disk_format_version: 6,
       committed_update_seq: 0,
       host: ''http://localhost:5984/my_database/'',
       auto_compaction: false,
       adapter: ''http''
    }
    

    Khóa học lập trình tại Toidayhoc vừa học vừa làm dự án vừa nhận lương: Khóa học lập trình nhận lương tại trung tâm Toidayhoc