-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathlocaldb.min.js
13 lines (12 loc) · 2.99 KB
/
localdb.min.js
1
2
3
4
5
6
7
8
9
10
11
12
13
/*
localDB © 2012 Michael Donaldson
Simple localStorage Database
Version: 0.2.1
License: MIT (http://opensource.org/licenses/MIT)
*/
function localdb(g){void 0===localStorage[g]&&(localStorage[g]=JSON.stringify({}));var b=JSON.parse(localStorage[g]),e=function(a){localStorage[g]=JSON.stringify(a);return!0},l={deleteDatabase:function(a){if(void 0!==localStorage[a])return delete localStorage[a],!0;console.error('A database with the name "'+a+'" could not be found');return!1},createTable:function(a){if(void 0===b[a])b[a]={totalrows:0,autoinc:1,rows:{}},e(b);else return console.error('A table with the name "'+a+'" already exists'),
!1},dropTable:function(a){if(void 0!==b[a])delete b[a],e(b);else return console.error('A table with the name "'+a+'" could not be found'),!1},tableMeta:function(a){if(void 0!==b[a])return{totalrows:b[a].totalrows,autoinc:b[a].autoinc};console.error('A table with the name "'+a+'" could not be found');return!1},insert:function(a,c){void 0===b[a]&&l.createTable(a);autoinc=b[a].autoinc;c.ID=autoinc;b[a].rows[autoinc]=c;b[a].autoinc+=1;b[a].totalrows+=1;e(b)},update:function(a,c,f){if(void 0!==b[a]){var d=
b[a].rows;for(row in d)if(null!==d[row]){var m=0;for(i in f)b[a].rows[row][i]!=f[i]&&(m=1);if(0==m){for(i in c)b[a].rows[row][i]=c[i];e(b)}}}else return console.error('A table with the name "'+a+'" could not be found'),!1},updateById:function(a,c,f){if(void 0!==b[a]){for(i in c)b[a].rows[f][i]=c[i];e(b)}else return console.error('A table with the name "'+a+'" could not be found'),!1},remove:function(a,c){if(void 0!==b[a]){var f=b[a].rows;for(row in f)if(null!=f[row]){var d=0;for(i in c)b[a].rows[row][i]!=
c[i]&&(d=1);0==d&&(delete b[a].rows[row],e(b))}}else return console.error('A table with the name "'+a+'" could not be found'),!1},removeById:function(a,c){if(void 0!==b[a])if(null!==b[a].rows[c])delete b[a].rows[c],e(b);else return console.error("The specified row could not be found"),!1;else return console.error('A table with the name "'+a+'" could not be found'),!1},find:function(a,c,f,d,e){e||(e="AND");if(void 0!==b[a]){var h=b[a].rows,k=[],g=1;for(p in c){g=0;break}if(0==g)for(row in h){h=0;if("AND"==
e.toUpperCase())for(i in c)if(b[a].rows[row][i]==c[i])h=1;else{h=0;break}else if("OR"==e.toUpperCase())for(i in c)if(b[a].rows[row][i]==c[i]){h=1;break}1==h&&k.push(b[a].rows[row])}else for(row in h)k.push(b[a].rows[row]);d?"number"!=typeof d&&(d=0):d=0;return"number"==typeof f?0<f?k.slice(d,f+d):k.slice(d):k}console.error('A table with the name "'+a+'" could not be found');return!1},findById:function(a,c){if(void 0!==b[a]){if(void 0!==b[a].rows[c])return b[a].rows[c];console.error("The requested record could not be found");
return!1}console.error('A table with the name "'+a+'" could not be found');return!1},dbExists:function(a){return void 0!==localStorage[a]?!0:!1},tableExists:function(a){return void 0!==b[a]?!0:!1},exportData:function(a){if(a){if(void 0!==b[a])return JSON.stringify(b[a]);console.error("Table not found");return!1}return JSON.stringify(b)}};return l};