IndexedDB: constants changing to match spec

Showing 1-1 of 1 messages
IndexedDB: constants changing to match spec Alec Flett 5/8/12 3:55 PM
TL;DR: To match the current IndexedDB specification, we are changing some constants in the WebKit implementation. This affects all the various openCursor()methodsIDBDatabase.transaction(), and IDBRequest.readyState. This is landed yesterday (May 7th) in WebKit and will show up in Chrome 20. We are providing a mostly backwards-compatible API and while most usage of the API will continue to work, some usage may break.

The long version:

In keeping with current Web API trends, the latest revision of the IndexedDB spec (http://dvcs.w3.org/hg/IndexedDB/raw-file/tip/Overview.html) uses string constants for certain values, where enum-like 'unsigned short' values were used previously. These values were constants defined on the relevant interface.

For instance to iterate through a store or index backwards, the legacy API call looked like:

var request = datastore.openCursor('startingkey', IDBCursor.PREV);

In this case, IDBCursor.PREV is an unsigned short with the value 2.

To use the updated API, pass a string as the second parameter:

var request = datastore.openCursor('startingkey', 'prev');

The IDBCursor.PREV constant is no longer in the specification, but we will attempt to support this pattern. The API will still accept numeric values such as 2, and the constants will still be available. We encourage active migration to the new API and expect removal of the old API in August, 2012: https://bugs.webkit.org/show_bug.cgi?id=85315

The one place that this could cause problems for you is if you also access attributes such as the readyState of a request:

if (request.readyState == IDBRequest.DONE) { // THIS WILL ALWAYS FAIL!
    ...
}

Here, request.readyState will return "done" or "pending". 

Another example: examining the 'direction' attribute of the IDBCursor interface, for example:

datastore.openCursor('startingkey', IDBCursor.PREV).onsuccess = examineData;

function examineData(cursor) {
    if (cursor.direction == IDBCursor.PREV) { // THIS WILL ALWAYS FAIL!
      ...
    }
    ...
}

In this case, cursor.direction will return the string 'prev' and this test will always fail.

Another example of this this failure is looking at the
As a temporary measure to support both new and legacy APIs, you can define the constants you need:

var NEXT = ('NEXT' in IDBCursor) ? IDBCursor.NEXT : 'next';

The above applies to the following constants:
  • IDBDatabase.transaction(name, mode) and IDBTransaction.mode: Mode is now a string: 'readonly', 'readwrite' or 'versionchange' though of course 'versionchange' only occurs when the database is being upgraded.
  • IDBRequest.readyState: This is now 'pending' or 'done'
  • IDB*.openCursor(key, direction) / IDBIndex.openKeyCursor(key, direction) and IDBCursor.direction: Direction is now a string, 'next', 'nextunique', 'prev', or 'prevunique'

Let me know if you have any questions or comments.