JQuery plugin for IndexedDB API - version upgrade

A new version of the Jquery library for IndexedDB API has been released and here are some useful links for you to get started.

In my last post, I had written about the updates to the examples for IndexedDB based on the latest version supported on Firefox 12. The most significant change was the removal of the setVersion method and introduction on the onupgradeneeded paradigm. This method gives us a new perspective on how we manage object stores and indexes in IndexedDB.
Over the past few days, I have also updated the Jquery plugin for IndexedDB to reflect this change. Though the "onupgradeneeded" method is only available in Firefox as of now, other browsers will soon implement it. The new version of the library is currently implemented as jquery.indexeddb.new.js but will eventually be called jquery.indexeddb.js. Note that for now, the file jquery.indexeddb.js still points to the older version that works with the implementation of IndexedDB in the stable versions of the browser.
In addition to adding support of onupgradeneeded, some other parts of the API have also changed. Here is the structure of the new API.
  • $.indexedDB(database_name) is the way to open the database to the latest version
  • $.indexedDB(database_name, schema) initializes the database with the schema.
  • $.indexedDB(db_name).transaction(store_names).progress(transactionMethod) can be used to enclose a set of CRUD operations in a transaction.
  • transaction.createObjectStore() can be used to create object stores inside the transaction method.
  • $.indexedDB(db).objectStore(name).add() is a shorthand for creating a transaction with just the single add operation.
  • $.indexedDB(db).objectStore(name).each() can be used to iterate over the objects in the store. 
  • $.indexedDB(db).objectStore(name, true) is a short hand to create an object store if it does not exist.
For a detailed list of APIs, you can take a look at the API documentation
As indicated in the original blog post about the guidelines for this API,
  1. The API has a good support for method chaining for quick and shorthand operations without having to write boiler plate code like in the native IndexedDB API.
  2. Another example of short hand operations is the use of implicit transactions using the .objectStore() syntax. Not all IndexedDB CRUD operations have to be explicitly in a transactions.
  3. Use of Jquery Deferreds makes it gel well with other parts of Jquery and frameworks that use Jquery. 
  4. Errors bubble up the method chain and hence are not required to be checked at every step of the API. 
  5. Smart defaults in the form of range, direct, automatic object store creation, etc, make it easy to start using the API without sacrificing the flexibility. 
If you have a  live project that is data intensive and where it makes sense to cache some data at the client, you could give this plugin a try. However, it may take some time before the IndexedDB implementation in browsers itself is ready for production.

IndexedDB Examples updated for Firefox 11 - onupgradeneeded method


The latest editor's draft of IndexedDB has been revised with changes to manage versions of the database better using the onupgradeneeded method.
The onupgradeneeded method seems so obvious in hindsight and it is a great way to manage database versions, providing migration paths. Like traditional databases, the schema of IndexedDB would change over time with requirements to create additional stores and indexes. However, unlike traditional databases, these databases are distributed may not be able to follow a simple, single step upgrade path.
For example
  • Some users would have a version of 1, and return to the website when the version is upgraded to 2, and then to 3, etc. 
  • There may also be users who were on version 1, but would return to the website when the latest version is 7. The IndexedDB for such users would have to be guided through a migration path that runs the steps for upgrade in each version. 
The onupgrademethod callback will simply have migration code for all versions of IndexedDB, making database migration easier. You can take a look at the examples here - http://nparashuram.com/trialtool/index.html#example=/IndexedDB/trialtool/moz_indexedDB.html

The onupgradeneededmethod is supported in Firefox 11.0a Aurora. It is still not available in Google Chrome 18.0.1025.2 canary yet.