Querying Your PouchDB Database With SQL

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

Glynn Bird
Jul 31, 2017 · 2 min read

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:

Image for post
Image for post

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.

Center for Open Source Data and AI Technologies

Things we made with data at IBM’s Center for Open Source…

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store