Created
          July 2, 2020 10:21 
        
      - 
      
 - 
        
Save rozeappletree/c04232c34a0602e39f45d8398370fc39 to your computer and use it in GitHub Desktop.  
    Boilerplate
  
        
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | // Load IndexedDB | |
| window.indexedDB = window.indexedDB || window.mozIndexedDB || window.webkitIndexedDB || window.msIndexedDB; | |
| window.tx = window.IDBTransaction || window.webkitIDBTransaction || window.msIDBTransaction; | |
| window.IDBKeyRange = window.IDBKeyRange || window.webkitIDBKeyRange || window.msIDBKeyRange | |
| if (!window.indexedDB) { | |
| window.alert("Your browser doesn't support a stable version of IndexedDB.") | |
| } | |
| // Operations | |
| function operation(type){ | |
| const dbName = "myDB"; | |
| var request = indexedDB.open(dbName, 3), db, tx, store, index; // globals | |
| // doesn't execute while retreiving data | |
| request.onupgradeneeded = function(e) { | |
| let db = request.result, | |
| store = db.createObjectStore('logStore', {keyPath: 'id'}), | |
| index = store.createIndex('date', 'date', {unique: true}); /* ('logStore', {autoIncrement: true}) */ | |
| }; | |
| request.onerror = function(e) { | |
| console.log("ERROR: ", e.target.errorCode) | |
| }; | |
| request.onsuccess = function(e) { | |
| db = request.result; | |
| tx = db.transaction('logStore', 'readwrite'); | |
| store = tx.objectStore('logStore'); | |
| dateIndex = store.index('date') | |
| db.onerror = function(e){ | |
| console.log("DB ERROR: ", e.target.errorCode) | |
| } | |
| if (type=='putData'){ | |
| _putData(); | |
| } else if (type == 'getData'){ | |
| _getData(); | |
| } | |
| tx.oncomplete = function(){ | |
| db.close() | |
| } | |
| } | |
| function _putData(){ | |
| // put in info | |
| store.put({ | |
| id : 1, | |
| date : '21 Mar 2020 - 6pm', | |
| head : 'head1', | |
| log : 'log1' | |
| }); | |
| store.put({ | |
| id : 2, | |
| date : '22 Mar 2020 - 7pm', | |
| head : 'head2xx', | |
| log : 'log2' | |
| }); | |
| } | |
| function _getData(){ | |
| // get data | |
| let atId1 = store.get(1); // key i.e id = 1 | |
| let atDateIdx = dateIndex.get('22 Mar 2020 - 7pm'); | |
| // as asynchronous, use | |
| atId1.onsuccess = function(){ | |
| console.log(atId1.result); | |
| console.log(atId1.result.head, atId1.result.log); | |
| }; | |
| atDateIdx.onsuccess = function(){ | |
| console.log(atDateIdx.result); | |
| console.log(atDateIdx.result.head, atId1.result.log); | |
| }; | |
| } | |
| } | |
| // MAIN | |
| // ---- | |
| // comment one of two | |
| // 1. | |
| //operation(type="putData") | |
| // 2. After put data / if existing db | |
| operation(type="getData") | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment
  
            
list all databases
deleteDatabase
list all object stores
delete object store
Note: These are just APIs. Got these info by checking data-members and methods in console for corresponding object.
(__proto__)