Skip to content

Instantly share code, notes, and snippets.

@rwaldron
Created October 19, 2010 22:58
Show Gist options
  • Save rwaldron/635342 to your computer and use it in GitHub Desktop.
Save rwaldron/635342 to your computer and use it in GitHub Desktop.
Working through the indexedDB API that was released in Chromium 8.0.552.5 dev
<!doctype html>
<html>
<head>
<title>IDB* API</title>
<script src="idb.js"></script>
</head>
<body>
<input type="button" id="button" value="message" />
</body>
</html>
var global = this,
dbOpen, db, store, index, result;
document.addEventListener('DOMContentLoaded', function () {
dbOpen = indexedDB.open('testDB', 'This is my temporary indexedDB');
console.log( "dbOpen", dbOpen );
for ( var prop in dbOpen ) {
console.log(prop, typeof dbOpen[prop]);
}
dbOpen.addEventListener('success', function (event) {
// IDBSuccessEvent
console.log("event", event);
// IDBDatabase
console.log("event.result", event.result);
// Alias IDBDatabase
db = event.result;
for ( var prop in db ) {
//console.log(prop, db[prop]);
// name tempDB
// version a version id
// objectStores DOMStringList
// createObjectStore function createObjectStore() { [native code] }
// removeObjectStore function removeObjectStore() { [native code] }
// setVersion function setVersion() { [native code] }
// transaction function transaction() { [native code] }
// close function close() { [native code] }
}
var vdb = db.setVersion('a version id');
vdb.addEventListener('success', function (event) {
console.log("event", event);
console.log("event.result", event.result);
console.log("db.version", db.version);
// objectStores [methods: contains, item]
console.log("db.objectStores", db.objectStores);
console.log("typeof db.objectStores", typeof db.objectStores);
// object
console.log("db.objectStores.length", db.objectStores.length);
// 0
for ( var prop in db.objectStores ) {
console.log(prop, db.objectStores[prop], typeof db.objectStores[prop]);
// length 0
// item function item() { [native code] }
// contains function contains() { [native code] }
}
store = db.createObjectStore('cooler', null);
//store = db.objectStores.item(0);
console.log(db.objectStores.item(0));
index = store.createIndex('anIndex', 'foo', true);
console.log("store", store); // null ?
console.log("db.objectStores", db.objectStores);
console.log("db.objectStores.length", db.objectStores.length);
console.log("db.objectStores.contains('cooler')", db.objectStores.contains('cooler'));
console.log("index", index);
console.log("store.indexNames.contains('anIndex')", store.indexNames.contains('anIndex'));
/*
transaction = db.transaction();
store = transaction.objectStore('cooler');
result = store.add({ 'foo': 'bar'}, 'key');
console.log("result", result);
*/
}, false);
}, false);
}, false);
// Constants
console.log(IDBKeyRange.SINGLE, 0);
console.log(IDBKeyRange.LEFT_OPEN, 1);
console.log(IDBKeyRange.RIGHT_OPEN, 2);
console.log(IDBKeyRange.LEFT_BOUND, 4);
console.log(IDBKeyRange.RIGHT_BOUND, 8);
console.log(IDBDatabaseException.UNKNOWN_ERR, 0);
console.log(IDBDatabaseException.NON_TRANSIENT_ERR, 1);
console.log(IDBDatabaseException.NOT_FOUND_ERR, 2);
console.log(IDBDatabaseException.CONSTRAINT_ERR, 3);
console.log(IDBDatabaseException.DATA_ERR, 4);
console.log(IDBDatabaseException.NOT_ALLOWED_ERR, 5);
console.log(IDBDatabaseException.SERIAL_ERR, 11);
console.log(IDBDatabaseException.RECOVERABLE_ERR, 21);
console.log(IDBDatabaseException.TRANSIENT_ERR, 31);
console.log(IDBDatabaseException.TIMEOUT_ERR, 32);
console.log(IDBDatabaseException.DEADLOCK_ERR, 33);
console.log(IDBRequest.LOADING, 1);
console.log(IDBRequest.DONE, 2);
console.log(IDBCursor.NEXT, 0);
console.log(IDBCursor.NEXT_NO_DUPLICATE, 1);
console.log(IDBCursor.PREV, 2);
console.log(IDBCursor.PREV_NO_DUPLICATE, 3);
console.log(IDBTransaction.READ_WRITE, 0);
console.log(IDBTransaction.READ_ONLY, 1);
console.log(IDBTransaction.SNAPSHOT_READ, 2);
console.log(IDBTransaction.VERSION_CHANGE, 3);
<!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>
// *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);
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment