You are viewing a read-only archive of the Blogs.Harvard network. Learn more.

Released: W3C Indexed Database API Draft Implementation

The W3C Indexed Database API working draft defines an method by which a developer may operate on a set of indexable object stores persisted in a client’s web browser environment. Values in an object store or index may be associated with a developer-specified primary/secondary key, or alternatively keys may be automatically generated based upon the value inserted. Left-, right-, full-, and un-bounded cursors are supported in both the forward and reverse direction. Each connection supports up to one active transaction across any set of open object stores (as is elucidated by the working draft).

The working draft exists as a incremental improvement over previous specifications (e.g. web storage) in that robust indexes and duplicate keys are supported. Indeed, an object store may have an arbitrary number of such indexes, each manually or automatically populated according to a developer’s needs. This API allows for advanced data scenarios on the client that were until now quite difficult (or not possible).

Unfortunately, there currently exists no reference implementation for this working draft. This project serves to fill this need, and exists as a browser plug-in that implements the API defined by the working draft.

Project Goals

My goals for any satisfactory solution include:

  • Implementing the Indexed Database API working draft with as much fidelity as is possible,
  • Producing a solution that is, insofar as is feasible, cross-browser compatible and platform agnostic,
  • Separating browser interoperation concerns from the underlying database technology such that new persistence logic may be easily introduced, and
  • Ensuring that the persistence layer has no direct dependencies upon the browser interoperation logic, so that the database persistence may be promoted or leveraged in outside projects (by being directly incorporated into a browser, for example).

Sample Code

By way of example, a client might leverage the Indexed Database API to perform client-side data persistence that reads:

connection = db.indexedDB.open("Fruits", "A Fruit Database!");

fruits = connection.createObjectStore("A Fruity Object Store", "fruit", true);
fruitIndexByColor = fruits.createIndex("A Fruity Index", "color", false);

fruits.put({ fruit: "Apple", color: "Red" });
fruits.put({ fruit: "Tangerine", color: "Orange" });
fruits.put({ fruit: "Grape", color: "Purple" });

var cursor = fruitIndexByColor.openCursor(undefined, db.IDBCursor.NEXT_NO_DUPLICATE);

assertEquals("Orange", cursor.key);
assertEquals("Tangerine", cursor.value);
cursor.continue();

assertEquals("Purple", cursor.key);
assertEquals("Grape", cursor.value);
cursor.continue();

assertEquals("Red", cursor.key);
assertEquals("Apple", cursor.value);
assertFalse(cursor.continue());
cursor.close();

Click here to access the project site for additional details and downloads.  Though the content herein is protected under the license below, be sure to consult the project license (GNU Lesser General Public License) for integration-related details.

As always, feedback is greatly appreciated.

B

Be Sociable, Share!

3 Comments

  1. Srri

    August 9, 2010 @ 4:16 am

    1

    Here is a playground for IndexedDB API that was released in Firefox 4 Beta 2
    http://tinyurl.com/ff-idxdb

  2. Brandon Haynes

    August 9, 2010 @ 9:41 am

    2

    Thanks for that Srri; I saw it come across on the W3C mailing list, and it is indeed a great resource!

    Brandon

  3. DayOfDNN Chicago

    August 30, 2010 @ 8:32 pm

    3

    Brandon Haynes, EverySports.net Inc….

Log in