PouchDB .get internals

I decided to take a quick look at the PouchDB source code. How I explored the code: I choose an entry point of interest, for example how ‘get’ works, – and then I started grepping/exploring the code. After finding ‘get’ I looked into the implementation on top of LevelUp.

  • PouchDB has storage/database adaptors in lib/adapters/. The backends are IndexedDB, WebSQL, LevelUp(LevelDB and others, primarily on node.js), and http-client for CouchDB etc.
  • The actual get API-function is defined is passed through utils.adapterFun, which adds API-logging, and promises. PouchDB supports both calling with promises and callbacks.
  • The entry point for get is in adapter.js which contains code that is common for the adapters. The http-adapter also overrides/defines it on its own, as the things is handled by remote CouchDB instead.
  • adapter.js extract options, and implements the open_revs option, used for fetching all/specific older revisions. It also fetches attachments, and extract the specific doc etc.
  • The object we get from the adapter contains a json-object, that has doc(document), metadata, and ctx(some kind of context, used for attachments and maybe also other stuff).
  • The core implementation is ._get (in the leveldb adapter).
  • It is wrapped in a read lock, which queues the reads and execute the next one from the queue.
  • Each get has two LevelUP database gets: first it gets the metadata, and then it gets the actual document, which resides in a different data store.

The code is easy to read, and appears to be well engineered. Could be interesting to add a node-couchbase adaptor some day, if time permits.