forked from careerlister/indexedDB
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindexedDB.min.js
9 lines (9 loc) · 4 KB
/
indexedDB.min.js
1
2
3
4
5
6
7
8
9
function DB(i){var q=this,y=window.indexedDB||window.webkitIndexedDB||window.mozIndexedDB,z=window.IDBKeyRange||window.webkitIDBKeyRange||window.mozIDBKeyRange,B=(window.IDBTransaction||window.webkitIDBTransaction).readwrite||"readwrite",r,w,s,t,A,h,u,v,n,g,j,x,aa="Invalid parameter: ",bb="Could not get object store ",cc="continue",dd="delete",ee="Error ",ff=" from ";x=function(a){this.message=a};x.prototype.toString=function(){return"IndexedDBException: "+this.message};h=function(a,f,b){a&&a instanceof Function&&a.apply(f,b)};u=function(a){return"[object Array]"===Object.prototype.toString.call(a)};v=
function(a){A&&(console&&console.log)&&console.log(a)};n=function(a,f,b){var c;try{c=b?a.transaction(f,"readwrite"):a.transaction(f)}catch(d){v(d.message)}if(!c)try{c=b?a.transaction([f],B):a.transaction([f])}catch(e){v(e.message)}return c};g=function(a,f){if(!a)throw new x(f);};w=""+i.name;s=parseInt(i.version,10);t=i.schema;A=!!i.debug;g(w.length,aa+"name <string>");g(s,aa+"version <int>");g(u(t),aa+"schema <array>");if(!y)return h(i.error,q,[!1,$e]),
!1;r=y.open(w,s);r.onerror=function(a){h(i.error,q,[!0,a])};r.onupgradeneeded=function(a){for(var f=a.target.result,b,c,d,e=0,k=t.length;e<k;e++)if(c=t[e],b=f.createObjectStore(c.name,c.key),u(c.indexes)){for(var g=0,l=c.indexes.length;g<l;g++)d=c.indexes[g],b.createIndex(d.name,d.field,{unique:!!d.unique});h(i.upgrade,q,[a])}};r.onsuccess=function(a){var f=r.result,b=new j(f);s!==f.version?(a=f.setVersion(s),a.onerror=function(a){h(i.upgradeError,q,[a])},a.onsuccess=function(a){for(var d,e,k,g=0,
l=schema.length;g<l;g++)if(e=schema[g],d=f.createObjectStore(e.name,e.key),u(e.indexes)){for(var p=0,m=e.indexes.length;p<m;p++)k=e.indexes[p],d.createIndex(k.name,k.field,{unique:!!k.unique});h(i.upgrade,q,[a])}a.target.transaction.oncomplete=function(a){h(i.success,q,[b,a])}}):h(i.success,q,[b,a])};j=function(a){this.db=a};j.prototype.insert=function(a){var f=this,b=a.store,c=a.data,d=!!a.repl,e=n(this.db,b,!0),k,i,l=[],p=[],m;g(e,ee+"inserting to "+b);k=e.objectStore(b);g(k,bb+
b);m=function(b){l.length+p.length===j&&h(a.error,f,[l,p,b])};for(var b=function(a){try{i=d?k.put(a):k.add(a)}catch(b){v(b.message),p.push(a),m({})}i&&(i.onsuccess=function(b){l.push(a);m(b)},i.onerror=function(b){p.push(a);m(b)})},e=0,j=c.length;e<j;e++)b(c[e])};j.prototype[dd]=function(a){var f=this,b=a.store,c=a.id,d=n(this.db,b,!0);g(d,ee+"deleting "+c+ff+b);d=d.objectStore(b);g(d,bb+b);b=d[dd](c);b.onsuccess=function(b){h(a.success,f,[c,b])};b.onerror=
function(b){h(a.error,f,[c,b])}};j.prototype.get=function(a){var f=this,b=a.store,c=a.id,d=n(this.db,b),e;g(d,ee+"getting "+c+ff+b);d=d.objectStore(b);g(d,bb+b);e=d.get(c);e.onsuccess=function(c){h(a.success,f,[e.result,b,c])};e.onerror=function(d){h(a.error,f,[c,b,d])}};j.prototype.all=function(a){var f=this,b=a.store,c=n(this.db,b),d,e=[];g(c,ee+"getting all from "+b);c=c.objectStore(b);g(c,bb+b);c=c.openCursor();c.onsuccess=function(c){(d=
c.target.result)?(e.push(d.value),d[cc]()):h(a.success,f,[e,b,c])};c.onerror=function(c){h(a.error,f,[b,c])}};j.prototype.filter=function(a){var f=this,b=a.store,c=a.key,d=a.value,e=a.order,k=a.orderCol,i=a.limit,l=n(this.db,b),j,m=[];g(l,ee+"filtering "+c+" for "+d+ff+b);l=l.objectStore(store);g(l,bb+b);c=l.index(c);d=z.only(d);d=c.openCursor(d);d.onsuccess=function(c){(j=c.target.result)?(m.push(j.value),j[cc]()):(e&&k&&(m.sort(function(a,b){var c=
a[k],d=b[k];return c<d?-1:c>d?1:0}),"desc"===e&&m.reverse()),i&&(m=m.slice(0,i)),h(a.success,f,[m,b,c]))};d.onerror=function(c){h(a.error,f,[b,c])}};j.prototype.clear=function(a){var f=this,b=a.store,c=n(this.db,b,!0);g(c,ee+"clearing "+b);c=c.objectStore(b);g(c,bb+b);c=c.clear();c.onsuccess=function(c){h(a.success,f,[b,c])};c.onerror=function(c){h(a.error,f,[b,c])}};j.prototype.count=function(a){var f=this,b=a.store,c=a.key,d=a.value,e=n(this.db,b,!0);g(e,ee+"counting "+
b);e=e.objectStore(b);g(e,bb+b);c&&d?(c=e.index(c),d=z.only(d),d=c.count(d)):d=e.count();d.onsuccess=function(c){h(a.success,f,[c.target.result,b,c])};d.onerror=function(){h(a.error,f,[b,$e])}}};