CRUD usando código de amostra indexedDB / WebSQL

Este código de amostra é parte de um aplicativo no qual estou trabalhando. Eu sou meio que novato, então, por favor, dê algum feedback construtivo. O código é baseado neste código de amostra: http://nparashuram.com/trialtool/#example=/IndexedDB/trialtool/index.html&selected=

Para compatibilidade com WebSQL, use seu IndexedDBShim ( http://nparashuram.com/IndexedDBShim/ )

Acho que é muito mais fácil escolher esse código e adaptá-lo ao seu próprio cenário. Você está convidado a apresentar melhorias.

Se for difícil de ler, obtenha o script neste link:
https://www.dropbox.com/s/kc5rjp8dmwcl1ze/myIndexedDB.js

/* -------------------------- */
/* myindexedDB
/* -------------------------- */


var indexedDBModule = (function(){

//'global' index;
var i = 0;
//keep a refference to the database name in dbName
var dbName;

//prefixes of implementation that we want to Test
var indexedDB = window.indexedDB || window.mozIndexedDB || window.webkitIndexedDB || window.msIndexedDB;
//not used yet
var IDBTransaction = window.IDBTransaction || window.webkitIDBTransaction || window.msIDBTransaction;
var IDBKeyRange = window.IDBKeyRange || window.webkitIDBKeyRange || window.msIDBKeyRange;

var init = function(db,store) {
if (!indexedDB) {
alert
("Sorry!Your browser doesn't support IndexedDB");
} else {
initDB
(db,store);
}
};

var destroy = function(db){
try {
//request to delete the entire database
if(!db || db=='')
var dbDeleteRequest = indexedDB.deleteDatabase(DB_NAME);
else var dbDeleteRequest = indexedDB.deleteDatabase(db);
dbDeleteRequest
.onsuccess = function(e) {
alert
('Database successfully deleted');
/*
* I found out that when I need to destroy the database and recreate

* it, there are some issues. I tried to destroy database, create database,

* insert items, deleteAllItems, count the items in the objectStore etc...

* after destroying the database, a location.reaload() helps refreshing these operations

*/

location
.reload();
};
dbDeleteRequest
.onupgradeneeded = function(e) {
console
.log('Database upgrade needed ');
};
dbDeleteRequest
.onerror = function(e) {
console
.log('Error deleting database ');
};
dbDeleteRequest
.onblocked = function(e) {
console
.log('Deleting Database Blocked... Try closing the database and then deleting it ');
};
} catch (e) {
console
.log(e);
alert
('Error on destroy -> deleteDatabase ');
}
};

var createItem = function(id,data,store,update){
try {
//request to open the database
var dbOpenRequest = indexedDB.open(dbName);
dbOpenRequest
.onsuccess = function(event) {
var thisDB = dbOpenRequest.result;
thisDB
.onversionchange = function(e) {
console
.log('Version change triggered, so closing database connection ' + e.oldVersion + ' ' + e.newVersion + ' ' +thisDB);
thisDB
.close();
};
try{
var transaction = thisDB.transaction([store], "readwrite");
transaction
.oncomplete = function(e) {
console
.log('transaction is complete ');
};
transaction
.onabort = function(e) {
var error = e.target.error; // DOMError
if (error.name == 'QuotaExceededError') {
alert
('createItem -> transaction['+store+'] -> onabort - QuotaExceededError ');
}
console
.log('transaction aborted ');
};
transaction
.onerror = function(e) {
console
.log('transaction error');
};
try {
var request;
var objectStore = transaction.objectStore(store);
if(update && update==true){
//request to UPDATE new entry in the object store
request
= objectStore.put(data);
} else{
//request to ADD entry in the object store
request
= objectStore.add(data);
}
request
.onsuccess = function(event) {
//var element = event.target.result;
if(update && update == true)
console
.log('The record with ID = ' + id + ' has been updated in the database ');
else console.log('A new record with ID = ' + id + ' has been added to the database ');
};
request
.onerror = function(event) {
if (update && update == true){
console
.log('Error. Could not update record with id = ' + id);
}
else {
console
.log('Error ' + event + ' This record is already in the database. ');
}
};
} catch (e) {
console
.log('Could not operate on '+ '['+store+']' +'. You may have to create it first ');
};
} catch(e){
console
.log('Error ' + e);
};
};
dbOpenRequest
.onupgradeneeded = function(e) {
console
.log('Database upgrade needed ');
//not used
//var db = dbOpenRequest.result;
//var transaction = dbOpenRequest.transaction;
};
dbOpenRequest
.onerror = function(e){
console
.log('Error ');
};
dbOpenRequest
.onblocked = function(e) {
console
.log('Database open request blocked ');
};
} catch(e){
console
.log('Error ');
};
};

var readItem = function(id,store) {
try {
var dbOpenRequest = indexedDB.open(dbName);
dbOpenRequest
.onsuccess = function(event) {
var thisDB = dbOpenRequest.result;
thisDB
.onversionchange = function(e) {
console
.log('Version change triggered, so closing database connection ' + e.oldVersion + ' ' + e.newVersion + ' ' +thisDB);
thisDB
.close();
};
try{
var transaction = thisDB.transaction([store], "readonly");
transaction
.oncomplete = function(e) {
console
.log('transaction is complete ');
};
transaction
.onabort = function(e) {
console
.log('transaction aborted ');
};
transaction
.onerror = function(e) {
console
.log('transaction error ');
};

try {
var objectStore = transaction.objectStore(store);
var request = objectStore.get(id);
request
.onsuccess = function(event) {
var element = event.target.result;
if(element!==undefined){
console
.log('Elemtent with id = ' + id + ' was foundn' + JSON.stringify(element));
}
else{
console
.log('Element with id = ' + id + ' does not exist in the IndexedDB ');
}
};
request
.onerror = function(event){
console
.log('Error - could not request data from the object store ' + event);
};
} catch (e) {
console
.log('Could not operate on '+ '['+store+']' +'. You may have to create it first ');
}
} catch(e){
console
.log('Error ');
}
};
dbOpenRequest
.onupgradeneeded = function(e) {
console
.log('Database upgrade needed ');
//var db = dbOpenRequest.result;
//var transaction = dbOpenRequest.transaction;
};
dbOpenRequest
.onerror = function(e){
console
.log('Error ');
};
dbOpenRequest
.onblocked = function(e) {
console
.log('Database open request is blocked ');
};
} catch(e){
console
.log('Error ');
};
};

var updateItem = function(id,data,store){
createItem
(id,data,store,true);
};

var deleteItem = function(id,store) {
try {
var dbOpenRequest = indexedDB.open(dbName);
dbOpenRequest
.onsuccess = function(event) {
var thisDB = dbOpenRequest.result;
thisDB
.onversionchange = function(e) {
console
.log('Version change triggered, so closing database connection ' + e.oldVersion + ' ' + e.newVersion + ' ' +thisDB);
thisDB
.close();
};
try{
//attempt to create a transaction
var transaction = thisDB.transaction([store], "readwrite");
transaction
.oncomplete = function(e) {
console
.log('transaction is complete ');
};
transaction
.onabort = function(e) {
console
.log('transaction aborted ');
};
transaction
.onerror = function(e) {
console
.log('Transaction error -> [deleteItem] ');
};

try {
var objectStore = transaction.objectStore(store);
var getRequest = objectStore.get(id);
getRequest
.onsuccess = function(e){
if (e.target.result != null) {
console
.log('Object Exist');
//request to delete entry from the object store
var request = objectStore.delete(id);
request
.onsuccess = function(event){
console
.log('Entry id = ' + id + ' was deleted from the database');
};
request
.onerror = function(event){
console
.log('Error [deleteItem] -> delete('+id+') ');
};
}else{
console
.log('Object doesn't exist ');
}
};
getRequest
.onerror = function(e){
console
.log('Error [deleteItem] -> delete('+id+') ');
};
} catch (e) {
console
.log('Could not operate on '+ '['+store+']' +'. You may have to create the entry first ');
}
} catch(e){
console
.log('Error ');
}
};
dbOpenRequest
.onupgradeneeded = function(e) {
console
.log('Database upgrade needed ');
//var db = dbOpenRequest.result;
//var transaction = dbOpenRequest.transaction;
};
dbOpenRequest
.onerror = function(e){
console
.log('Error ');
};
dbOpenRequest
.onblocked = function(e) {
console
.log('Database open request is blocked ');
};
} catch(e){
console
.log('Error ');
}
};

var insertItems = function(store,obj,length){
//inserts the same item multiple times in the same transaction.
//just a test
try {
//request to open the database
var dbOpenRequest = indexedDB.open(dbName);
dbOpenRequest
.onsuccess = function(event) {
var thisDB = dbOpenRequest.result;
thisDB
.onversionchange = function(e) {
console
.log('Version change triggered, so closing database connection ' + e.oldVersion + ' ' + e.newVersion + ' ' +thisDB);
thisDB
.close();
};
try{
//creating transaction to open an object store in "readwrite" mode
var transaction = thisDB.transaction([store], 'readwrite');
transaction
.oncomplete = function(e) {
console
.log('transaction is complete ');
};
transaction
.onabort = function(e) {
var error = e.target.error; // DOMError
console
.log('transaction aborted ' + error);
};
transaction
.onerror = function(e) {
console
.log('transaction error ');
};

try {
var request;
var objectStore = transaction.objectStore(store);

putNext
();

function putNext() {
if