User:Abel/experimental.js

From Wikibase Personal data
< User:Abel
Revision as of 02:53, 25 April 2019 by Abel (talk | contribs)
Jump to navigation Jump to search

Note: After saving, you may have to bypass your browser's cache to see the changes.

  • Firefox / Safari: Hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (⌘-R on a Mac)
  • Google Chrome: Press Ctrl-Shift-R (⌘-Shift-R on a Mac)
  • Internet Explorer / Edge: Hold Ctrl while clicking Refresh, or press Ctrl-F5
  • Opera: Go to Menu → Settings (Opera → Preferences on a Mac) and then to Privacy & security → Clear browsing data → Cached images and files.
console.log("~");
console.log("~");

var pddb = window.indexedDB.open("pdio", 1);

pddb.addEventListener('error', (event) => {
  console.log('Request error:', pddb.error);
}, false);

pddb.onupgradeneeded = function() {
    var db = pddb.result;
    var store = db.createObjectStore("MyObjectStore", {keyPath: "id"});
    var index = store.createIndex("NameIndex", ["name.last", "name.first"]);
};

pddb.onsuccess = function() {
    // Start a new transaction
    var db = pddb.result;
    var tx = db.transaction("MyObjectStore", "readwrite");
    var store = tx.objectStore("MyObjectStore");
    var index = store.index("NameIndex");

    // Add some data
    // var John = {id: 12345, name: {first: "John", last: "Doe"}, age: 42, phone: '+33123456789'};
    // var Bob = {id: 67890, name: {first: "Bob", last: "Smith"}, age: 35}
    // store.put(John);
    // store.put(Bob);
    

    // Query the data
    var getJohn = store.get(12345);
    var getBob = index.get(["Smith", "Bob"]);

    getBob.onsuccess = function() {
       console.log("updating");
       getBob.result.phone = '+312';   // => "Bob"
       store.put(getBob.result);

       getBobAgain = index.get(["Smith", "Bob"]);
          getBobAgain.onsuccess = function() {
             console.log(getBobAgain.result.phone);   // => "Bob"
          };
   
 };
    



    // Close the db when the transaction is done
    tx.oncomplete = function() {
        db.close();
    };
}