I'm new to javascript but not programming. I recently learned about local storage and love the idea of it. I'm creating a small website that let's me track my golf scores and other stats and I'm doing it 100% on the client side and storing the results in the local storage.
I work in databases all day long so I love that style more than the key/value that local storage offers so I set out to make a small library that converts local storage into a more DB structure. Thought I'd share with others to see what their thoughts are and maybe it'll be useful for someone.
- The table name is a key in the local storage
- The data is stored as an array of stringified JSON per table (key)
- The data is compressed and uncompressed when written to and read from local storage
- The first entry in an item is the table definition
Below is the usage with comments:
Code:
<script language="javascript" type="text/javascript">
$(document).ready(OnDocumentReady);
function OnDocumentReady() {
// create a database object which allows many database operations
var db = new LocalStorageDB();
// this is just for testing to clear the local storage while we test
db.TruncateAll();
// deletes the data in the players table
db.TruncateTable("players");
// check if the table exists
if (!db.TableExists("players")) {
// create a new table
var tblPlayers = new LocalStorageTable("players");
// add columns to the new table
tblPlayers.AddColumn("name");
tblPlayers.AddColumn("email");
// add the table to the database
db.AddTable(tblPlayers);
}
// insert some records
// first parameter is the table name to insert into
// second parameter is an object that has the same fields as the table definition
db.Insert("players", { name: "Bob", email: "bob@gmail.com" });
db.Insert("players", { name: "Joe", email: "joe@gmail.com" });
db.Insert("players", { name: "Rick", email: "rick@gmail.com" });
db.Insert("players", { name: "Mike", email: "mike@gmail.com" });
db.Insert("players", { name: "Kevan", email: "kevan@gmail.com" });
// this will update the name to Ricky where the name is currently Rick
// first param is table name
// second param is object of the fields you want to update with the value you want to update to
// third param is the where clause. a function that when the condition returns true, will update those records
db.Update("players", { name: "Ricky" }, function (v) { return v.name == "Rick"; });
// deletes when returns true in function
// first parameter is table name
// second parameter is the where clause. a function in where if the condition returns true the record will be deleted
db.Delete("players", function (v) { return v.name == "Mike"; });
// query recrods
// first param is the table name
// second param is the where clause. a function that when the condition returns true, will return that record as part of an array
var rsNames = db.Query("players", function (v) { return v.name == "Rick" && v.email == "rick@gmail.com"; });
// the result is an array of objects that match the table structure. check it's length to determine if anything was returned. access it's
// records via an index, and it's columns via the field name defined when the table was created
if (rsNames.length > 0)
var email = rsNames[0].email;
// erase the table completely. records and structure
db.DropTable("players");
}
</script>
Library is attached.