UPDATE:a new version is here.
i saw Google Gears and i thought :"nice finally i have some kind of client side storage in browsers other than Firefox".(i know that only IE and Firefox are supported,but safari support is on the way,and in gears group they have been talking about opera support,also this is a open source project)
this short script give you a way to declare your tables as objects and provide a nicer OO interface than the normal Google Gears api.
so to define a table you do it like this:
var Person = new GearsOrm.Model("Person",
the first argument is the table name to use in the db.
the second argument is a object that define all the fields in the table, the keys are the field names and the values are the field types.
there 4 field types: Integer,Float,TimeStamp and String.
each of this get a object which define sql properties:
- notNull - if this is set to true the field will be appended with "NOT NULL",meaning that the field cannot accept the value null
- defaultValue - will use the value of this option as the default value of the field using the "DEFAULT" sql syntax
- maxLength - this is used with String field type to define the max length of the field
- unique - if this is set to true the field will be appended with "UNIQUE",meaning that values of this field will be unique
now that we have our tables defined,we need to create them,no problem just call the createTable() function of the model and the table will be created for you(if it is already created it won`t do anything),like this:
now for some real work,to insert a row to the database:
var myself = Person.insert(["Uriel","Katz"]);
that simple!,this will insert a Person with first name "Uriel" and last name "Katz" and will return a object
that have firstName set to "Uriel" and lastName set to "Katz".
now that we have something in the db we can retrive it using select method of the model:
var persons = Person.select();
while(current = persons.next())
alert(current.firstName + ":" +current.lastName);
what Person.select() do is to return a object with a function next(like a iterator in some languages), when calling to next it return a person object,when there is no more persons to retrieve it will return null and by that will stop the while loop.
select can also get a expression that will be used as the WHERE in the sql query,like this:
Person.select("firstName = 'Uriel'")
or using bind variables(recommended,since it auto quote the values and prevent SQL injection attacks):
Person.select("firstName = ?",["Uriel"])
now for delete,delete works like select but it must get a expression,let say we want to delete all the pepole with last name Cats(common misspell of the name Katz):
Person.delete_("lastName = ?",["Cats"])
this will delete all the Persons(i.e. all the rows in the table Person) that have their last name is Cats.
the model have also a function called dropTable that will drop the table if it exists.
note that there is no support for relations but that can be add easily.
NOTE: by default GearsOrm use the database name "gears.orm" if you want to use different name just add a line just after the include of gears_orm.js like this:
GearsOrm.dbName = "myDataBaseName";