Getting Started with PouchDB

--

PouchDB is an open-source JavaScript database inspired by Apache CouchDB that is designed to run well within the browser.

In this article , I will be explaining about the installation of pouchDB and also some basic CRUD operations involved.

Before playing with database we need to install it , below I have mentioned the installation methods for the database.

Installation

For installing pouchDB in your system , you can either include the cdn or install using package manager like npm , bower etc.

Using CDN :

Using npm :

npm install --save pouchdb 

after installing using , you have to use require inorder to use pouchDB for your application.

let PouchDB = require('pouchdb');
let db = new PouchDB('my_database');

Now the installation part is done , we will see some basic CRUD operations using pouchDB. You can also refer to pouchDB’s official documentation, mentioned the link below,

Getting started Guide with PouchDB

WORKING WITH DATABASE :

CREATING THE DATABASE :

You can create the local database using PouchDB constructor. Method goes as follows

GETTING THE INFO ABOUT THE DATABASE :

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

Now if you run the file from command line , you will be getting the output as below,

DELETING THE DATABASE :

You can delete the database using db.destroy method. Example for deleting the method goes as follows

WORKING WITH DOCUMENTS :

PouchDB is a no-sql database , so instead of storing the data in relational form , data is stored in the form of documents. You can perform the basic CRUD operations in documents.

Comparison chart from pouch db official docs

CREATING A DOCUMENT :

Documents can be created using db.put method.

You will get the following message from the console if you run the program

Document created successfully

If you try to add the same document , you will get the following error

DISPLAYING THE DOCUMENT :

You can display the document using db.get which will take a key as argument which is supposed to be unique.

You will get the output as follows

UNDERSTANDING REVISIONS (`_rev`)

The new field _rev is called revision-marker which will be updated whenever you create or update the documents.We will be using _rev when we are updating the documents.

UPDATING THE DOCUMENTS :

So inorder to update the documents , you need to get the _rev but you don’t need to manually add the _rev as it is already present in the doc which we will be fetching.

Now you will get the following result in the console

DELETING THE DOCUMENTS:

You can use the db.delete() method to delete the documents in the database.

You will get the following output with result

Useful Resources :

  1. pouchDB official Docs
  2. pouchDB tutorial — Tutorials point

Conclusion:

This concludes some of the basic operations that can be performed with pouchDB. Thanks for spending your time , to read the article. Feedbacks about the article are more welcome.

--

--