forked from knadh/localStorageDB
-
Notifications
You must be signed in to change notification settings - Fork 2
/
localstoragedb.min.js
12 lines (9 loc) · 5.08 KB
/
localstoragedb.min.js
1
2
3
4
5
6
7
8
9
10
11
12
/*
Kailash Nadh (http://kailashnadh.name)
Del Bianco Luca <[email protected]>
localStorageDB
August 2012
A simple database layer for localStorage with indexes
License : MIT License
*/
function localStorageDB(a){function f(){delete localStorage[c];e=null}function g(){var a=0;for(var b in e.tables){if(e.tables.hasOwnProperty(b)){a++}}return a}function h(a){return e.tables[a]?true:false}function i(a){if(!h(a)){w("The table '"+a+"' does not exist.")}}function j(a,b,c){var d={};for(var f=0;f<c.length;f++){d[c[f]]={}}e.tables[a]={fields:b,auto_increment:1,indexes:d};e.data[a]={}}function k(a){delete e.tables[a];delete e.data[a]}function l(a){e.tables[a].auto_increment=1;e.data[a]={}}function m(a){var b=0;for(var c in e.data[a]){if(e.data[a].hasOwnProperty(c)){b++}}return b}function n(a,b){b.ID=e.tables[a].auto_increment;e.data[a][e.tables[a].auto_increment]=b;e.tables[a].auto_increment++;for(var c in e.tables[a].indexes){if(b.hasOwnProperty(c)){if(e.tables[a].indexes[c][b[c]]===undefined){e.tables[a].indexes[c][b[c]]=[]}if(e.tables[a].indexes[c][b[c]].indexOf(b.ID)==-1){e.tables[a].indexes[c][b[c]].push(b.ID)}}}return b.ID}function o(a,b){var c=null,d=[],f=null;for(var g=0;g<b.length;g++){c=b[g];f=e.data[a][c];d.push(x(f))}return d}function p(a,b,c){var d=[],f=false,g=null;var h=[];var i=false;for(var j in e.tables[a].indexes){if(b.hasOwnProperty(j)&&e.tables[a].indexes[j].hasOwnProperty(b[j])){i=true;for(var k=0;k<e.tables[a].indexes[j][b[j]].length;k++){h[e.tables[a].indexes[j][b[j]][k]]=null}}}if(h.length==0&&!i){h=e.data[a]}for(var l in h){if(!e.data[a].hasOwnProperty(l)){continue}g=e.data[a][l];f=false;for(var m in b){if(!b.hasOwnProperty(m)){continue}if(typeof b[m]=="string"){if(g[m].toString().toLowerCase()==b[m].toString().toLowerCase()){f=true;break}}else{if(g[m]==b[m]){f=true;break}}}if(f){d.push(l)}if(d.length==c){break}}return d}function q(a,b,c){var d=[],f=false,g=null;for(var h in e.data[a]){if(!e.data[a].hasOwnProperty(h)){continue}g=e.data[a][h];if(b(x(g))==true){d.push(h)}if(d.length==c){break}}return d}function r(a,b){var c=[];for(var d in e.data[a]){if(e.data[a].hasOwnProperty(d)){c.push(d);if(c.length==b){break}}}return c}function s(a,b){for(var c=0;c<b.length;c++){for(var d in e.data[a][b[c]]){if(e.tables[a].indexes.hasOwnProperty(d)){var f=e.tables[a].indexes[d][e.data[a][b[c]][d]].indexOf(Number(b[c]));if(f>-1){delete e.tables[a].indexes[d][e.data[a][b[c]][d]][f]}}}if(e.data[a].hasOwnProperty(b[c])){delete e.data[a][b[c]]}}return b.length}function t(a,b,c){var d="",f=0;for(var g=0;g<b.length;g++){d=b[g];var h=c(x(e.data[a][d]));if(h){delete h["ID"];var i=e.data[a][d];for(var j in e.tables[a].indexes){if(h.hasOwnProperty(j)){delete e.tables[a].indexes[j][i[j]][d];e.tables[a].indexes[j][h[j]][d]=null}}for(var k in h){if(h.hasOwnProperty(k)){i[k]=h[k]}}e.data[a][d]=z(a,i);f++}}return f}function u(){localStorage[c]=JSON.stringify(e)}function v(){return JSON.stringify(e)}function w(a){throw new Error(a)}function x(a){var b={};for(var c in a){if(a.hasOwnProperty(c)){b[c]=a[c]}}return b}function y(a){return a.match(/[^a-z_0-9]/ig)?false:true}function z(a,b){var c="",d={};for(var f=0;f<e.tables[a].fields.length;f++){c=e.tables[a].fields[f];if(b[c]){d[c]=b[c]}}return d}function A(a,b){var c="",d={};for(var f=0;f<e.tables[a].fields.length;f++){c=e.tables[a].fields[f];d[c]=b[c]?b[c]:null}return d}var b="db_",c=b+a,d=false,e=null;e=localStorage[c];if(!(e&&(e=JSON.parse(e))&&e.tables&&e.data)){if(!y(a)){w("The name '"+a+"'"+" contains invalid characters.")}else{e={tables:{},data:{}};u();d=true}}return{commit:function(){u()},isNew:function(){return d},drop:function(){f()},serialize:function(){return v()},tableExists:function(a){return h(a)},tableCount:function(){return g()},createTable:function(a,b,c){var d=false;if(c===undefined){c=[]}if(!y(a)){w("The database name '"+a+"'"+" contains invalid characters.")}else if(this.tableExists(a)){w("The table name '"+a+"' already exists.")}else{var e=true;for(var f=0;f<b.length;f++){if(!y(b[f])){e=false;break}}for(var g=0;g<c.length;g++){if(b.indexOf(c[g])<0){w('The index "'+c[g]+'" does not bind with any field in the table')}}if(e){var h={};for(var f=0;f<b.length;f++){h[b[f]]=true}delete h["ID"];b=["ID"];for(var i in h){if(h.hasOwnProperty(i)){b.push(i)}}j(a,b,c);d=true}else{w("One or more field names in the table definition contains invalid characters.")}}return d},dropTable:function(a){i(a);k(a)},truncate:function(a){i(a);l(a)},rowCount:function(a){i(a);return m(a)},insert:function(a,b){i(a);return n(a,A(a,b))},update:function(a,b,c){i(a);var d=[];if(!b){d=r(a)}else if(typeof b=="object"){d=p(a,z(a,b))}else if(typeof b=="function"){d=q(a,b)}return t(a,d,c)},query:function(a,b,c){i(a);var d=[];if(!b){d=r(a,c)}else if(typeof b=="object"){d=p(a,z(a,b),c)}else if(typeof b=="function"){d=q(a,b,c)}return o(a,d,c)},deleteRows:function(a,b){i(a);var c=[];if(!b){c=r(a)}else if(typeof b=="object"){c=p(a,z(a,b))}else if(typeof b=="function"){c=q(a,b)}return s(a,c)}}}if(!Array.prototype.indexOf){Array.prototype.indexOf=function(a,b){if(b==null){b=0}else if(b<0){b=Math.max(0,this.length+b)}for(var c=b,d=this.length;c<d;c++){if(this[c]===a)return c}return-1}}