Skip to content

Commit

Permalink
added try catch for upgrade
Browse files Browse the repository at this point in the history
  • Loading branch information
geforcesong committed Dec 1, 2020
1 parent 09814a8 commit 8b60fac
Showing 1 changed file with 19 additions and 8 deletions.
27 changes: 19 additions & 8 deletions statics/indexeddb/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ class IndexDBHelper {
constructor() {
this.dbName = 'MyNewSite';
this.storeName = 'SitesData';
this.version = 8;
this.version = 12;
this.database = null;
}

Expand All @@ -26,10 +26,18 @@ class IndexDBHelper {
};
request.onupgradeneeded = function (event) {
var db = event.target.result;
// Create an objectStore to hold information about our customers. We're
// going to use "ssn" as our key path because it's guaranteed to be
// unique - or at least that's what I was told during the kickoff meeting.
db.createObjectStore(self.storeName, { keyPath: "key" });
try {
const s = request.transaction.objectStore(self.storeName);
} catch {
db.createObjectStore(self.storeName, { keyPath: "key" });
}
db.onversionchange = function (event) {
db.close();
console.log("A new version of this page is ready. Please reload or close this tab!");
};
};
request.onblocked = function (event) {
console.log("Please close all other tabs with this site open!");
};
});
}
Expand Down Expand Up @@ -68,6 +76,9 @@ class IndexDBHelper {
}

async delete(key) {
if (!this.database) {
throw new Error('Database is not init successfully');
}
const tx = this.database.transaction([this.storeName], 'readwrite');
const store = tx.objectStore(this.storeName);
store.delete(key);
Expand Down Expand Up @@ -116,16 +127,16 @@ btnSave.addEventListener('click', async function (el) {

await IndexDBHelper.getInstance().add({
key: 'hotleadCount',
value: 301
value: 12301
});
await IndexDBHelper.getInstance().add({
key: 'visitedCount',
value: 302
value: 9302
});

await IndexDBHelper.getInstance().add({
key: 'visitedCount1',
value: 303
value: 9303
});

const data = await IndexDBHelper.getInstance().get('hotleadCount');
Expand Down

0 comments on commit 8b60fac

Please sign in to comment.