HTML/Web Storage: Difference between revisions

From Chorke Wiki
Jump to navigation Jump to search
No edit summary
Line 57: Line 57:
         $class  : 'CKi.db',
         $class  : 'CKi.db',
         $meta    : function(){return $.meta(me)},
         $meta    : function(){return $.meta(me)},
         getData  : function(name, secured, store, data){
         getData  : function(name, secured, store){
             if(name && (store = secured ? session : local)){
             if($.hasDB() && name && (store = secured ? session : local)){
                 data = JSON.parse(store.getItem(name));
                 return JSON.parse(store.getItem(name));
                me[name] = data;
                return data;
             }
             }
         },
         },
         setData  : function(name, data, secured, store){
         setData  : function(name, data, secured, store){
             if(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));
                me[name] = data;
                 return true;
                 return true;
             }
             }
Line 77: Line 74:
//permanent/local storage database
//permanent/local storage database
;CKi.db.pdb||(function($, win, me){
;CKi.db.pdb||(function($, win, me){
    var local  = win && win.localStorage;
       
     me  = {
     me  = {
         $package : 'CKi.db',
         $package : 'CKi.db',
         $class  : 'CKi.db.pdb',
         $class  : 'CKi.db.pdb',
         $meta    : function(){return $.meta(me)}
         $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));
            }
        }
     };
     };
     $.db.pdb = me;
     $.db.pdb = me;
Line 88: Line 93:


//temporary/session storage database
//temporary/session storage database
;CKi.db.tdb||(function($, win, me){
;CKi.db.tdb||(function($, win, secured, me){
    var session = win && win.sessionStorage;
       
     me  = {
     me  = {
         $package : 'CKi.db',
         $package : 'CKi.db',
         $class  : 'CKi.db.tdb',
         $class  : 'CKi.db.tdb',
         $meta    : function(){return $.meta(me)}
         $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);
            }
        }
     };
     };
     $.db.tdb = me;
     $.db.tdb = me;
})(CKi, window);
})(CKi, window, true);
</syntaxhighlight>
</syntaxhighlight>


==Checking Output==
==Checking Output==
<syntaxhighlight lang="js">
<syntaxhighlight lang="js">
//store data into local/session storage
//insert/store data into client db
CKi.db.setData('T00I00', {id: 2, name: 'Selina'}, true);
CKi.db.tdb.setData('T00I00', {id: 1, name: 'Selina'});
CKi.db.setData('T00I00', {id: 1, name: 'Raiyan'});
CKi.db.pdb.setData('T00I00', {id: 1, name: 'Raiyan'});


//get data from local/session storage
//retrieve data from client db
CKi.db.getData('T00I00', true);
CKi.db.tdb.getData('T00I00');
CKi.db.getData('T00I00');
CKi.db.pdb.getData('T00I00');
</syntaxhighlight>
</syntaxhighlight>



Revision as of 11:00, 28 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){
            var tpl = '{\n  "package" : "{0}",\n  "class"   : "{1}"\n}',
                out = tpl.format(ns.$package, ns.$class);
            return out;
        }
    };
    return me;
})(),
//chorke alias name
CKi=CKi||(function(me){
    me.$class = 'CKi';
    me.$meta  = function(){return me.meta(me)};
    me.hasDB  = function(){return typeof(Storage)!=='undefined'};
    return me;
})(Chorke);

Database Engine

//main database engine
;CKi.db||(function($, win, me){
    var 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;
            }
        }
    };
    $.db = me;
})(CKi, window);

//permanent/local storage database
;CKi.db.pdb||(function($, win, 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));
            }
        }
    };
    $.db.pdb = me;
})(CKi, window);

//temporary/session storage database
;CKi.db.tdb||(function($, win, secured, me){
    me  = {
        $package : 'CKi.db',
        $class   : 'CKi.db.tdb',
        $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);
            }
        }
    };
    $.db.tdb = me;
})(CKi, window, true);

Checking Output

//insert/store data into client db
CKi.db.tdb.setData('T00I00', {id: 1, name: 'Selina'});
CKi.db.pdb.setData('T00I00', {id: 1, name: 'Raiyan'});

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

References