Difference between revisions of "MediaWiki:Gadget-personaldata.js"
Jump to navigation
Jump to search
m |
m |
||
Line 37: | Line 37: | ||
this.EnqueuePDIO = function( sw, c1, c2, c3 ) { | this.EnqueuePDIO = function( sw, c1, c2, c3 ) { | ||
− | + | return Enquire( PersonalDataDataBaseName, CurrentPerson, sw, c1, c2, c3 ); | |
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
} | } | ||
Line 63: | Line 57: | ||
// retrieve record, make record.field1 = field2, push back | // retrieve record, make record.field1 = field2, push back | ||
− | + | function Enquire( pddbname, record, sqitch, control_1, control_2, control_3 ) { | |
− | + | return new Promise( | |
− | + | function(resolve, reject) { | |
− | |||
− | |||
− | |||
− | + | var pddb = await window.indexedDB.open(pddbname, 3); | |
+ | var inp_obj; | ||
+ | pddb.onupgradeneeded = function() { | ||
+ | var db = pddb.result; | ||
+ | var store = db.createObjectStore(pddbname, {keyPath: "id"}); | ||
− | + | // shouldn't the index be the wiki.personaldata.io username? | |
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | + | var index = store.createIndex("NameIndex", ["id"]); | |
− | + | }; | |
+ | pddb.onsuccess = function() { | ||
+ | var db = pddb.result; | ||
+ | var tx = db.transaction(pddbname, "readwrite"); | ||
+ | var store = tx.objectStore(pddbname); | ||
+ | var index = store.index("NameIndex"); | ||
− | + | // Get the original record from the db... | |
− | + | var getRecord = index.get([record.id]); | |
− | |||
− | |||
− | + | getRecord.onsuccess = function() { | |
− | + | console.log( "Record found in database: \n <<"); | |
− | + | console.log( getRecord.result ); | |
− | + | console.log( ">>"); | |
− | |||
− | |||
− | |||
− | |||
− | console.log(" | ||
− | |||
− | |||
− | console.log(" | ||
− | |||
− | |||
− | |||
− | + | // Essential recordAdd | |
− | + | if ( sqitch === "aggressive push" ) { | |
− | + | inp_obj = record; | |
− | if ( sqitch === " | + | await store.put( inp_obj ); |
− | |||
− | |||
− | |||
return inp_obj; | return inp_obj; | ||
− | } | + | } else if ( sqitch === "checkin" ) { |
− | + | ||
− | + | if ( getRecord.result != undefined ) { | |
− | + | console.log("getting from store"); | |
− | + | inp_obj = getRecord.result; | |
− | + | } else { | |
− | + | console.log("overriding whatever in there"); | |
+ | inp_obj = record; | ||
+ | await store.put(inp_obj); | ||
+ | } | ||
+ | |||
} else { | } else { | ||
− | + | if ( getRecord.result != undefined ) { | |
− | + | inp_obj = getRecord.result; | |
− | + | if ( sqitch === "get record" ) { | |
− | + | // if found, let's use that from now on | |
− | + | console.log("Getting record"); | |
− | + | // let's return it so that our addrecord can be used as a getter as well | |
− | + | return inp_obj; | |
− | + | } | |
− | + | // | |
− | + | // endof onsuccess | |
− | + | } else { | |
− | if ( | + | console.log("record not found"); |
− | inp_obj | + | if ( sqitch === "update record" ) { |
+ | inp_obj = record; | ||
} else { | } else { | ||
− | + | return undefined; | |
} | } | ||
− | store.put(inp_obj); | + | } |
− | + | // endof resultdefined | |
− | + | } | |
− | + | // endofELSE | |
− | + | if ( sqitch === "update record" ) { // Record Update | |
+ | if ( control_1 != undefined ) { | ||
+ | if ( control_2 != undefined ) { | ||
+ | if ( control_3 != undefined ) { | ||
+ | |||
+ | if ( Array.isArray( inp_obj[ control_1 ][ control_2 ] ) ) { | ||
+ | inp_obj[ control_1 ][ control_2 ].push( control_3 ); | ||
+ | } else { | ||
+ | inp_obj[ control_1 ][ control_2 ] = control_3; // => "Bob" | ||
+ | } | ||
+ | store.put(inp_obj); | ||
+ | } else { | ||
+ | |||
+ | if ( Array.isArray( inp_obj[ control_1 ] ) ) { | ||
+ | inp_obj[ control_1 ].push( control_2 ); | ||
+ | } else { | ||
+ | inp_obj[ control_1 ] = control_2; | ||
+ | } | ||
+ | store.put(inp_obj); | ||
+ | |||
+ | } | ||
+ | // +3 | ||
} else { | } else { | ||
− | inp_obj[ control_1 ] = | + | store.put(inp_obj); |
+ | console.log("mark N"); | ||
+ | console.log(inp_obj); | ||
+ | console.log(inp_obj[ control_1 ]); | ||
+ | console.log("mark NN") | ||
+ | inp_obj = inp_obj[ control_1 ]; | ||
} | } | ||
− | + | // +2 | |
+ | } else { | ||
+ | |||
+ | //return inp_obj; | ||
} | } | ||
− | + | console.log("return 3") | |
− | + | // return inp_obj; | |
− | |||
− | console.log(" | ||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | + | }; | |
+ | console.log("return 2") | ||
+ | resolve( inp_obj ); | ||
− | } | + | }; |
− | |||
− | |||
+ | await tx.oncomplete = function() { | ||
+ | console.log("return 1") | ||
+ | db.close(); | ||
+ | }; | ||
}; | }; | ||
− | console.log("return | + | console.log("return 0") |
− | + | }); | |
− | + | } | |
− | } | ||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | } | ||
}; | }; |
Revision as of 22:20, 18 May 2019
// License: GPL
// Indexeddb JavaScript
console.log('Indb loading');
function idxdbContainer( ww ) {
hashCode = function(s) {
if ( s == undefined) {
s = '00'
}
var h = 0, l = s.length, i = 0;
if ( l > 0 )
while (i < l)
h = (h << 5) - h + s.charCodeAt(i++) | 0;
return h;
};
if ( ww.user != undefined ) {
var self = ww.user.getName();
var idgen = hashCode(self);
console.log(idgen);
// Take username for standard databasename format
var PersonalDataDataBaseName = "PD.IO" + idgen + "Database";
// Use username in user to get up to date version of self
var CurrentPerson = {id: idgen, name: {user: self}, age: 0};
console.log(PersonalDataDataBaseName);
console.log(CurrentPerson);
}
// ADD MORE DOC
// EnqueuePDIO a PDIOLocalDatabase:User database access function with modes
// External accessible wrapper for Enquire
this.EnqueuePDIO = function( sw, c1, c2, c3 ) {
return Enquire( PersonalDataDataBaseName, CurrentPerson, sw, c1, c2, c3 );
}
// Enquire, a general purpose indexeddb update function with modes
// Mode 'aggressive push':
// parameter record is pushed to database, overwriting whatever is there
// Mode 'checkin':
// record is retrieved from database, parameter record is pushed if not found
// Mode 'get record'
// tries to get record from db, returns undefined if not found
// Mode 'update record' field1 field2 field3
// retrieve record, make record.field1.field2 = field3, push back
// Mode 'update record' field1 field2
// retrieve record, make record.field1 = field2, push back
function Enquire( pddbname, record, sqitch, control_1, control_2, control_3 ) {
return new Promise(
function(resolve, reject) {
var pddb = await window.indexedDB.open(pddbname, 3);
var inp_obj;
pddb.onupgradeneeded = function() {
var db = pddb.result;
var store = db.createObjectStore(pddbname, {keyPath: "id"});
// shouldn't the index be the wiki.personaldata.io username?
var index = store.createIndex("NameIndex", ["id"]);
};
pddb.onsuccess = function() {
var db = pddb.result;
var tx = db.transaction(pddbname, "readwrite");
var store = tx.objectStore(pddbname);
var index = store.index("NameIndex");
// Get the original record from the db...
var getRecord = index.get([record.id]);
getRecord.onsuccess = function() {
console.log( "Record found in database: \n <<");
console.log( getRecord.result );
console.log( ">>");
// Essential recordAdd
if ( sqitch === "aggressive push" ) {
inp_obj = record;
await store.put( inp_obj );
return inp_obj;
} else if ( sqitch === "checkin" ) {
if ( getRecord.result != undefined ) {
console.log("getting from store");
inp_obj = getRecord.result;
} else {
console.log("overriding whatever in there");
inp_obj = record;
await store.put(inp_obj);
}
} else {
if ( getRecord.result != undefined ) {
inp_obj = getRecord.result;
if ( sqitch === "get record" ) {
// if found, let's use that from now on
console.log("Getting record");
// let's return it so that our addrecord can be used as a getter as well
return inp_obj;
}
//
// endof onsuccess
} else {
console.log("record not found");
if ( sqitch === "update record" ) {
inp_obj = record;
} else {
return undefined;
}
}
// endof resultdefined
}
// endofELSE
if ( sqitch === "update record" ) { // Record Update
if ( control_1 != undefined ) {
if ( control_2 != undefined ) {
if ( control_3 != undefined ) {
if ( Array.isArray( inp_obj[ control_1 ][ control_2 ] ) ) {
inp_obj[ control_1 ][ control_2 ].push( control_3 );
} else {
inp_obj[ control_1 ][ control_2 ] = control_3; // => "Bob"
}
store.put(inp_obj);
} else {
if ( Array.isArray( inp_obj[ control_1 ] ) ) {
inp_obj[ control_1 ].push( control_2 );
} else {
inp_obj[ control_1 ] = control_2;
}
store.put(inp_obj);
}
// +3
} else {
store.put(inp_obj);
console.log("mark N");
console.log(inp_obj);
console.log(inp_obj[ control_1 ]);
console.log("mark NN")
inp_obj = inp_obj[ control_1 ];
}
// +2
} else {
//return inp_obj;
}
console.log("return 3")
// return inp_obj;
};
console.log("return 2")
resolve( inp_obj );
};
await tx.oncomplete = function() {
console.log("return 1")
db.close();
};
};
console.log("return 0")
});
}
};