January 2015

Gavin Pickin

Cordova Cross Platform Database Issue - The Solution Might Surprise You

Android, Cordova / Phonegap, IOS

There was a point when Local Storage key value pair just didn’t seem to be a good solution. Its great for small key and values, but when you’re starting to dump JSON Objects Arrays, which have children, and you do not have all the NoSQL goodness, its time to get something bigger and beefier.

This App I am working on is an interesting one. The project itself is tied to a bigger internal system we are building, and like that, this App has evolved, before even being released. We started off, planning to target a Surface, so I decided that JavaScipt and HTML5 is a good solution, since Windows 8 allows you to wrap those now, so off we went. After that, the target changed, from Surface to iPad, from iPad to Android, and now, Android back to Surface. Luckily for me, my project, with Cordova only had to adapt slightly, as we changed.

Now, the interesting part here is, although the App moves pretty smoothly from platform to platform… it does, assuming each platform has the same support for technology. Local storage, yes it does… but as I mentioned above, I am needing to migrate from Local Storage to a bigger data storage option. Since I have setup the app with JSON styled storage, I first look at NoSQL solutions… preferably with some type of sync built in, since this app is going to need it, and I would love the help.

Looking a CouchBase Mobile, Pouch, a lot of great options out there, I come to one sad conclusion… NO SUPPORT FOR WINDOWS 8. Now this might not be 100% true, but with respect to Cordova plugins… I see 0 Windows 8 compatible Cordova Plugins.

So, I try to get back to the basics, HTML5 supports WebSQL right?
Not on Windows 8

This is where Microsoft steps in?
I wonder what you are thinking here… Bad Microsoft, no support right?
Well, that is where you are wrong.

A lot of people bash on Microsoft… but there are changing, they even started a new company called MS Open Tech, which is their Open Source Company, which works on Open Source projects, and being its own company, all the legalities are easier etc. They have done a lot of great work, and one of those items is a Cordova Plugin… com.msopentech.websql - Web Sql Plugin for Apache Cordova

At the writing of this, it has over 5500 downloads, compared to 3 and 60 for other SQL plugins which do not do the same thing.

MS Open Tech, looking to bridge the gap decided to create this plugin, under the covers, use a SQLite Database, but they made the plugin so it uses all the WebSQL APIs, so my app will not have a clue if its using WebSQL or SQLite. For those that don’t know, a polyfill filled the gap, and your Application cannot tell.

So now, if I deploy to Windows, all my DB calls go to the WebSQL plugin, which makes the appropriate SQLite calls under the covers. If I deploy to iOS or Android, the Plugin doesn’t do anything, and all my DB calls go to the native WebSQL API.

Of course, the API might not be perfect, as I use it more and more, I might find quirks, but so far so good. Its great to see big companies like Microsoft seeing the importance of Open Source, and how something as small as this, allows their Products to do better. If they did not have a easy solution to this problem, it just makes it harder for developers to develop for Windows 8… and without Apps, they will not have the desire for their Products.

MS Open Tech is doing a lot of cool things, Microsoft in general is, with Azure Cloud Services, Free versions of Visual Studio in the form if VS Express 2013… and a lot more.

Time to dive into this WebSQL (SQLite) now.

Thanks for reading.

by Jim Cumming
01/21/2015 06:48:52 AM

Looks like you've solved your local data storage woes already. But I thought I'd point you at LawnChair. It provides a consistent api to local storage across browser platforms and provides to useful extra functionality.

by Gavin
01/21/2015 10:20:37 AM

Actually, I have heard of Lawnchair, but haven't looked at it yet., thanks for the link, I will definitely have to try this out on some of my smaller simpler apps.

I think with the sync issues I'm facing, a real SQL db will work better, especially with my experience.

Blog Search