HTML/Web Storage: Difference between revisions
Jump to navigation
Jump to search
(13 intermediate revisions by the same user not shown) | |||
Line 56: | Line 56: | ||
me.isUndefine = function(o){return me.getType(o)==='[object Undefined]'}; | me.isUndefine = function(o){return me.getType(o)==='[object Undefined]'}; | ||
me.isEmpty = function(o){return me.isNone(o)||(me.isString(o)&&o.trim()==='')||(me.isArray(o)&&o.length===0) | me.isEmpty = function(o){return me.isNone(o)||(me.isString(o)&&o.trim()==='')||(me.isArray(o)&&o.length===0) | ||
||(me.isObject(o)&&(function(p,f){f= | ||(me.isObject(o)&&(function(p,f){f=true;for(p in o){if(o.hasOwnProperty(p)){f=false}}return f}())) | ||
}; | }; | ||
return me; | return me; | ||
Line 72: | Line 72: | ||
$class : 'CKi.db', | $class : 'CKi.db', | ||
$meta : function(){return $.meta(me)}, | $meta : function(){return $.meta(me)}, | ||
truncate : function(name, secured, store){ | |||
if($.hasDB() && name && (store = secured ? session : local)){ | |||
if(!$.isEmpty(me.findAll(name, secured))) store.removeItem(name); | |||
} | |||
}, | |||
findAll : function(name, secured, store){ | |||
if($.hasDB() && name && (store = secured ? session : local)){ | if($.hasDB() && name && (store = secured ? session : local)){ | ||
return JSON.parse(store.getItem(name)); | return JSON.parse(store.getItem(name)); | ||
} | } | ||
}, | }, | ||
saveAll : function(name, data, secured, store){ | |||
if($.hasDB() && name && data && (store = secured ? session : local)){ | if($.hasDB() && name && data && (store = secured ? session : local)){ | ||
store.setItem(name, JSON.stringify(data)); | store.setItem(name, JSON.stringify(data)); | ||
Line 83: | Line 88: | ||
} | } | ||
}, | }, | ||
findBy : function(name, where, secured, records, index, record, field, match, result){ | |||
records = me. | records = me.findAll(name, secured); | ||
if($.isEmpty( | if($.isEmpty(where)){ | ||
return records; | return records; | ||
} else if(!$.isEmpty(records)&&$.isArray(records)){ | } else if(!$.isEmpty(records)&&$.isArray(records)){ | ||
result = [] | result = []; | ||
for(index=0;index<records.length;index++){ | for(index=0;index<records.length;index++){ | ||
record=records[index]; | record=records[index]; | ||
if(!$.isEmpty(record)&&$.isObject(record)){ | if(!$.isEmpty(record)&&$.isObject(record)){ | ||
if($.isNumber( | if($.isNumber(where)||$.isString(where)){ | ||
if(record.hasOwnProperty('id')&&(record.id=== | if(record.hasOwnProperty('id')&&(record.id===where)) return record; | ||
} else if($.isObject( | } else if($.isObject(where)){ | ||
match=true; | match=true; | ||
for(field in | for(field in where){ | ||
match = record.hasOwnProperty(field)&&(record[field]!== | match = record.hasOwnProperty(field)&&(record[field]!==where[field]) ? false : match; | ||
} | } | ||
if(match) result.push(record); | if(match) result.push(record); | ||
Line 104: | Line 109: | ||
} | } | ||
return !$.isEmpty(result) ? result : null; | return !$.isEmpty(result) ? result : null; | ||
} | |||
}, | |||
deleteBy : function(name, where, secured, records, index, record, field, match, result){ | |||
records = me.findAll(name, secured); | |||
if(!$.isEmpty(where)&&!$.isEmpty(records)&&$.isArray(records)){ | |||
result = []; | |||
for(index=0;index<records.length;index++){ | |||
record=records[index]; | |||
if(!$.isEmpty(record)&&$.isObject(record)){ | |||
if($.isNumber(where)||$.isString(where)){ | |||
if(record.hasOwnProperty('id')&&(record.id!==where)) result.push(record); | |||
} else if($.isObject(where)){ | |||
match=true; | |||
for(field in where){ | |||
match = record.hasOwnProperty(field)&&(record[field]!==where[field]) ? false : match; | |||
} | |||
if(!match) result.push(record); | |||
} | |||
} | |||
} | |||
if(!$.isEmpty(result)){ | |||
me.saveAll(name, result, secured); | |||
} else { | |||
me.truncate(name, secured); | |||
} | |||
} | } | ||
} | } | ||
Line 119: | Line 150: | ||
$class : 'CKi.db.pdb', | $class : 'CKi.db.pdb', | ||
$meta : function(){return $.meta(me)}, | $meta : function(){return $.meta(me)}, | ||
truncate : function(name){ | |||
if($.hasDB() && name){ | |||
$.db.truncate(name); | |||
} | |||
}, | |||
findAll : function(name){ | |||
if($.hasDB() && name){ | if($.hasDB() && name){ | ||
return (me[name] = $.db. | return (me[name] = $.db.findAll(name)); | ||
} | } | ||
}, | }, | ||
saveAll : function(name, data){ | |||
if($.hasDB() && name && data){ | if($.hasDB() && name && data){ | ||
return $.db. | return $.db.saveAll(name, (me[name] = data)); | ||
} | |||
}, | |||
findBy : function(name, where){ | |||
if($.hasDB() && name && where){ | |||
return $.db.findBy(name, where); | |||
} | } | ||
}, | }, | ||
deleteBy : function(name, where){ | |||
if($.hasDB() && name && where){ | |||
$.db.deleteBy(name, where); | |||
me.findAll(name); | |||
} | |||
} | } | ||
}; | }; | ||
Line 145: | Line 189: | ||
$class : 'CKi.db.sdb', | $class : 'CKi.db.sdb', | ||
$meta : function(){return $.meta(me)}, | $meta : function(){return $.meta(me)}, | ||
truncate : function(name){ | |||
if($.hasDB() && name && secured){ | if($.hasDB() && name && secured){ | ||
$.db.truncate(name, secured); | |||
} | } | ||
}, | }, | ||
findAll : function(name){ | |||
if($.hasDB() && name && secured){ | |||
return (me[name] = $.db.findAll(name, secured)); | |||
} | |||
}, | |||
saveAll : function(name, data){ | |||
if($.hasDB() && name && data && secured){ | if($.hasDB() && name && data && secured){ | ||
return $.db. | return $.db.saveAll(name, (me[name] = data), secured); | ||
} | |||
}, | |||
findBy : function(name, where){ | |||
if($.hasDB() && name && where){ | |||
return $.db.findBy(name, where, secured); | |||
} | |||
}, | |||
deleteBy : function(name, where){ | |||
if($.hasDB() && name && where){ | |||
$.db.deleteBy(name, where, secured); | |||
me.findAll(name); | |||
} | } | ||
} | } | ||
}; | }; | ||
$.db.sdb = me; | $.db.sdb = me; | ||
})(CKi, | })(CKi, true); | ||
</syntaxhighlight> | </syntaxhighlight> | ||
Line 163: | Line 223: | ||
<syntaxhighlight lang="js"> | <syntaxhighlight lang="js"> | ||
//insert/store data into client db | //insert/store data into client db | ||
CKi.db.sdb. | CKi.db.sdb.saveAll('T00I00', [{id: 1, name: 'Selina'}, {id: 2, name: 'Daliya'}]); | ||
CKi.db.pdb. | CKi.db.pdb.saveAll('T00I00', [{id: 1, name: 'Raiyan'}, {id: 2, name: 'Rashid'}]); | ||
//retrieve data from client db | //retrieve data from client db | ||
CKi.db.sdb. | CKi.db.sdb.findAll('T00I00'); | ||
CKi.db.pdb. | CKi.db.pdb.findAll('T00I00'); | ||
CKi.db.pdb.findBy('T00I00', 1) | |||
CKi.db.pdb.findBy('T00I00', {id: 1}) | |||
CKi.db.sdb.findBy('T00I00', {id: 2, name: 'Daliya'}) | |||
CKi.db.pdb.deleteBy('T00I00', 1) | |||
CKi.db.pdb.deleteBy('T00I00', {id: 1}) | |||
CKi.db.sdb.deleteBy('T00I00', {id: 2, name: 'Daliya'}) | |||
</syntaxhighlight> | </syntaxhighlight> | ||
Latest revision as of 21:28, 29 September 2018
Prototyping Library
//prototyping for string format
;String.prototype.format||(function(){
String.prototype.format = function() {
var args = arguments;
return this.replace(/{(\d+)}/g, function(match, number) {
return typeof args[number] != 'undefined' ? args[number] : match;
});
};
})();
//prototyping for console info
;String.prototype.info||(function(){
String.prototype.info = function() {
if(console && console.info){
console.info(this.toString());
}
return this;
};
})();
Proprietary Library
//chorke main namespace/package
var Chorke=Chorke||(function(me){
me = {
$class : 'Chorke',
$package : 'Default',
meta : function(ns, tpl){
tpl = '{\n "package" : "{0}",\n "class" : "{1}"\n}';
return tpl.format(ns.$package, ns.$class);
}
};
return me;
})(),
//chorke alias name
CKi=CKi||(function($, me){
me = $;
me.$origin = $.meta($);
me.$class = 'CKi';
me.$meta = function(){return me.meta(me)};
me.hasDB = function(){return typeof(Storage)!=='undefined'};
me.isNone = function(o){return me.isNull(o)||me.isUndefine(o)};
me.isNull = function(o){return me.getType(o)==='[object Null]'};
me.isDate = function(o){return me.getType(o)==='[object Date]'};
me.isArray = function(o){return me.getType(o)==='[object Array]'};
me.getType = function(o){return Object.prototype.toString.call(o)};
me.isNumber = function(o){return me.getType(o)==='[object Number]'};
me.isObject = function(o){return me.getType(o)==='[object Object]'};
me.isRegExp = function(o){return me.getType(o)==='[object RegExp]'};
me.isString = function(o){return me.getType(o)==='[object String]'};
me.isBoolean = function(o){return me.getType(o)==='[object Boolean]'};
me.isFunction = function(o){return me.getType(o)==='[object Function]'};
me.isUndefine = function(o){return me.getType(o)==='[object Undefined]'};
me.isEmpty = function(o){return me.isNone(o)||(me.isString(o)&&o.trim()==='')||(me.isArray(o)&&o.length===0)
||(me.isObject(o)&&(function(p,f){f=true;for(p in o){if(o.hasOwnProperty(p)){f=false}}return f}()))
};
return me;
})(Chorke);
Database Engine
//chorke database engine
;CKi.db||(function($, win, me, session, local){
session = win && win.sessionStorage;
local = win && win.localStorage;
me = {
$package : 'CKi',
$class : 'CKi.db',
$meta : function(){return $.meta(me)},
truncate : function(name, secured, store){
if($.hasDB() && name && (store = secured ? session : local)){
if(!$.isEmpty(me.findAll(name, secured))) store.removeItem(name);
}
},
findAll : function(name, secured, store){
if($.hasDB() && name && (store = secured ? session : local)){
return JSON.parse(store.getItem(name));
}
},
saveAll : function(name, data, secured, store){
if($.hasDB() && name && data && (store = secured ? session : local)){
store.setItem(name, JSON.stringify(data));
return true;
}
},
findBy : function(name, where, secured, records, index, record, field, match, result){
records = me.findAll(name, secured);
if($.isEmpty(where)){
return records;
} else if(!$.isEmpty(records)&&$.isArray(records)){
result = [];
for(index=0;index<records.length;index++){
record=records[index];
if(!$.isEmpty(record)&&$.isObject(record)){
if($.isNumber(where)||$.isString(where)){
if(record.hasOwnProperty('id')&&(record.id===where)) return record;
} else if($.isObject(where)){
match=true;
for(field in where){
match = record.hasOwnProperty(field)&&(record[field]!==where[field]) ? false : match;
}
if(match) result.push(record);
}
}
}
return !$.isEmpty(result) ? result : null;
}
},
deleteBy : function(name, where, secured, records, index, record, field, match, result){
records = me.findAll(name, secured);
if(!$.isEmpty(where)&&!$.isEmpty(records)&&$.isArray(records)){
result = [];
for(index=0;index<records.length;index++){
record=records[index];
if(!$.isEmpty(record)&&$.isObject(record)){
if($.isNumber(where)||$.isString(where)){
if(record.hasOwnProperty('id')&&(record.id!==where)) result.push(record);
} else if($.isObject(where)){
match=true;
for(field in where){
match = record.hasOwnProperty(field)&&(record[field]!==where[field]) ? false : match;
}
if(!match) result.push(record);
}
}
}
if(!$.isEmpty(result)){
me.saveAll(name, result, secured);
} else {
me.truncate(name, secured);
}
}
}
};
$.db = me;
})(CKi, window);
Local Database
//permanent/local storage database
;CKi.db.pdb||(function($, me){
me = {
$package : 'CKi.db',
$class : 'CKi.db.pdb',
$meta : function(){return $.meta(me)},
truncate : function(name){
if($.hasDB() && name){
$.db.truncate(name);
}
},
findAll : function(name){
if($.hasDB() && name){
return (me[name] = $.db.findAll(name));
}
},
saveAll : function(name, data){
if($.hasDB() && name && data){
return $.db.saveAll(name, (me[name] = data));
}
},
findBy : function(name, where){
if($.hasDB() && name && where){
return $.db.findBy(name, where);
}
},
deleteBy : function(name, where){
if($.hasDB() && name && where){
$.db.deleteBy(name, where);
me.findAll(name);
}
}
};
$.db.pdb = me;
})(CKi);
Session Database
//temporary/session storage database
;CKi.db.sdb||(function($, secured, me){
me = {
$package : 'CKi.db',
$class : 'CKi.db.sdb',
$meta : function(){return $.meta(me)},
truncate : function(name){
if($.hasDB() && name && secured){
$.db.truncate(name, secured);
}
},
findAll : function(name){
if($.hasDB() && name && secured){
return (me[name] = $.db.findAll(name, secured));
}
},
saveAll : function(name, data){
if($.hasDB() && name && data && secured){
return $.db.saveAll(name, (me[name] = data), secured);
}
},
findBy : function(name, where){
if($.hasDB() && name && where){
return $.db.findBy(name, where, secured);
}
},
deleteBy : function(name, where){
if($.hasDB() && name && where){
$.db.deleteBy(name, where, secured);
me.findAll(name);
}
}
};
$.db.sdb = me;
})(CKi, true);
Checking Output
//insert/store data into client db
CKi.db.sdb.saveAll('T00I00', [{id: 1, name: 'Selina'}, {id: 2, name: 'Daliya'}]);
CKi.db.pdb.saveAll('T00I00', [{id: 1, name: 'Raiyan'}, {id: 2, name: 'Rashid'}]);
//retrieve data from client db
CKi.db.sdb.findAll('T00I00');
CKi.db.pdb.findAll('T00I00');
CKi.db.pdb.findBy('T00I00', 1)
CKi.db.pdb.findBy('T00I00', {id: 1})
CKi.db.sdb.findBy('T00I00', {id: 2, name: 'Daliya'})
CKi.db.pdb.deleteBy('T00I00', 1)
CKi.db.pdb.deleteBy('T00I00', {id: 1})
CKi.db.sdb.deleteBy('T00I00', {id: 2, name: 'Daliya'})