Support for Out of Line Keys - IndexedDB Jquery Plugin

In a comment to a previous post, a reader had observed that there was no way in the Jquery IndexedDB plugin for creating Object Stores with Out of Line Keys.
According to the IndexedDB Specification, an out-of-line key is a type of key that is used when no key path is specified while creating the object store. In such cases, the key needs to be specified during an add or a put (update) operation as the second parameter.
To explain how out of line keys are supported, a quick introduction to the ways an object store can be created.

Create Statement
An object store is created using the explicit create statement that looks like


$.indexeddb("BookShop1").createObjectStore("BookList", {
    keyPath: "id",
    autoIncrement: true
}).then(console.info, console.error);
 

The second option basically defines the key generator and the key path. This option can be "undefined" or omitted to indicate out-of-line key, just like the createObjectStore statement in the original specification.

Get Object Store
Even as I was writing application leveraging IndexedDB, I noticed that I was painful to get an object store, check for its existence, and create it if it does not exist. Since this is a very common case, the get object store statement defaulted to creating an object store if one did not exist already. Deciding the defaults for this was tricky, but here is what I found most logical from my experiences.
  • In most cases, the developer would love to do something like $.indexeddb("DB").objectStore("BookList").add(data) and would like BookList to be created if it does not exist. The add statement also has the key as an optional parameter. 
  • This is a case where the developer explicitly specifies the key path and key generator as the second parameter to the objectStore() function. Thought it is longer, the developer needs more control over the keyPath created.
  • This is a case where the developer does not want the object store to be created when it does not exist. The developer would probably use the createObjectStore() call to create an object store explicitly.
Taking these three cases into account, an acceptable default for the GetObjectStore call would be
  1. If the second parameter is NOT specified, use out of line keys, and make them auto-increment. This way, the user is not required to specify the key for every add statement. 
  2. If the second parameter is specified and set to an object, use that object and pass it to the createObjectStore IndexedDB call. The IndexedDB implementation would use the key path and generator values from this.
  3. If the second parameter is false, the user explicitly has called out the "createOption" to false - so do not create the object store. 
 Understanding this could be a little harder, but i hope that these choices make life easy for 80% of the developers who just access data store and want everything else to magically work. This has been implemented as a part of this commit. 

As usual, comments are welcome. You can also follow this space for updates on the plugin.