| Line 1: |
Line 1: |
| | console.log("~"); | | console.log("~"); |
| | | | |
| − | var pddb = window.indexedDB.open("pdio", 1); | + | var PersonalDataDataBaseName = "pdio"; |
| | + | var Pete = {id: 11345, name: {first: "Pete", last: "Rock"}, age: 52}; |
| | | | |
| − | pddb.addEventListener('error', (event) => {
| + | addRecord(PersonalDataDataBaseName, Pete); |
| − | console.log('Request error:', pddb.error);
| + | recordUpdate(PersonalDataDataBaseName, ["Pete", "Rock"], 'phone', '+313555777'); |
| − | }, false);
| |
| | | | |
| − | pddb.onupgradeneeded = function() { | + | function addrecord(pddbname, record){ |
| | + | var pddb = window.indexedDB.open(pddbname, 1); |
| | + | |
| | + | pddb.addEventListener('error', (event) => { |
| | + | console.log('Request error:', pddb.error); |
| | + | }, false); |
| | + | |
| | + | 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() { |
| | + | store.put(record); |
| | + | } |
| | + | |
| | + | tx.oncomplete = function() { |
| | + | db.close(); |
| | + | }; |
| | + | } |
| | + | |
| | + | function recordUpdate(pddbname, record, field, value){ |
| | + | |
| | + | var pddb = window.indexedDB.open(pddbname, 1); |
| | + | |
| | + | pddb.addEventListener('error', (event) => { |
| | + | console.log('Request error:', pddb.error); |
| | + | }, false); |
| | | | |
| − | pddb.onsuccess = function() { | + | pddb.onupgradeneeded = function() { |
| − | // Start a new transaction
| |
| | var db = pddb.result; | | var db = pddb.result; |
| − | var tx = db.transaction("MyObjectStore", "readwrite"); | + | var store = db.createObjectStore("MyObjectStore", {keyPath: "id"}); |
| − | var store = tx.objectStore("MyObjectStore");
| + | var index = store.createIndex("NameIndex", ["name.last", "name.first"]); |
| − | var index = store.index("NameIndex");
| + | }; |
| | + | |
| | + | 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 getRecod = index.get(record); |
| − | // var John = {id: 12345, name: {first: "John", last: "Doe"}, age: 42, phone: '+33123456789'};
| + | getRecord.onsuccess = function() { |
| − | // var Bob = {id: 67890, name: {first: "Bob", last: "Smith"}, age: 35}
| + | console.log("updating"); |
| − | // store.put(John);
| + | getRecord.result.field = value; // => "Bob" |
| − | // store.put(Bob);
| + | store.put(getRecord.result); |
| − | var Pete = {id: 11345, name: {first: "Pete", last: "Rock"}, age: 52};
| |
| − | store.put(Pete);
| |
| − | recordUpdate(store, index, ["Pete", "Rock"], 'phoneNumber', '+674316');
| |
| − | // Query the data
| |
| − |
| |
| − | // Close the db when the transaction is done
| |
| − | tx.oncomplete = function() {
| |
| − | db.close();
| |
| − | };
| |
| − | }
| |
| | | | |
| − | function recordUpdate(db, ref, record, field, value){ | + | getRecordAgain = index.get(record); |
| − | var index = db.index("NameIndex");
| + | getRecordAgain.onsuccess = function() { |
| − | var getRecord = index.get(record);
| + | console.log(getRecordAgain.result.field); // => "Bob" |
| | + | if ( getRecordAgain.result.field == value ) { |
| | + | console.log("update succeeded"); |
| | + | } else { |
| | + | console.log("update unsuccessful"); |
| | + | } |
| | + | }; |
| | + | }; |
| | | | |
| − | getRecord.onsuccess = function() {
| |
| − | console.log("updating");
| |
| − | getRecord.result.field = value; // => "Bob"
| |
| − | db.put(getRecord.result);
| |
| − | checkrecord(db, ref, record, field)
| |
| − | };
| |
| − | }
| |
| | | | |
| − | function checkRecord(db, ref, record, field) {
| + | // Close the db when the transaction is done |
| − | checkRecord = ref.get(record);
| + | tx.oncomplete = function() { |
| − | checkRecord.onsuccess = function() {
| + | db.close(); |
| − | console.log(getRecordAgain.result.phone); // => "Bob"
| + | }; |
| − | };
| + | } |
| − | } | |