Joseph Jude

Consult . Code . Coach

Using Pouchdb with nodejs


code . nodejs

Pouchdb is a good choice when you want to store unstructured data locally (without any servers). You can interact with Pouchdb with Javascript, which is a plus, because it reduces cognitive load on you.

When I wanted to store results from auditing websites, I used Pouchdb because of the reasons I mentioned in the earlier paragraph.

In this article, I explain how to use Pouchdb from nodejs.

Using Pouchdb with nodejs

As with any nodejs package we start with installing. Let us install it with npm i pouchdb --save.

Now it is available for us to use in our program.

const PouchDB = require('pouchdb');
let db = new PouchDB('sites');

Pouchdb provides .post, .get, .put, and .delete methods to work with the db. Pouchdb inherently supports promises, so we will use the promises in this article. You can also use the regular callback methods.

You can use either post or put to create a document. If you use post, an id is automatically created; if you use put, you need to supply the id. In some cases, you may want to supply the id explicitly.

siteUrl = 'http://jjude.com';
putDoc = {id: siteUrl, title: 'a new site by joseph'};
postDoc = {title: 'a new site by joseph'};

db.post(postDoc)
  .then(doc => {
    console.log(doc);
  })
  .catch(err => {
    console.log("got an err: ", err)
  })

This will return:

{ ok: true,
  id: '8C2BE173-5A6F-AC6B-8C3D-DC9776F4685C',
  rev: '1-0c0d853d7ae240df969521688779b6fa' }

Change it to put:

db.put(putDoc).then(function (response) {
  console.log(response)
}).catch(function (err) {
  console.log(err);
});

Now you have this response:

{ ok: true,
  id: 'http://jjude.com',
  rev: '1-06c9c24ea2e48c501ad8a42a0dc6d8ab' }

Since all querying operations are based on id, you should use put and supply the id. Pouchdb help document also recommends using put than post. If you use, put, and if you try to insert the same document, it will also throw an error. Another reason, why you should use put.

How to get back the document? Pouchdb offers a get method:

db.get('http://jjude.com')
.then(doc=>{
  console.log("got ", doc)
})

This will return the earlier inserted document.

You can use the same format to delete a document.

As you can see, in-built querying is limited. You can use pouchdb-find, which provides a Mongo like, easy-to-use query language. Code to pick up all the documents in the db is:

let PouchDB = require('pouchdb');
PouchDB.plugin(require('pouchdb-find'));

let db = new PouchDB('sites');

db.find({
  selector: { '_id': { '$gt': null } }
}).then(function (results) {
  results['docs'].forEach(result => {
    console.log(result)
  })
}).catch(function (err) {
  console.log(err)
});

References:


Like the post? Retweet it. Got comments? Reply.

Using @pouchdb with nodejs by @jjude: https://t.co/Dz7sf7VcER pic.twitter.com/rdWyn0BQeM

— Joseph Jude (@jjude) November 7, 2016
Share this on: Twitter / /

Comments

comments powered by Disqus