Sunday 11 March 2012

Javascript JSON Database

This is a simple Class to create a JSON structured database. This class creates  a JSON object and populates it via the methods provided.


Easy Helper methods to CREATE, UPDATE, ADD, DELETE and REMOVE data.


Below I will show an example of a database create and spits out the JSON as a String. Also I will Explain each function.


Live Example: Demo

JS file on Github: https://github.com/fahimc/Utensil/tree/master/lib/com/data/database


Full Example:

        Database.create();
Database.addTable("newTable");
Database.addProperty("newTable","id");
Database.addProperty("newTable","name");
Database.addProperty("newTable","email");
Database.addData("newTable",{id:"123456"});
Database.addData("newTable",{id:"2222",email:"asas@asas.com"});
Database.updateDataByValue("newTable",{email:"fahim@gmx.com"},{id:"123456"});
Database.updateDataByValue("newTable",{email:""},{email:"asas@asas.com"});
document.body.innerHTML+= Database.toString();



Output:
{"newTable":{"props":["id","name","email"],"data":[{"id":"123456","name":"","email":"fahim@gmx.com"},{"id":"2222","name":"","email":""}]}}


METHODS


Creating a Database: This function creates a new database.
 Database.create();


Creating a new Table: This function creates a new table. You provide a unique name for the table.
Database.addTable("newTable");


Adding a new property to a Table:  you need to provide the table name and the property name.
Database.addProperty("newTable","id");


Adding data to a Table:  provide the table name and then in an object provide the values for each property. You can provide values for all the properties or just one.
Database.addData("newTable",{id:"123456"});


Updating data by Values: State the table name, an object with the property/properties and value/s and then provide another object with properties and values to find the data you wish to update.
Database.updateDataByValue("newTable",{email:"fahim@gmx.com"},{id:"123456"});


Clear the whole Database: execute this function.
Database.clearDatabase();



Deleting a Table: execute this function with the table name,
Database.deleteTable('Table Name');



Get a Table: gets the table as an Object.
Database.getTable('Table Name');



Remove data by value: remove a data row by a value. provide the table name and an object with properties and values to match.
Database.removeDataByValue('Table Name',{prop:value});



Get data by value: get a data row by a value. provide the table name and an object with properties and values to match.
Database.getDataByValues('Table Name',{prop:value});



Delete all Table Data: deletes all data in a table. Provide the table name.
Database.deleteData('Table Name');



Remove data by Index: remove a data row by an index. Provide the table name and the index you wish to remove.
Database.removeDataByIndex('Table Name',index);



Database to String: returns the JSON as a String. If you want all the Data in the database do not provide a table name but if you want a specific table then provide the name of the table.
Database.toString('Table Name');








No comments:

Post a Comment