The Internet Explorer team along with the broader Web community has continued to make improvements to the IndexedDB specification. IndexedDB is a W3C Working Draft that enables you to store, search, and retrieve data on the user's device, even when Internet connectivity is disabled. IndexedDB is a feature of the Web platform shared by IE10 and Metro style apps in the Windows 8 Consumer Preview.
This blog post describes changes we made in Internet Explorer 10 Consumer preview to implement the latest version of the W3C spec.
The most significant change involves how developers update their database schemas (i.e. object stores, indexes) from one version to the next. The working group (including Microsoft) decided to replace the IDBDatabase.setVersion API with a new IDBFactory.open API. The new open API uses an additional parameter to detect if you want to use the current database version of the or if you want to update the database schema to a newer version. The open API now returns an IDBOpenDBRequest object which lets you register for the onupgradeneeded event. In the event handler you can update your database schema by adding additional object stores and indexes.
IDBDatabase.setVersion
IDBFactory.open
open
IDBOpenDBRequest
To update your existing code, add a version parameter to your open method, replacing the call to setVersion, and register a new onupgradeneeded event handler on the IDBOpenDBRequest (see below).
version
setVersion
onupgradeneeded
Old code example – green highlights the impacted code
function openDBTest(dbName) { var rq = window.msIndexedDB.open(dbName); rq.onsuccess = successOpenningDB; rq.onerror = failureHandler; } function successOpenningDB(evt) { var db = evt.target.result; var rq = db.setVersion("1"); rq.onsuccess = successHandler; rq.onerror = failureHandler; } function successHandler(evt) { //create schema }
function openDBTest(dbName) {
var rq = window.msIndexedDB.open(dbName);
rq.onsuccess = successOpenningDB;
rq.onerror = failureHandler;
}
function successOpenningDB(evt) {
var db = evt.target.result;
var rq = db.setVersion("1");
rq.onsuccess = successHandler;
function successHandler(evt) {
//create schema
New code example – yellow highlights the changed code
function openDBTest(dbName) { var rq = window.msIndexedDB.open(dbName, 1); rq.onsuccess = useDB; rq.onupgradeneeded = successHandler; rq.onerror = failureHandler; } function successHandler(evt) { //create schema }
var rq = window.msIndexedDB.open(dbName, 1);
rq.onsuccess = useDB;
rq.onupgradeneeded = successHandler;
In addition, we made several smaller (but still significant) changes to the platform:
IDBCursor.advance(count)
count
IDBObjectStore.count(key)
IDBIndex.count(key)
IDBFactory.cmp(first, second)
IDBKeyRanges
IDBObjectStore.count
IDBObjectStore.delete
IDBIndex.count
IDBObjectStore
onsuccess
// This won’t abort the transaction because there is no active transaction. function createTransaction() { var txn = db.transaction("test"); window.aaaaaa(); } // This will abort the transaction because the exception is thrown when there is an active transaction. var rq = objectStore.add(record); rq.onsuccess = function (event) { window.aaaaaa(); }
// This won’t abort the transaction because there is no active transaction.
function createTransaction() {
var txn = db.transaction("test");
window.aaaaaa();
// This will abort the transaction because the exception is thrown when there is an active transaction.
var rq = objectStore.add(record);
rq.onsuccess = function (event) {
Screen shot of IE Test Drive demo Facebook Companion
We also submitted over 100 test cases to the W3C working group for IndexedDB, including new tests and updates to existing tests.
In addition to updates to match changes in the W3C specification, the Windows 8 Consumer Preview includes improvements that allow users to remain in control of how sites and apps use IndexedDB on their devices:
Screen shots of the new Caches and databases tab of the Website Data Settings dialog and the updated Delete Browsing History dialog
The W3C WebApp Working Group continues to drive IndexedDB to completion by making important improvements and reducing the number and scope of changes made to the spec. This is an important step for Web developers building on this technology in the near future, including in Windows 8 and IE10.
We look forward to seeing how you use IndexedDB in your sites and apps, and welcome your feedback on IndexedDB in IE10.
—Israel Hilerio, Ph.D., Principal Program Manager, Internet Explorer