Difference between revisions of "User:Abel/experimental.js"
Jump to navigation
Jump to search
m |
m |
||
Line 5: | Line 5: | ||
addRecord(PersonalDataDataBaseName, Pete); | addRecord(PersonalDataDataBaseName, Pete); | ||
− | recordUpdate(PersonalDataDataBaseName, | + | recordUpdate(PersonalDataDataBaseName, "Pete", "Rock", 'phone', '+313555777'); |
function addRecord(pddbname, record){ | function addRecord(pddbname, record){ | ||
Line 27: | Line 27: | ||
store.put(record); | store.put(record); | ||
+ | |||
+ | var recordcheck = index.get([record.name.first, record.name.second]); | ||
+ | recordcheck.onsuccess = function() { | ||
+ | console.log(recordcheck); | ||
+ | } | ||
tx.oncomplete = function() { | tx.oncomplete = function() { | ||
Line 34: | Line 39: | ||
}; | }; | ||
− | function recordUpdate(pddbname, | + | function recordUpdate(pddbname, recordA, recordB, field, value){ |
var pddb = window.indexedDB.open(pddbname, 1); | var pddb = window.indexedDB.open(pddbname, 1); | ||
Line 55: | Line 60: | ||
var index = store.index("NameIndex"); | var index = store.index("NameIndex"); | ||
− | var getRecord = index.get( | + | var getRecord = index.get([recordA, recordB]); |
getRecord.onsuccess = function() { | getRecord.onsuccess = function() { | ||
console.log("updating"); | console.log("updating"); | ||
Line 61: | Line 66: | ||
store.put(getRecord.result); | store.put(getRecord.result); | ||
− | getRecordAgain = index.get( | + | getRecordAgain = index.get([recordA, recordB]); |
getRecordAgain.onsuccess = function() { | getRecordAgain.onsuccess = function() { | ||
console.log(getRecordAgain.result.field); // => "Bob" | console.log(getRecordAgain.result.field); // => "Bob" |
Revision as of 03:59, 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() {
var db = pddb.result;
var tx = db.transaction("MyObjectStore", "readwrite");
var store = tx.objectStore("MyObjectStore");
var index = store.index("NameIndex");
store.put(record);
var recordcheck = index.get([record.name.first, record.name.second]);
recordcheck.onsuccess = function() {
console.log(recordcheck);
}
tx.oncomplete = function() {
db.close();
};
};
};
function recordUpdate(pddbname, recordA, recordB, 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 getRecord = index.get([recordA, recordB]);
getRecord.onsuccess = function() {
console.log("updating");
getRecord.result.field = value; // => "Bob"
store.put(getRecord.result);
getRecordAgain = index.get([recordA, recordB]);
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();
};
};
};