- Detailed API documentation
- Example on using the new library
- Test cases to see if the library runs on your browser.
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.
As indicated in the original blog post about the guidelines for this API,
- 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.
- 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.
- Use of Jquery Deferreds makes it gel well with other parts of Jquery and frameworks that use Jquery.
- Errors bubble up the method chain and hence are not required to be checked at every step of the API.
- 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.