assuncaocharles/react-indexed-db

initDB() doesn't (re)create missing objectStore(s)

Closed this issue · 2 comments

zzynx commented

In my PWA I use initDB() to create an indexedDB + one objectStore.

Once, for some reason, I had to clear my browser (Chrome & Firefox) data and as a result I had the situation that I had my indexedDB WITHOUT its objectStore.
I expected initDB() to correct that situation, but apparently initDB() does NOT recreate the objectStore for me. (diving into the code, I saw that it's because in the above mentioned situation onupgradeneeded() in your method CreateObjectStore is not triggered. Since the indexedDB is still there.)

I can't ask from the (non-technical) user (of my PWA) to manually delete that indexedDB himself and restart the PWA (which is a working workaround), so I wrote my own check method before calling initDB(). In that method I check whether the indexedDB exists AND has the expected objectStore. If not, I simply delete the indexedDB. Then the subsequent initDB() call recreates the complete indexedDB including the requested objectStore(s).

I think it would be great if that was incorporated in the onsuccess method of CreateObjectStore.

Hi @zzynx Thanks for the feedback, you seem to know pretty much the solution so PRs are always welcomed :)

Hi @zzynx

Clearing the browser data should delete the db, therefore triggering the initDB next time. If it doesn't work for you, please provide a small reproduction example with steps.