Difference between revisions of "User:Abel/experimental.js"
Jump to navigation
Jump to search
m |
m |
||
Line 72: | Line 72: | ||
db.close(); | db.close(); | ||
}; | }; | ||
− | } | + | }; |
+ | }; |
Revision as of 03:48, 25 April 2019
console.log("~");
var PersonalDataDataBaseName = "pdio";
var Pete = {id: 11345, name: {first: "Pete", last: "Rock"}, age: 52};
addRecord(PersonalDataDataBaseName, Pete);
recordUpdate(PersonalDataDataBaseName, ["Pete", "Rock"], 'phone', '+313555777');
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 store = db.createObjectStore("MyObjectStore", {keyPath: "id"});
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.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");
var getRecod = 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
tx.oncomplete = function() {
db.close();
};
};
};