Line 52: |
Line 52: |
| return read.split('.').reduce(indexcheck, array); | | return read.split('.').reduce(indexcheck, array); |
| } | | } |
− | */
| |
| | | |
− | //|||||||||||||||||||||[ db objects ]|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| |
− |
| |
| | | |
− | var personal = {};
| + | var PersonalDataDataBaseName = "pdio"; |
− | var indexedDB = window.indexedDB ;
| + | var Pete = {id: 11345, name: {first: "Pete", last: "Rock"}, age: 52}; |
− | var indexedDBVersion = "2";
| |
− | var Pete = {id: '11345', name: {first: "Pete", last: "Rock"}, age: '52'};
| |
| | | |
− | personal.indexedDB = {};
| + | addRecord(PersonalDataDataBaseName, Pete); |
− | personal.indexedDB.db = null;
| + | recordUpdate(PersonalDataDataBaseName, Pete, 'phone', '+313555777'); |
− |
| |
| | | |
− | personal.indexedDB.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("personalDataIO", {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"]); |
− | }; | + | }; |
− | //|||||||||||||||||||||||||||[ Error ]|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| + | |
− |
| + | pddb.onsuccess = function() { |
− | | + | var db = pddb.result; |
− | personal.indexedDB.onerror = function(arr){ | + | var tx = db.transaction("MyObjectStore", "readwrite"); |
− | console.log(arr);
| + | var store = tx.objectStore("MyObjectStore"); |
| + | var index = store.index("NameIndex"); |
| + | |
| + | store.put(record); |
| + | |
| + | var recordcheck = index.get([record.name.first, record.name.last]); |
| + | recordcheck.onsuccess = function() { |
| + | console.log(recordcheck); |
| } | | } |
| | | |
| + | tx.oncomplete = function() { |
| + | db.close(); |
| + | }; |
| + | }; |
| + | }; |
| | | |
− | //|||||||||||||||||||||||||||[ Open database ]|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| + | function recordUpdate(pddbname, record, field, value){ |
− |
| |
− |
| |
− | personal.indexedDB.open = function(){
| |
− | // open database of
| |
− | var request = indexedDB.open("personalDataIO");
| |
− |
| |
− | request.onsuccess = function(e){
| |
− | //
| |
− | personal.indexedDB.db = e.target.result;
| |
− | var db = personal.indexedDB.db;
| |
− |
| |
− | //object stores are created in setVersion transactions
| |
− | if(indexedDBVersion!=db.version){
| |
− | var setDBVersion = db.setVersion(indexedDBVersion);
| |
− |
| |
− | //object store is created onsuccess
| |
− | setDBVersion.onerror = personal.indexedDB.onerror;
| |
− |
| |
− | setDBVersion.onsuccess = function(e){
| |
− | if(db.objectStoreNames.contains("personalDataIO")){
| |
− | // db.openObjectStore("personalDataIO");
| |
− | } else {
| |
− | var store = db.createObjectStore("personalDataIO",{autoIncrement:true});
| |
− | }
| |
− | store.createIndex("NameIndex", ["name.last", "name.first"]);
| |
− | personal.indexedDB.recordAll();
| |
− | };
| |
− | }else{
| |
− | personal.indexedDB.recordAll();
| |
− | }
| |
− | };
| |
− | request.onerror = personal.indexedDB.onerror;
| |
− | };//end open
| |
− |
| |
| | | |
− | //|||||||||||||||||||||||||||||[ Add new record ]|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| + | var pddb = window.indexedDB.open(pddbname, 1); |
− |
| |
| | | |
− | //add Items
| + | pddb.addEventListener('error', (event) => { |
− | personal.indexedDB.recordAdd = function(recordObject){
| + | console.log('Request error:', pddb.error); |
− | var db = personal.indexedDB.db;
| + | }, false); |
− | var trans = db.transaction(["personalDataIO"],IDBTransaction.READ_WRITE);
| |
− | var store = trans.objectStore("personalDataIO");
| |
− | var request = store.put(recordObject);
| |
− |
| |
− | request.onsuccess = function(e){
| |
− | personal.indexedDB.recordAll();
| |
− | };
| |
− | request.onerror = function(e){
| |
− | console.log('Error adding: '+e);
| |
− | };
| |
− | };
| |
− |
| |
| | | |
− | //|||||||||||||||||||||||||||||[ Add new property to existing record ]|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| + | pddb.onupgradeneeded = function() { |
− |
| + | var db = pddb.result; |
− | //get Items
| + | var store = db.createObjectStore("MyObjectStore", {keyPath: "id"}); |
− | personal.indexedDB.recordAppend = function(Uname, field, value){
| + | var index = store.createIndex("NameIndex", ["name.last", "name.first"]); |
| + | }; |
| | | |
− | var db = personal.indexedDB.db;
| + | pddb.onsuccess = function() { |
− | var trans = db.transaction("personalDataIO");
| + | // Start a new transaction |
− | var store = trans.objectStore("personalDataIO");
| + | var db = pddb.result; |
− |
| + | var tx = db.transaction("MyObjectStore", "readwrite"); |
− | var index = store.index(uname);
| + | var store = tx.objectStore("MyObjectStore"); |
− | index.get(uname).onsuccess = function(e){
| + | var index = store.index("NameIndex"); |
− | var nit = e.target.result;
| |
− | nit.field = value;
| |
− | recordAdd(nit);
| |
− | };
| |
− | };
| |
− |
| |
− | //|||||||||||||||||||||||||||||[ Init wrapper ]|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| |
− |
| |
− | function init() {
| |
− | personal.indexedDB.open();
| |
− | }
| |
| | | |
− | //||||||||||||||||||||||||||||||[ Get Record by name]||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| + | var getRecord = index.get([record.name.first, record.name.last]); |
− |
| + | getRecord.onsuccess = function() { |
− | personal.indexedDB.recordGet = function(uname) {
| + | console.log("updating"); |
− | var db = personal.indexedDB.db;
| + | getRecord.result.field = value; // => "Bob" |
− | var trans = db.transaction("personalDataIO");
| + | store.put(getRecord.result); |
− | var store = trans.objectStore("personalDataIO");
| |
− |
| |
− | var index = store.index(uname);
| |
− | console.log(index);
| |
− |
| |
− | index.get(uname).onsuccess = function(e){ | |
− | console.log(e.target);
| |
− | /*/ $('#results').append('<li>'+e.target.result+'</li>'); /*///
| |
− | };
| |
− |
| |
− | }
| |
| | | |
| + | getRecordAgain = index.get([record.name.first, record.name.last]); |
| + | getRecordAgain.onsuccess = function() { |
| + | console.log("hey there" + getRecordAgain); // => "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() { |
− | init();
| + | db.close(); |
− | | + | }; |
− | personal.indexedDB.recordAdd(Pete); | + | }; |
− | personal.indexedDB.recordAppend(["Pete", "Rock"], 'telephone', '+123-XXX');
| + | }; |
− | personal.indexedDB.recordGet(["Pete", "Rock"]);
| |