Category: pouchdb

  • 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 – Create Document nhận dự án làm có lương

    PouchDB – Create Document



    You can create a document in PouchDB using the db.put() method.

    Syntax

    Following is the syntax of using the db.put() method of PouchDB. You can store the document that is to be created in PouchDB, in a variable and pass as a parameter to this method. In addition, this method also accepts a callback (optional) function as a parameter.

    db.put(document, callback)
    

    Example

    Following is an example of creating a document in PouchDB using the put() method. The document 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 document
    doc = {
       _id : ''001'',
       name: ''Raju'',
       age : 23,
       designation : ''Designer''
       }
    //Inserting Document
    db.put(doc, function(err, response) {
       if (err) {
          return console.log(err);
       } else {
          console.log("Document created Successfully");
       }
    });
    

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

    C:PouchDB_Examples >node Create_Document.js
    

    This creates the given document in PouchDB database named my_database, which is stored locally, displaying the following message.

    Document created Successfully
    

    Inserting a Document in a Remote Database

    You can also insert a document in the database that is stored remotely on the server (CouchDB).

    To do so, instead of database name you need to pass the path to the database where you 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 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 following screenshot.

    Empty Database

    Following is an example of inserting a document in a database named my_database that 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 document
    doc = {
       _id : ''001'',
       name: ''Raju'',
       age : 23,
       designation : ''Designer''
       }
    //Inserting Document
    db.put(doc, function(err, response) {
       if (err) {
          return console.log(err);
       } else {
          console.log("Document created Successfully");
       }
    });
    

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

    C:PouchDB_Examples >node Remote_Create_Document.js
    

    This creates the given document in PouchDB database named my_database which is stored in CouchDB, displaying the following message.

    Document created Successfully
    

    Verification

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

    Remote Database 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 – Overview nhận dự án làm có lương

    PouchDB – Overview



    This chapter provides a brief introduction to PouchDB along with its features and how it works.

    What is PouchDB?

    PouchDB is an open source in-browser database API written in JavaScript. It is modelled after &minuss; a NoSQL database. Using this API, we can build applications that work offline and online. It internally uses WebSQL and IndexedDB to store data.

    How Does it Work?

    In PouchDB, when the application is offline, the data is stored locally using WebSQL and IndexedDB in the browser. When the application is back online, it is synchronized with CouchDB and compatible servers.

    Using PouchDB, you can communicate with both local and remote databases seamlessly without noticing any difference.

    Features of PouchDB

    Following are the features of PouchDB −

    • Cross Browser − The API provided by PouchDB works the same in every environment, therefore, we can run a PouchDB application in various browsers.

    • Light Weight − PouchDB is a very light-weight API, it is also included easily just using a script tag.

    • Easy to Learn − If you have a prior knowledge 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

    Following are the advantages of PouchDB −

    • Since PouchDB resides inside the browser, there is no need to perform queries over the network, this results in faster execution of queries.

    • You can synchronize the data with any of the supported server and by doing so you can run apps both online and offline.

    Browsers that Support PouchDB

    Following are the browsers that support 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+

    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