JSFiddle - React, Tailwind, and code Playground

by kwikwag

JavaScript

window.indexedDB = window.indexedDB || window.mozIndexedDB || window.webkitIndexedDB;
window.IDBTransaction = window.IDBTransaction || window.webkitIDBTransaction;
window.IDBKeyRange = window.IDBKeyRange || window.webkitIDBKeyRange;

var DB_VER = 1.03;
try {
    window.indexedDB.deleteDatabase('test');
} catch (e) {
    console.log(e);
}
var req = window.indexedDB.open('test', DB_VER);

var db;


function upg() {
    if (isNaN(parseFloat(db.version)) || (db.version < 1)) {
        db.createObjectStore('test', {
            keyPath: 'id'
        });
    }
    console.log('upg done');
}

function errhandler(s) {
    return function() {
        console.log('#err# ' + s);
        if (db) {
            try {
                db.close();
            } catch (e) {}
        }
        db = null;
    }
}

function ready() {
    console.log('ready!');
    var trans = db.transaction(['test'], window.IDBTransaction.READ_WRITE);
    var req = trans.objectStore('test').put({id: 1, a:"one"});
    console.log('after write req');
    req.onsuccess = function() {
        var trans = db.transaction(['test'], window.IDBTransaction.READ_ONLY);
        console.log('before read req');
        var req = trans.objectStore('test').get(1);
        console.log('after read req');
        req.onsuccess = function() {
            console.log(this.result);
        };
        
    };   
}
req.onupgradeneeded = function() {
    db = this.result;
    console.log('upg (std)', db);
    upg(db);
};
req.onsuccess = function() {
    db = this.result;
    console.log('open', db);
    if (db.version != DB_VER) {
        console.log('upg (depr)');
        var req = db.setVersion(DB_VER);
        req.onsuccess = function(e) {
            upg();
            this.result.oncomplete = ready;
        };
        req.onerror = errhandler('setVersion err');
        req.onblock = errhandler('setVersion block');
    }
    else {
        ready();
    }

    db.onversionchange = function(e) {
        console.log('ver...