Changes

Jump to navigation Jump to search
m
no edit summary
Line 55: Line 55:  
*/
 
*/
   −
var PersonalDataDataBaseName = "pdio";
+
console.log("~");
var Pete = {id: 11345, name: {first: "Pete", last: "Rock"}, age: 52};
     −
addRecord(PersonalDataDataBaseName, Pete);
+
var pddb = window.indexedDB.open("pdio", 1);
recordUpdate(PersonalDataDataBaseName, ["Pete", "Rock"], 'phone', '+313555777');
     −
function addRecord(pddbname, record){
+
pddb.addEventListener('error', (event) => {
   var pddb = window.indexedDB.open(pddbname, 1);
+
   console.log('Request error:', pddb.error);
 +
}, false);
   −
  pddb.onupgradeneeded = function() {
+
pddb.onupgradeneeded = function() {
 
     var db = pddb.result;
 
     var db = pddb.result;
 
     var store = db.createObjectStore("MyObjectStore", {keyPath: "id"});
 
     var store = db.createObjectStore("MyObjectStore", {keyPath: "id"});
 
     var index = store.createIndex("NameIndex", ["name.last", "name.first"]);
 
     var index = store.createIndex("NameIndex", ["name.last", "name.first"]);
  };
+
};
   −
  pddb.onsuccess = function() {
+
pddb.onsuccess = function() {
 +
    // Start a new transaction
 
     var db = pddb.result;
 
     var db = pddb.result;
 
     var tx = db.transaction("MyObjectStore", "readwrite");
 
     var tx = db.transaction("MyObjectStore", "readwrite");
 
     var store = tx.objectStore("MyObjectStore");
 
     var store = tx.objectStore("MyObjectStore");
     var index = store.index("NameIndex");
+
     var index = store.index("NameIndex");
 
  −
    store.put(record);
  −
 
  −
    tx.oncomplete = function() {
  −
      db.close();
  −
    }; 
  −
  };
  −
};
  −
 
  −
function recordUpdate(pddbname, record, field, value){
     −
  var pddb = window.indexedDB.open(pddbname, 1);
+
    // 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);
 +
   
   −
  pddb.addEventListener('error', (event) => {
+
    // Query the data
     console.log('Request error:', pddb.error);
+
    var getJohn = store.get(12345);
  }, false);
+
     var getBob = index.get(["Smith", "Bob"]);
   −
  pddb.onupgradeneeded = function() {
+
    getBob.onsuccess = function() {
    var db = pddb.result;
+
      console.log("updating");
    var store = db.createObjectStore("MyObjectStore", {keyPath: "id"});
+
      getBob.result.phone = '+312';  // => "Bob"
    var index = store.createIndex("NameIndex", ["name.last", "name.first"]);
+
      store.put(getBob.result);
  };
     −
  pddb.onsuccess = function() {
+
      getBobAgain = index.get(["Smith", "Bob"]);
        // Start a new transaction
+
          getBobAgain.onsuccess = function() {
        var db = pddb.result;
+
            console.log(getBobAgain.result.phone);   // => "Bob"
        var tx = db.transaction("MyObjectStore", "readwrite");
+
          };
        var store = tx.objectStore("MyObjectStore");
+
 
        var index = store.index("NameIndex");
+
};
 +
   
   −
        var getRecord = index.get(record);
  −
        getRecord.onsuccess = function() {
  −
          console.log("updating");
  −
            getRecord.result.field = value;  // => "Bob"
  −
            store.put(getRecord.result);
     −
            getRecordAgain = index.get(record);
  −
            getRecordAgain.onsuccess = function() {
  −
                console.log(getRecordAgain.result.field);  // => "Bob"
  −
                if ( getRecordAgain.result.field == value ) {
  −
                  console.log("update succeeded");
  −
                } else {
  −
                  console.log("update unsuccessful");
  −
                }
  −
              }; 
  −
            };
     −
 
+
    // Close the db when the transaction is done
      // Close the db when the transaction is done
+
    tx.oncomplete = function() {
      tx.oncomplete = function() {
   
         db.close();
 
         db.close();
      };
   
     };
 
     };
  };
+
}
530

edits

Navigation menu