From 3a8eea6f6a4ceed44d01df1fd82939f61bbd5431 Mon Sep 17 00:00:00 2001 From: Deven Gangwani Date: Thu, 14 Mar 2024 14:50:10 +0530 Subject: [PATCH] Change IDB to version 3 to force newer structure --- components/data_worker.tsx | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/components/data_worker.tsx b/components/data_worker.tsx index 2f04785..da4c57b 100644 --- a/components/data_worker.tsx +++ b/components/data_worker.tsx @@ -111,7 +111,7 @@ async function start_IDB() { //if this resolves, the global variable 'db' should return new Promise((resolve, reject) => { db = ""; try { - const openRequest = indexedDB.open("students", 2); + const openRequest = indexedDB.open("students", 3); openRequest.addEventListener("error", (error) => { // console.error("Failed to access local database."); // console.log(error); @@ -123,9 +123,17 @@ async function start_IDB() { //if this resolves, the global variable 'db' should resolve("Success"); }, {once: true}); openRequest.addEventListener("upgradeneeded", (event) => { + db = event.target?.result; + //delete the previous db because I am a bad programmer and I am using upgradeneeded to fix issues + //try/catch so that if it isn't there, it doesn't stop the whole thing + try { + db.deleteObjectStore("students"); + console.log("Deleted old table"); + } catch (err) { + console.error("Error in deleting students db on version change", err); + } //set up the DB, and if nothing goes wrong (i.e. no errors) then resolve successfully // console.log("Setting up IDB"); - db = event.target?.result; const objStore = db.createObjectStore("students", {keyPath:"key", autoIncrement: false}); objStore.createIndex("students", "students", {unique: false}) //this will hold the array/json string of the response // console.log("Finished setting up IDB");