Last active
December 14, 2015 13:38
-
-
Save pjschreifels/5094359 to your computer and use it in GitHub Desktop.
#javascript #ORM manipulates a browser's Local Storage with create, update, increment, decrement and delete. Also provides functions for getting data with get, get all, find value, and find. The script treats the Local Storage like a database (as much as possible), so when manipulating the data, the function requires:
table - a string that refer…
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function createStore(table, row){ // Inserts a single row of data into a table | |
var rows = JSON.parse(localStorage.getItem(table)); | |
if (rows == null) { | |
rows = {}; | |
var id = 1; | |
} else { | |
var id = $.map(Object.keys(rows), function(val, i){return parseInt(val);}).reduce(function(a,b){return Math.max(a,b);})+1; | |
} | |
row.id = id; | |
rows[id.toString()] = row; | |
storeLocal(table, rows); | |
return id; | |
} | |
function updateStore(table, row, id){ // Updates a single row of a table by id | |
var rows = JSON.parse(localStorage.getItem(table)); | |
rows[id] = row; | |
storeLocal(table, rows); | |
} | |
function incrementValueStore(table, id, column, amount){ | |
var update = getStore(table, id); | |
var count = parseInt(update[table][0][column])+parseInt(amount); | |
update[table][0][column] = count; | |
row = update[table][0]; | |
updateStore(table, row, id); | |
} | |
function decrementValueStore(table, id, column, amount){ | |
var update = getStore(table, id); | |
var count = parseInt(update[table][0][column])-parseInt(amount); | |
update[table][0][column] = count; | |
row = update[table][0]; | |
updateStore(table, row, id); | |
} | |
function deleteStore(table, id){ // Deletes a single row of a table by id | |
var rows = JSON.parse(localStorage.getItem(table)); | |
delete rows[id]; | |
storeLocal(table, rows); | |
} | |
function getAllStore(table) { // Returns all rows from a table | |
var rows = JSON.parse(localStorage.getItem(table)); | |
var data = []; | |
if (rows != null) { | |
for(id in rows){ | |
data.push(rows[id]); | |
} | |
var result = {}; | |
result[table] = data; | |
return result; | |
} | |
return null; | |
} | |
function getStore(table, id){ // Returns a single row from a table by matching id | |
var rows = JSON.parse(localStorage.getItem(table)); | |
if (rows != null){ | |
var data = []; | |
data.push(rows[id]); | |
var result = {}; | |
result[table] = data; | |
return result; | |
} else { | |
return null; | |
} | |
} | |
function findStore(table, find){ // Find is an array of columns and values to return matching rows of a table. i.e. SHOW * WHERE column = value AND column = value, etc. | |
var rows = JSON.parse(localStorage.getItem(table)); | |
var data = []; | |
var i = 0; | |
$.map(find, function(){ i++; }); | |
for (var id in rows) { | |
var row = rows[id]; | |
var n = 0; | |
$.map(find, function(c,v){ | |
var mapthis = v + ":" + c; | |
$.map(row, function(c,v){ | |
var tothis = v + ":" + c; | |
if(mapthis == tothis){ | |
n++; | |
} | |
}) | |
}); | |
if ( i == n ){data.push(rows[id]);} | |
} | |
if (data.length == 0) { | |
return null; | |
} else { | |
var result = {}; | |
result[table] = data; | |
return result; | |
} | |
} | |
// This one needs an ID | |
function findValueStore(table, column){ // Returns the value of a column in a specified row of a table | |
var rows = JSON.parse(localStorage.getItem(table)); | |
var data = []; | |
for (var id in rows) { | |
var row = rows[id]; | |
for (var find in row) { | |
if (find == column){ return row[find]; } | |
} | |
} | |
} | |
function storeLocal(table, rows) { | |
try { | |
localStorage.setItem( table, JSON.stringify(rows) ); | |
} catch (error) { | |
if (error == QUOTA_EXCEEDED_ERR) { | |
alert('Quota exceeded!'); | |
} | |
} | |
location.reload(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment