In javascript, I have a class here to handle requests. I want to make sure I have all the error handling set up ok. I want to open the connection to the db and close it on each request. I have this code so far
export default class IndexedDBStorage {
#name:string;
constructor(name:string) {
this.#name = name;
}
private async GetDB():Promise<IDBDatabase> {
return new Promise((resolve, reject) => {
// https://javascript.info/indexeddb
const request = window.indexedDB.open(this.#name, 1);
request.onupgradeneeded = () => {
//this.#db = request.result;
if (!request.result.objectStoreNames.contains(this.#name)) {
request.result.createObjectStore(this.#name, {keyPath: 'id', autoIncrement:true});
}
};
request.onerror = () => {
reject("Why didn't you allow my web app to use IndexedDB?! " + request.error?.code);
};
request.onsuccess = () => {
//request.result.onerror = () => {
// Generic error handler for all errors targeted at this database's requests!
// reject("Database error: " + request.error?.code);
//};
resolve(request.result);
};
});
}
async GetAllItems<T>():Promise<Array<T>> {
return new Promise((resolve, reject) => {
this.GetDB().then(db => {
const transaction = db.transaction(this.#name);
const store = transaction.objectStore(this.#name);
const datarequest = store.getAll();
db.close();
datarequest.onsuccess = function() {
resolve(datarequest.result);
};
datarequest.onerror = function() {
reject("Error " + datarequest.error);
};
}).catch(err => {
console.error(err);
});
});
}
}
Also wanted to know, what is the difference between this part inside the GetDB
function. This is the onerror event of the db itself, which is setup in the onsuccess event.
//request.result.onerror = () => {
// Generic error handler for all errors targeted at this database's requests!
// reject("Database error: " + request.error?.code);
//};
And this part
datarequest.onerror = function() {
reject("Error " + datarequest.error);
};
?
Thanks
EDIT:
export default class IndexedDBStorage {
#name:string;
constructor(name:string) {
this.#name = name;
}
private async GetDB():Promise<IDBDatabase> {
return new Promise((resolve, reject) => {
const request = window.indexedDB.open(this.#name, 1);
request.onerror = (event: Event) => {
// this function catches all errors
// all errors in database, transaction or request levels should be caught and handled there
const errorEvent = event as Event & {error:string};
console.error(errorEvent.error);
alert(errorEvent.error);
reject(errorEvent.error);
};
request.onblocked = function () {
// this event shouldn't trigger if we handle onversionchange correctly
// it means that there's another open connection to the same database
// and it wasn't closed after db.onversionchange triggered for it
const message = 'Database is currently blocked, page needs to reload, click ok to reload.';
alert(message);
window.location.reload();
//console.log(message);
//reject(message);
};
request.onupgradeneeded = (event: IDBVersionChangeEvent) => {
const db = request.result;
console.log('idb onupgradeneeded firing');
switch(event.oldVersion) { // existing db version
case 0:
// version 0 means that the client had no database
// perform initialization
console.log('Database currently at version 0 which means that the client had no database');
console.log(`Upgrading to version ${db.version}`);
request.result.createObjectStore(this.#name, {keyPath: 'id', autoIncrement:true});
case 1:
// client had version 1
// update
console.log('Database currently at version 1 which means that the database already exists');
}
};
request.onsuccess = () => {
const db = request.result;
db.onversionchange = function() {
db.close();
const message = "Database is outdated, page needs to reload, click ok to reload.";
alert(message);
window.location.reload();
};
resolve(db);
};
});
}
async GetAllItems<T>():Promise<Array<T>> {
return new Promise((resolve, reject) => {
this.GetDB().then(db => {
const transaction = db.transaction(this.#name);
const store = transaction.objectStore(this.#name);
const datarequest = store.getAll();
db.close();
datarequest.onsuccess = function() {
resolve(datarequest.result);
};
}).catch(err => {
reject(err);
});
});
}
}