IndexedDB functions for when localstorage is too small

Thought I’d put this here for any who might find it helpful.

'This is code largly taken from AS IndexedDB example file
'I use this as a replacement for localstorage when data > localstorage limit
'How to use...
'Call StorageOpenDB() This opens/creates an IndexedDB store with a key column call "keyname"
'To save an object use StorageAddKey(object). This will fail if key exists and go on to StorageUpdateKey(object)
'object must have an element {"keyname":"yourkey",...your data} in it eg {"keyname":"clientkey", "clientname":"dude", "phone":"0123456789"}
'To get your data back call StorageReturnKey(yourkey). These functions are all Asynchronous so go on to your functions in the StorageReturnKey success function.
'DeleteKey and ReturnAll are self explanatory
'This is Javascript code but can be pasted into Basic projects with the Javascript/End Javascript tags included

JavaScript
var db = null;
var DBNAME = "appdata_db";
var DBVER = 1;

function StorageOpenDB() {
    var request = indexedDB.open(DBNAME, DBVER);
    request.onupgradeneeded = function(e) {
        console.log("Upgrading...");
        var thisDB = e.target.result;
        var store = null;
        if (!thisDB.objectStoreNames.contains("storage")) {
            store = thisDB.createObjectStore("storage", {
                keyPath: "keyname"
            });
        }
    };
    request.onsuccess = function(e) {
        db = e.target.result;
        console.log("openDB success!");
        StorageReturnKey("deviceid");
    };
    request.onerror = function(e) {
        console.log("openDB error");
    };
}

function StorageAddKey(o) {
    var tx = db.transaction(["storage"], "readwrite");
    var store = tx.objectStore("storage");
    // add to store
    var request = store.add(o);
    request.onsuccess = function(e) {
        console.log("Added successfully! storage=" + JSON.stringify(o));
    };
    request.onerror = function(e) {
        console.log("Add error", e.target.error.name);
        console.log("o.keyname:" + o.keyname)
        StorageUpdateKey(o);
    };
}

function StorageReturnKey(key) {
    if (key == undefined) {
      console.log("StorageReturnKey undefined");
    };
    console.log("StorageReturnKey key:" & key);
    SaveKey = key;
    if (SaveKey == "setupdevice") {
      key = "deviceid";
    };
    var tx = db.transaction(["storage"], "readonly");
    var store = tx.objectStore("storage");
    var request = store.get(key);
    request.onsuccess = function(e) {
        console.log("StorageReturnKey SUCCESS: " & JSON.stringify(e.target.result));
		//go on to functions with the loaded data
        if (SaveKey == "deviceid") {
          CheckDeviceActiveStatus(e.target.result);
          return;
        } else if (SaveKey == "LastSaveMission") {
          MissionData = e.target.result;
          MissionMain();
          return;
        };
    };
    request.onerror = function(e) {
        console.log("StorageReturnKey ERROR");
        if (SaveKey == "deviceid") {
          CheckDeviceActiveStatus("fail");
          return;
        } else if (ssRow == "LastSaveMission") {
          console.log("Failed to load local saved mission from IndexedDB");
          SearchMissionFiles();
        };
    };
}

function StorageUpdateKey(o) {
    var tx = db.transaction(["storage"], "readwrite");
    var store = tx.objectStore("storage");
    store.get(o.keyname).onsuccess = function(e) {
        console.log("store.get:" + o.keyname);
        var data = e.target.result;
        if (!data) {
            console.log("nothing matched.");
            return;
        }
        console.log("data:" + JSON.stringify(data));
        data = o;
        var request = store.put(data);
        request.onsuccess = function(e) {
            console.log("put success!");
        };
        request.onerror = function(e) {
            console.log("put error!");
        };
    };
}

function StorageReturnAll() {
    var tx = db.transaction(["storage"], "readonly");
    var objectStore = tx.objectStore("storage");
    var cursor = objectStore.openCursor();
    cursor.onsuccess = function(e) {
        var res = e.target.result;
        if (res) {
            console.log("key: " + res.key);
            console.log(JSON.stringify(res.value));
            dataresult.push(res.value);
            console.log("dataresult:" + JSON.stringify(dataresult));
            res.continue();
        }
        Textarea1.value = JSON.stringify(dataresult,null,'\t');
    };
}

function StorageDeleteKey(key) {
    var tx = db.transaction(["storage"], "readwrite");
    var store = tx.objectStore("storage");
    var request = store.delete(key);
    //var request = store.clear(); // delete all from the store
    request.onsuccess = function(e) {
        // calls even when nothing to remove.
        console.log("removeByKey success!");
    };
    request.onerror = function(e) {
        console.log("removeByKey error!");
    };
}

function StorageDeleteDB() {
  console.log("StorageDeleteDB()");
    var request = indexedDB.deleteDatabase(DBNAME);
    request.onsuccess = function(e) {
        console.log("deleteDB success!");
    };
    request.onerror = function(e) {
        console.log("deleteDB error!");
    };
}
End JavaScript

Thank you for that code! Could be very useful.
With the current issues about using SQLite on IOS and new versions of Chrome (especially for web apps) do you know offhand about the storage capacity of IndexedDB versus LocalStorage on IOS, Android?

(Not to worry if it’s not info you have to hand, I’m sure i can research it online at some point soon!)
Thanks again,
Neil

80% of device storage capacity seems to be the limit in chrome

This can actually vary widely by device and browser, but in all cases it’s going to be more than LocalStorage except on extremely space limited devices - at least 10% of available storage, and likely much more.