So, as always, I tend to feel I'm a bit late to things. Earlier today my coworker Andy was talking to me about PouchDB. PouchDB is a client-side database solution that works in all the major browsers (and Node) 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.
Edit on June 3, 2015: Oops, that isn't quite it - but close. The URL you get from the console is a root URL for your CouchDB instance. In order to work with a database, like todos, you want to add /todos (or whatever) to the end of the URL you use in your code.
Archived Comments
Raymond,
I have been reading your blog for a while and really find the information useful and practical. I am trying to build a cordova application with Ionic/AngularJS using pouch for persistence. Most of my data would be coming in from API sources such as Soundcloud, Vimeo and Flickr. What would be the best way to sync the data from the api with a cloudant Couchdb instance? Cron tasks with insert/update?
Not sure I can say what is best here. It sounds like you are saying you want to hit an API, but store the results in your own db. If you want to do this even w/o user intervention, then yes, you could set up a Node.js server that runs a task every N hours to fetch and store data. That way when the browsers run the app, they get the latest.
Does that answer your question?
Yep. Thanks !
Great! looking forward to use PouchDb and Cloudant on Bluemix. Actually your videos and posts help a lot to demystify what really Bluemix is. The info in the website does help too much to understand for folks like me that are not very familiar with cloud platforms concepts like containers, instances, droplets and so on.
Doesn't seems quite daunting after all and it seems it is not pricey for small-mid size projects!
"(And yes, I know other solutions exist too – and I’m going to explore this area more.)"
Does CouchDB (Cloudant) and PouchDB couple are still one of the most adequate solutions out there for an offline first approach for an hybrid app (with ionicframework), any other worthy options to explore?
Afaik Pouch is the best out there for doing syncing. For *just* client-side storage, you've got a lot of options. See my new book. ;)
Thanks Raymond! Yesterday I started to play around with pouchDB for a small ionic app that need to develop in order to store some app config settings, with no sync need (no real need to use PouchDB there but I was excited to use it).
The app works ok on the browser but when I tested on iOS device the data was only stored for the current app session. If I close and restart the app the data get lost, I read a lot about potential issues, tweaked the app, added some recommended cordova plugins, etc, but no results. Not sure if there are problems with PouchDB and iOS 9 and latest cordova. After several hours of frustration I decided to give it a try later and though "I think I will get Raymond's new book for client-side storage to apply it for this app".
Let me know how the book works for you. :)
Hi, I have been been using PouchDb with Cloudant in cordova and the syringing is very fast.
PouchDb FAQ Says:
-------------------------
PouchDB cant sync with MongoDB/MySQL/my current non-CouchDB database
as backend needs to speak the CouchDB replication protocol.
However there is a way to replicate the changes made in couchDb with mysql https://www.npmjs.com/packa...
My Question is; Is there a way to replicate the changes made in mysql to replicate within couchDb?
Here
https://gist.github.com/cfa...
is an implementation but am not sure whether this will work.
No idea. I didn't do replication between CouchDB and MySQL. That's a bit - well - a lot - off topic for this blog post. I'd post on the PouchDB support site (assuming they have one, or StackOverflow).
Hey if you can share your code of Syncing Data with PouchDB and Cloudant in Ionic, I'd be really grateful.
Hi Raymond Camden,
I was wondering if there is another hosting service for couchDB!? and I don't if it is possible to host CouchDB to my own server?
thanks
Milad
Best I can suggest is what I'd do - Google it. :) I'm 99.99% sure you can run Couch locally though.