-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLiteStorage.min.js
1 lines (1 loc) · 3.73 KB
/
LiteStorage.min.js
1
db=function(){"use strict";var a,e={connect:function(b){if(b){if(!("indexedDB"in window))return f.connect(b),void 0;d=this,a=indexedDB.open("chdb"),a.onupgradeneeded=function(){a.result.createObjectStore("kv")},a.onerror=function(a){a.preventDefault()},a.onsuccess=function(a){e.db=a.target.result,e.setItem("hello","world",function(){e.getItem("hello",function(a){"world"==a?b(e.db):f.connect(b)})})}}},setItem:function(a,b,c){null===v&&(v=void 0);var d=this.db.transaction("kv","readwrite").objectStore("kv"),e=d.put(b,a);c&&(e.onsuccess=function(){c(b)},e.onerror=function(){c()})},getItem:function(a,b){var c,d;b&&(c=this.db.transaction("kv","readonly").objectStore("kv"),d=c.get(a),d.onsuccess=function(){var a=d.result;void 0===a&&(a=null),b(a)},d.onerror=function(){b()})},removeItem:function(a,b){var c=this.db.transaction("kv","readwrite"),d=c.objectStore("kv");d.delete(a),b&&(c.oncomplete=function(){b(null)},c.onerror=c.onabort=function(){b()})},iterate:function(a,b){var c,d;a&&(c=this.db.transaction("kv","readonly").objectStore("kv"),d=c.openCursor(),d.onsuccess=function(){var e,f,c=d.result;c?(e=c.key,f=c.value,void 0===a(e,f)?c.continue():b&&b(null)):b&&b(null)},d.onerror=function(){b&&b()})},clear:function(a){var b=this.db.transaction("kv","readwrite").objectStore("kv"),c=b.clear();a&&(c.onsuccess=function(){a(null)},c.onerror=c.onabort=function(){a()})}},f={connect:function(a){if(a){if(!("openDatabase"in window))return g.connect(a),void 0;d=this,this.db=openDatabase("chdb","","chWebOS",4980736),this.db.transaction(function(b){b.executeSql("CREATE TABLE IF NOT EXISTS chdb (id INTEGER PRIMARY KEY, key unique, value)",[],function(){f.setItem("hello","world",function(){f.getItem("hello",function(b){"world"==b?a(e.db):f.connect(a)})})},function(){d.connect(a)})})}},setItem:function(a,b,c){this.db.transaction(function(d){d.executeSql("INSERT OR REPLACE INTO chdb (key, value) VALUES (?, ?)",[a,JSON.stringify(b)],function(){c&&c(b)},function(){c&&c()})})},getItem:function(a,b){b&&this.db.transaction(function(c){c.executeSql("SELECT * FROM chdb WHERE key = ? LIMIT 1",[a],function(a,c){b(c.rows.length?JSON.parse(c.rows.item(0).value):null)},function(){b()})})},removeItem:function(a,b){this.db.transaction(function(c){c.executeSql("DELETE FROM chdb WHERE key = ?",[a],function(){b&&b(null)},function(){b&&b()})})},iterate:function(a,b){a&&this.db.transaction(function(c){c.executeSql("SELECT * FROM chdb",[],function(c,d){var g,h,e=d.rows,f=e.length;for(g=0;f>g;g++)if(h=e.item(g),void 0!==a(h.key,JSON.parse(h.value)))return b&&b(null),void 0;b&&b(null)})})},clear:function(a){this.db.transaction(function(b){b.executeSql("DELETE FROM chdb",[],function(){a&&a(null)},function(){a&&a()})})}},g={connect:function(a){if(a){if(!("localStorage"in window))return console.warn("浏览器不支持本地储存!"),void 0;d=this,console.warn("浏览器正在使用localStorage存储!"),this.db=localStorage,a(this.db)}},setItem:function(a,b,c){this.db.setItem(a,JSON.stringify(b)),c&&c(b)},getItem:function(a,b){b&&b(JSON.parse(this.db.getItem(a)))},removeItem:function(a,b){this.db.removeItem(a),b&&b(null)},iterate:function(a,b){var c,d;if(a){for(c=0;c<this.db.length;c++)if(d=this.db.key(c),void 0!==a(d,this.db.getItem(d)))return b&&b(null),void 0;b&&b(null)}},clear:function(a){for(var b=this.db.length-1;b>=0;b--)this.db.removeItem(this.db.key(b));a&&a(null)}},d=e;return{setItem:function(a,b,c){d.db?d.setItem(a,b,c):d.connect(function(){d.setItem(a,b,c)})},getItem:function(a,b){d.db?d.getItem(a,b):d.connect(function(){d.getItem(a,b)})},removeItem:function(a,b){d.db?d.removeItem(a,b):d.connect(function(){d.removeItem(a,b)})},iterate:function(a,b){d.db?d.iterate(a,b):d.connect(function(){d.iterate(a,b)})},clear:function(a){d.db?d.clear(a):d.connect(function(){d.clear(a)})}}}();