Querying Your PouchDB Database With SQL

Adapting the simple SQL support in my silverlining npm module to work with in-browser database PouchDB

--

I blogged recently about querying a Cloudant (and CouchDB 2.0) database with SQL by using the silverlining library to convert SQL queries into a form of JSON that the database can understand.

What about PouchDB?

You can deploy PouchDB in a variety of configurations, and it too can understand the JSON known as “Mango” or “Cloudant Query” queries using the pouchdb-find plugin.

So can PouchDB databases be queried using SQL too? Yes!

The pouchdb-silverlining npm module builds on pouchdb-find to provide SQL support in PouchDB through a new .sql() function in your Node.js code:

This setup also works in web page JavaScript by loading the libraries in sequence:

Blob image credit: mark du toit.

PouchDB.find is the future

The PouchDB roadmap has the find function as the default means of querying the database. The MapReduce-based query function will be relegated to a plugin, so when PouchDB hits version 7, the pouchdb-silverlining plugin may not require the pouchdb-find plugin as a dependency, because it will be built in to the product.

If you’re interested in how SQL is converted into Mango JSON, then you can read the code in the sqltomango module, which is used by both Silverlining libraries.

Happy querying!

If you enjoyed this article, please ♡ it to recommend it to other Medium readers.

--

--