Back to Home

PouchDB or what to do when “Internet is stable”

pouchdb · nosql · web development · web programming

PouchDB or what to do when “Internet is stable”

  • Tutorial
Nowadays, the speed of a WEB application greatly affects user loyalty. Often it is necessary to transfer some business logic to client code that runs in the user's browser.

In such a situation, it becomes necessary to quickly obtain data related to tasks. But sometimes the time for their request from the server database is too long, and sometimes the connection to the network is completely unstable or absent. In this situation, you can resort to storing data locally at the user and synchronize as necessary.

Acquaintance


PouchDB is a database that works on the basis of existing solutions for storing information in the user's browser. In fact, it acts as a facade and provides a universal API regardless of the conditions in which the application is running.

image

Versions of browsers running 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+

Since this is a NoSQL database, the terminology is slightly different from the usual relational solutions. You can see the difference in the correspondence table:

image

Base API


To create a database, just do:


and you can already work with it. Creating, deleting, and reading data from PouchDB is just as easy:


Where to use?


One of the main areas of application of such functionality is continuous operation from the point of view of the user. For example, the user began to fill out a form with some data, until he finished, you can save them locally. Thus, it is possible not only to enable the user to interrupt, but also to protect against the loss of entered data in case of errors during sending them to the server.

Local databases can also be used for caching. For example, a user is viewing ads from their smartphone. You can save the list locally and provide information much faster.

Another area is offline work. For example, a user can manage their notes without access to the Internet, and they are synchronized as soon as access appears. Anyway, you can build full-fledged offline or desktop applications, without limiting functionality and without additional costs for supporting or renting servers.

Replication and Conflict


The main feature of PouchDB is its flexible synchronization system. This database can be synchronized with other local and remote systems. Such functionality is available out of the box and does not require additional settings or writing a lot of code.

The only condition is that you can only synchronize with CouchDB-like protocols. It can be CouchDB, Cloudant, Couchbase Sync Gateway or PouchDB Server. The latter, in turn, can be a layer between other services, like Redis, Riak or MySQL, through the LevelUP ecosystem .

An example of simple synchronization:


During synchronization, a conflict situation may arise when the record has been changed on both sides since the last update. PouchDB uses a record revision system to properly resolve such conflicts.

Each entity has its own history of changes. Each change, including “creation”, generates a unique identifier for the version of the entity:


For any operations with entities, you must specify the revision number. In the simplest cases, this helps resolve so-called “immediate conflicts” using optimistic blocking.

In real life there is a more serious problem - “possible conflicts”. They can occur during database synchronization, and then to understand which version of the record should remain in the end becomes not so simple. To solve such situations, PouchDB records the history of changes to the entity and saves all versions during synchronization.

This allows you to resolve the conflict without blocking the synchronization process, either using the algorithm or manually.

Synchronization options can be different: unidirectional replication to either of two sides, bidirectional replication.
Configuration is done with a few lines of code:


or the other way:


or both at once:


As expected, you can handle the events generated during synchronization:


You can use this functionality for different types of applications:

  • In-memory ⇄ Local (for caching data);
  • Local ⇄ Local (between browsers or applications without a server side);
  • Remote ⇄ Remote (replication);
  • Local ⇄ Remote ⇄ Local (synchronization with backup on the server).


Map / Reduce Functions


To simplify and speed up content searches, PouchDB uses Map / Reduce functions. A close analogy to them is the construction of an index in a relational database.

For example, to “index” the name field in a document, you can use the following construction:


Using the created function is as follows:


You can use a large number of different parameters that affect the selection, for example, the maximum number of records, displaying full information about a document or only its identifier and others:


First you need to create a map function, for example:


It will "index" the specified fields. In this case, you can control the processing of each value. In the example below, only documents with the brick type are processed, and only the color field. In addition, the blue color will be processed and saved as the string “Wow! Blue brick! ”


Further on the created indexes, you can search or aggregate data by any parameter. To do this, reduce-functions are used, they combine all the data obtained after the map-function works and process them as needed. Using the written map function, you can implement:


Thus, first we will select records according to the specified parameters and ultimately calculate the number of documents found:


Conclusion


The functionality of PouchDB is not limited to this.

There are many more features recommended for advanced users. And if the possibilities “out of the box” are not enough, you can connect one or more plugins. Their choice is large enough and is constantly expanding.

Faced with problems of quick access to data, organization of serverless-architecture and unstable Internet connection, you can pay attention to PouchDB, as one of the solutions.

Read Next