JavaScript: indexedDB in Chromium 8.0.552.5 dev!
Posted by Rick Waldron
Oct 19 2010
UPDATE!!! This morning (2010-10-22) Chromium dev channel updated to Chromium 8.0.552.11 dev and the indexedDB implementation has been renamed to webkitIndexedDB.
I must admit – sometimes browser updates are like Christmas. Like today. I updated Chrome and as usual I went through the motions of inspecting the window (DOMWindow) object in the console looking for new goodies. I was definitely not let down. Right there between HashChangeEvent and Image: The IDB* API. Further down I found the key to them all: indexedDB. So far this is what I’ve come up with as a means to test out the implementation. I plan to add more to the gist as time permits.
webkit-idb.html
<!doctype html>
<html>
<head>
<title>webkitIndexedDB API</title>
<script src="webkit-idb.js"></script>
</head>
<body>
<input type="button" id="button" value="message" />
</body>
</html>
webkit-idb.js
// *UPDATED* Now used webkit prefixed API. see gist for history to access non-prefixed code
(function() {
var global = this,
dbOpen, db, index, result;
dbOpen = webkitIndexedDB.open( "testDB", "This is my temporary indexedDB" );
console.log( "dbOpen", dbOpen );
/*
IDBRequest {
errorCode: 0
onerror: null
onsuccess: null
readyState: 2
result: IDBDatabase
source: IDBFactory
transaction: null
webkitErrorMessage: undefined
}
*/
dbOpen.addEventListener( "success", function( event ) {
// webkitIDBSuccessEvent
console.log( "event", event );
// webkitIDBDatabase
console.log( "open: event.target.result", event.target.result );
// Alias webkitIDBDatabase
var db = event.target.result,
vdb = db.setVersion( btoa("ohhai!") );
vdb.addEventListener( "success", function( event ) {
var prop, store;
console.log( "set version: event.target.result", event.target.result );
console.log( "db.version", db.version );
// objectStores [methods: contains, item]
console.log( "db", db );
// DOMStringList
console.log( "db.objectStoreNames", db.objectStoreNames );
if ( !db.objectStoreNames.length ) {
store = db.createObjectStore( "cooler", {} );
index = store.createIndex( "anIndex", "foo", true );
console.log( "store", store ); // null ?
console.log( "store.indexNames.contains('anIndex')", store.indexNames.contains("anIndex") );
}
console.log( db.objectStoreNames.item(0) );
// "cooler"
}, false);
}, false);
// Constants
// Noted as they appeared in 2010
// These are no longer defined
// console.log(webkitIDBKeyRange.SINGLE, 0);
// console.log(webkitIDBKeyRange.LEFT_OPEN, 1);
// console.log(webkitIDBKeyRange.RIGHT_OPEN, 2);
// console.log(webkitIDBKeyRange.LEFT_BOUND, 4);
// console.log(webkitIDBKeyRange.RIGHT_BOUND, 8);
// These do not match 2010v2012
console.log(webkitIDBDatabaseException.UNKNOWN_ERR, 0);
console.log(webkitIDBDatabaseException.NON_TRANSIENT_ERR, 1);
console.log(webkitIDBDatabaseException.NOT_FOUND_ERR, 2);
console.log(webkitIDBDatabaseException.CONSTRAINT_ERR, 3);
console.log(webkitIDBDatabaseException.DATA_ERR, 4);
console.log(webkitIDBDatabaseException.NOT_ALLOWED_ERR, 5);
console.log(webkitIDBDatabaseException.SERIAL_ERR, 11);
console.log(webkitIDBDatabaseException.RECOVERABLE_ERR, 21);
console.log(webkitIDBDatabaseException.TRANSIENT_ERR, 31);
console.log(webkitIDBDatabaseException.TIMEOUT_ERR, 32);
console.log(webkitIDBDatabaseException.DEADLOCK_ERR, 33);
// These match 2010v2012
console.log(webkitIDBRequest.LOADING, 1);
console.log(webkitIDBRequest.DONE, 2);
// These match 2010v2012
console.log(webkitIDBCursor.NEXT, 0);
console.log(webkitIDBCursor.NEXT_NO_DUPLICATE, 1);
console.log(webkitIDBCursor.PREV, 2);
console.log(webkitIDBCursor.PREV_NO_DUPLICATE, 3);
// These do not match 2010v2012
console.log(webkitIDBTransaction.READ_WRITE, 0);
console.log(webkitIDBTransaction.READ_ONLY, 1);
// This is no longer defined
console.log(webkitIDBTransaction.SNAPSHOT_READ, 2);
// This took the place of the previous constant
console.log(webkitIDBTransaction.VERSION_CHANGE, 3);
})();
Contact Us
We'd love to hear from you. Get in touch!