How-tos

Connecting PouchDB to Cloudant on IBM Bluemix

Republished from Raymond Camden’s Blog


 

So, as always, I tend to feel I’m a bit late to things. Earlier today my coworker Andy Trice was talking to me about PouchDB. PouchDB is a client-side database solution that works in all the major browsers (and Node.js) and intelligently picks the best storage system available. It is even smart enough to recognize that while Safari supports IDB, it doesn’t make sense to use it and switches to WebSQL. It has a relatively simply API and best of all – it has incredibly simple sync built in.

I tend to work with client-side databases with just the vanilla JavaScript APIs available to them, but honestly, after an hour or so of using PouchDB I can’t see going back. (And yes, I know other solutions exist too – and I’m going to explore this area more.) Probably the slickest aspect is the sync. If you have a CouchDB server setup, you can set up automatic sync between all the database instances in seconds. For my testing, I decided to use IBM Bluemix. This blog post assumes you’re following the PouchDB Getting Started guide.

First, add the Cloudant NoSQL DB service to your Bluemix app:

After you have added the service and restaged your app, select it, and then hit the Launch button:

This fires up the Cloudant administrator where you can do – well – pretty much everything related to setting up your database. But to work with that guide at PouchDB, select Databases and then “Add New Database”:

Then enter todos to match the guide:

Ok, you’re almost done. You then want to enable CORS for your Cloudant install. In the Cloudant admin, click Account and then CORS. Enable it, and then select what origin domains you want. For now, it may be easier to just allow all domains.

Woot! OK, one more step. When using PouchDB and sync, they expect you to supply a connection URL. You can get this back in your Bluemix console. Select the “Show Credentials” link to expand the connection data and then copy the “url” portion.

And voila – that’s it. If you open your test in multiple browsers, you’ll see everything sync perfectly. Remember you can also use PouchDB in Node.js, which, coincidentally, you can also host up on Bluemix, so yeah, that works out well too.

Share this post:

Share on LinkedIn

Add Comment
One Comment

Leave a Reply

Your email address will not be published.Required fields are marked *


Ahmet

Thanks for the post! I wonder how can I create database per user for todos, since I don’t want people access to each other’s todos.

Reply
More How-tos Stories

Getting Started with AT&T M2X on IBM Bluemix

Last year, we announced our new partnership with IBM to bring our IoT Platform capabilities to Bluemix, IBM’s cloud development platform. Our goal is to make it easy to launch IoT apps quickly, reliably, and at scale. This blog post shows you how to get started with AT&T’s M2X Data Service on the IBM Bluemix platform.

Migrate Analytics for Apache Spark Notebooks to the IBM Data Science Experience

Hello IBM Analytics for Apache Spark Users, We will be discontinuing support for the Jupyter notebooks on Bluemix as of April 6, 2017. As an IBM Analytics for Apache Spark user, we’d like you to leverage the latest Jupyter Notebooks on the Data Science Experience by migrating your notebooks (Spark 1.4.1 and later versions). Please […]

Create Swift mobile apps with IBM Watson services, part 1: Sentiment Analysis

Learn how to build Swift-based, cognitive mobile applications using the Watson services on Bluemix. Alchemy API, Visual Recognition and Speech to Text.