HTML/Web Storage: Difference between revisions

From Chorke Wiki
Jump to navigation Jump to search
Line 47: Line 47:
==Database Engine==
==Database Engine==
<syntaxhighlight lang="js">
<syntaxhighlight lang="js">
//main database engine
;CKi.db||(function($, win, me){
;CKi.db||(function($, win, me){
     var session = win && win.sessionStorage,
     var session = win && win.sessionStorage,
Line 71: Line 72:
     };
     };
     $.db = me;
     $.db = me;
})(CKi, window);
//permanent/local storage database
;CKi.db.pdb||(function($, win, me){
    var local  = win && win.localStorage;
       
    me  = {
        $package : 'CKi.db',
        $class  : 'CKi.db.pdb',
        $meta    : function(){return $.meta(me)}
    };
    $.db.pdb = me;
})(CKi, window);
//temporary/session storage database
;CKi.db.tdb||(function($, win, me){
    var session = win && win.sessionStorage;
       
    me  = {
        $package : 'CKi.db',
        $class  : 'CKi.db.tdb',
        $meta    : function(){return $.meta(me)}
    };
    $.db.tdb = me;
})(CKi, window);
})(CKi, window);
</syntaxhighlight>
</syntaxhighlight>

Revision as of 23:09, 27 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)};
    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, data){
            if(name && (store = secured ? session : local)){
                data = JSON.parse(store.getItem(name));
                me[name] = data;
                return data;
            }
        },
        setData  : function(name, data, secured, store){
            if(name && data && (store = secured ? session : local)){
                store.setItem(name, JSON.stringify(data));
                me[name] = data;
                return true;
            }
        }
    };
    $.db = me;
})(CKi, window);

//permanent/local storage database
;CKi.db.pdb||(function($, win, me){
    var local   = win && win.localStorage;
        
    me  = {
        $package : 'CKi.db',
        $class   : 'CKi.db.pdb',
        $meta    : function(){return $.meta(me)}
    };
    $.db.pdb = me;
})(CKi, window);

//temporary/session storage database
;CKi.db.tdb||(function($, win, me){
    var session = win && win.sessionStorage;
        
    me  = {
        $package : 'CKi.db',
        $class   : 'CKi.db.tdb',
        $meta    : function(){return $.meta(me)}
    };
    $.db.tdb = me;
})(CKi, window);

Checking Output

//store data into local/session storage
CKi.db.setData('T00I00', {id: 2, name: 'Selina'}, true);
CKi.db.setData('T00I00', {id: 1, name: 'Raiyan'});

//get data from local/session storage
CKi.db.getData('T00I00', true);
CKi.db.getData('T00I00');

References