Badass JavaScript

A showcase of awesome JavaScript that pushes the boundaries of what's possible on the web, by @devongovett.

IDBWrapper: A Cross Browser IndexedDB Wrapper for Offline Web Apps

February 6th 2013

Jens Arps has been working on IDBWrapper, a JavaScript library that wraps the IndexedDB API and makes it much easier to work with.  IndexedDB is very low level and perhaps one of the hardest browser APIs to deal with, so various wrappers are necessary to make it useful to most people.

IndexedDB is a JavaScript API found currently in Firefox, Chrome, and IE10 that implements a client side data store for web applications.  It stores data locally similar to localStorage, but can store much larger chunks of data and is asynchronous and much faster.  It was created after Mozilla refused to implement the now dead WebSQL spec over interoperability concerns of the unspecified SQL language itself.  At the time, all browsers implementing WebSQL used the opensource SQLite project so interoperability wasn’t a concern, but Mozilla feared that non-opensource browsers would have to use a different SQL implementation and that there would be differences.  The options were either to specify the entire SQL language in the WebSQL spec or scrap it all together and come up with a new spec.  Obviously, they chose the latter and now we have IndexedDB.  In my opinion it is a terribly designed API and we lost a lot of power when we lost SQL, but I suppose JavaScript libraries can help make up for this.

IDBWrapper is trying to do just that, making simple stuff like setting, getting, and deleting one or all records really easy, and making more complex stuff like queries simpler.  It also deals with several cross browser inconsistencies (ironic, after what I just wrote about WebSQL, right?).  IDBWrapper has been around since sometime in 2011, but just hit a 1.0 release, so I thought I’d mention it here.

You can find the code for IDBWrapper on Github, the up to date documentation, and two older blog posts that describe the API as more of a tutorial.  Although the IndexedDB is kinda ugly by itself, libraries can help just like they have with many other ugly web APIs like the DOM in the past.  Ugly or not, IndexedDB will be important in bringing offline web apps to the masses.