Nerd Business

30
Dec
2017

Underpouch (v1.0 npm release)

Today I published underpouch v1.0. Because I wanted to share this logo!

but also cause I finished writing tests for every function. Well, every function that is currently available in my API here; the actual underscore API has more but these are the ones you may routinely need.

The README is rather straight forward particularly if you are familiar with PouchDB and underscore APIs.

Basically, underpouch just mashes the two together so you can query your PouchDB docs 'directly' with familiar underscore functions.

Read More

Doing the native db.allDocs and then filtering/finding/findWhering; querying the results with Underscore was a common pattern in my coding until creating this library; which I now drop into almost any project that uses PouchDB.

Consider:


  db.allDocs({include_docs:true}, (err, res) => {
    var targetDoc = _.findWhere(res.rows, { name : "Elvis" })
    //okay, we got the doc we wanted
  })
  

with underpouch:


  _pouch.findWhere(db, { name : "Elvis" }, (err, targetDoc) => {
    //targetDoc ready! 
  })


Bonus!

There are also some extra functions including: _p.all, _p.replace, _p.merge, and _p.deleteDocs. These can help save time & condense code. For example:


  db.get({_id : 'doc-i-want-to-update'}, (err, doc) => {
    doc.new_status = 'good to go'
    db.put(doc, (err, res) => {
      //replaced
    })
  })
  

with underpouch.replace:

    
  db.replace({_id : 'doc-i-want-to-update', status : 'good to go'}, (err, doc) => {
    //replaced! (no need to .get the revision)
  })
  
    

if in the above example, you wanted to avoid losing existing properties use instead _p.extend or _p.merge (or their cousins extendPut, extendPutOrPost, mergePut, mergePutOrPost).



Improvements

Note, there can be performance issues when working with large volume since underpouch makes frequent use of .allDocs.

Performance could be improved by using the pouchdb-find API to query and create an index on the first call of any given underpouch function if not already existing; such that repeated calls are indexed and fast. I started exploring that but felt it went a little beyond the scope of this module; at least for now.

So if you aren't dealing with a huge volume of docs, underpouch v1.0 should be just fine.

And if you come across the need for an underscore style query on your docs which is not currently implemented, please write it! Your contribution of code is welcomed.