HTML/Web Storage: Difference between revisions

From Chorke Wiki
Jump to navigation Jump to search
Line 154: Line 154:
                 return $.db.setData(name, (me[name] = data), secured);
                 return $.db.setData(name, (me[name] = data), secured);
             }
             }
        },
        find    : function(name, params){
            return $.db.find(name, params, secured);
         }
         }
     };
     };

Revision as of 03:14, 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=!0;for(p in o){if(o.hasOwnProperty(p)){f=!1}}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)},
        getData  : function(name, secured, store){
            if($.hasDB() && name && (store = secured ? session : local)){
                return JSON.parse(store.getItem(name));
            }
        },
        setData  : function(name, data, secured, store){
            if($.hasDB() && name && data && (store = secured ? session : local)){
                store.setItem(name, JSON.stringify(data));
                return true;
            }
        },
        find     : function(name, params, secured, records, index, record, field, value, match, result){
            records = me.getData(name, secured);
            if($.isEmpty(params)){
                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(params)||$.isString(params)){
                            if(record.hasOwnProperty('id')&&(record.id===params)) return record;
                        } else if($.isObject(params)){
                            match=true;
                            for(field in params){
                                match = record.hasOwnProperty(field)&&(record[field]!==params[field]) ? !1 : match;
                            }
                            if(match) result.push(record);
                        }
                    }
                }
                return !$.isEmpty(result) ? result : null;
            }
        }
    };
    $.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)},
        getData  : function(name){
            if($.hasDB() && name){
                return (me[name] = $.db.getData(name));
            }
        },
        setData  : function(name, data){
            if($.hasDB() && name && data){
                return $.db.setData(name, (me[name] = data));
            }
        },
        find     : function(name, params){
            return $.db.find(name, params);
        }
    };
    $.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)},
        getData  : function(name){
            if($.hasDB() && name && secured){
                return (me[name] = $.db.getData(name, secured));
            }
        },
        setData  : function(name, data){
            if($.hasDB() && name && data && secured){
                return $.db.setData(name, (me[name] = data), secured);
            }
        },
        find     : function(name, params){
            return $.db.find(name, params, secured);
        }
    };
    $.db.sdb = me;
})(CKi, !0);

Checking Output

//insert/store data into client db
CKi.db.sdb.setData('T00I00', [{id: 1, name: 'Selina'}, {id: 2, name: 'Daliya'}]);
CKi.db.pdb.setData('T00I00', [{id: 1, name: 'Raiyan'}, {id: 2, name: 'Rashid'}]);

//retrieve data from client db
CKi.db.sdb.getData('T00I00');
CKi.db.pdb.getData('T00I00');

References