summaryrefslogtreecommitdiff
path: root/common/indexeddb.js
diff options
context:
space:
mode:
Diffstat (limited to 'common/indexeddb.js')
-rw-r--r--common/indexeddb.js43
1 files changed, 32 insertions, 11 deletions
diff --git a/common/indexeddb.js b/common/indexeddb.js
index bd87399..d83e605 100644
--- a/common/indexeddb.js
+++ b/common/indexeddb.js
@@ -53,11 +53,13 @@ let initial_data = (
);
/* Update when changes are made to database schema. Must have 3 elements */
-const db_version = [1, 0, 0];
+const db_version = [1, 0, 1];
const nr_reductor = ([i, s], num) => [i - 1, s + num * 1024 ** i];
const version_nr = ver => ver.slice(0, 3).reduce(nr_reductor, [2, 0])[1];
+let db_version_nr = version_nr(db_version);
+
const stores = [
["file", {keyPath: "sha256"}],
["file_uses", {keyPath: "sha256"}],
@@ -100,19 +102,38 @@ async function idb_del(transaction, store_name, key)
}
async function perform_upgrade(event) {
+ if (event.oldVersion > db_version_nr)
+ throw "bad db version: " + event.oldVersion;
+
const opened_db = event.target.result;
+ const transaction = event.target.transaction;
- /* When we move to a new database schema, we will add upgrade logic here. */
- if (event.oldVersion > 0)
- throw "bad db version: " + event.oldVersion;
+ if (event.oldVersion == 0) {
+ for (const [store_name, key_mode] of stores)
+ opened_db.createObjectStore(store_name, key_mode);
+ }
- let store;
- for (const [store_name, key_mode] of stores)
- store = opened_db.createObjectStore(store_name, key_mode);
+ if (event.oldVersion > 0 && event.oldVersion < db_version_nr) {
+ const v1_url = "https://hydrilla.koszko.org/api_v1/";
+ const v1_entry = await idb_get(transaction, "repo", v1_url);
- const ctx = make_context(store.transaction, initial_data.file);
- await _save_items(initial_data.resource, initial_data.mapping,
- initial_data.repo || [], ctx);
+ if (v1_entry) {
+ const v2_url = "https://hydrilla.koszko.org/api_v2/";
+
+ await idb_del(transaction, "repo", v1_url);
+ await idb_put(transaction, "repo", {url: v2_url});
+ }
+ }
+
+ if (event.oldVersion == 0) {
+ const ctx = make_context(transaction, initial_data.file);
+ await _save_items(initial_data.resource, initial_data.mapping,
+ initial_data.repo || [], ctx);
+ } else {
+ await new Promise(
+ (...cbs) => [transaction.onsuccess, transaction.onerror] = cbs
+ );
+ }
return opened_db;
}
@@ -125,7 +146,7 @@ async function get_db() {
let resolve, reject;
const waiter = new Promise((...cbs) => [resolve, reject] = cbs);
- const request = indexedDB.open("haketilo", version_nr(db_version));
+ const request = indexedDB.open("haketilo", db_version_nr);
request.onsuccess = ev => resolve(ev.target.result);
request.onerror = ev => reject("db error: " + ev.target.errorCode);
request.onupgradeneeded = ev => perform_upgrade(ev).then(resolve, reject);