Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
var currentTime = Date.now(),
dbName = "bugtest_" + currentTime,
indexes = [
{
name: "by_title",
path: "title",
unique: false
},
{
name: "by_author",
path: "author",
unique: false
}
],
createIndex = function(db, store, idx) {
console.log('creating index',store, db);
return [ store, store.createIndex(currentTime + "_" + idx.name, idx.path, {
unique: idx.unique || false
}) ];
},
getStore = function(event) {
var store = (event.transaction || event.target.transaction).objectStore(currentTime + "_" + "books", {
keyPath: "isbn"
});
console.log("got store", store);
return store;
},
createStore = function(db) {
var store = db.createObjectStore(currentTime + "_" + "books", {
keyPath: "isbn"
});
console.log("created store", store);
return store;
},
processIndexes = function(db, storeName) {
var next_request, idx = indexes.pop(), store, index;
console.log("processing next index",idx);
if (idx) {
console.log("opening database",dbName, db.version + 1);
next_request = indexedDB.open(dbName, db.version + 1);
next_request.addEventListener('error', function(event) {
console.log("Next request errors", storeName, event);
});
next_request.addEventListener('upgradeneeded', function(event) {
store = event.target.transaction.objectStore(storeName);
tuple = createIndex(event.target.result, store, idx);
store.transaction.addEventListener('complete',function() {
console.log("INDEX CREATE COMPLETE")
});
console.log('next request finished',event.target.result, tuple[0],tuple[1]);
event.target.transaction.db.close();
processIndexes(event.target.result, storeName);
});
} else {
console.log("Done creating indexes");
}
},
first_request = indexedDB.open(dbName),
was_upgraded = false;
first_request.addEventListener('success', function(event) {
console.log("success");
var db = event.target.result,
store;
if (!was_upgraded) {
store = getStore(event.transcation);
db.close();
processIndexes( db, store.name );
} else {
was_upgraded = false;
}
});
first_request.addEventListener('upgradeneeded', function(event) {
console.log("upgradeneeded");
var db = event.target.result,
store = createStore(db);
was_upgraded = true;
processIndexes( db, store.name );
db.close();
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment