Google Gears ORM

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)

but then i thought:"damn i hate to write sql",so i started to write a simple ORM something like SQLObject but way simpler.

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", { firstName:GearsOrm.Fields.String({maxLength:25}), lastName:GearsOrm.Fields.String({maxLength:25}) });

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: Person.createTable();

now for some real work,to insert a row to the database: var myself = Person.insert(["Uriel","Katz"]); alert(myself.firstName); alert(myself.lastName);

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 =; var current; while(current = { alert(current.firstName + ":" +current.lastName); }

what 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:"firstName = 'Uriel'") or using bind variables(recommended,since it auto quote the values and prevent SQL injection attacks):"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.
NOTE: delete_ is used instead of delete since it is a keyword in JavaScript.

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.

a simple demo is here and source code here.

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";


  1. MP:Schorsch Jun 3

    yeah way to go. i was ofline during the weekend and directly thought of doing something like this for gears. i´ll look at your solution. thanks

  2. Uriel Katz Jun 3

    no problem,if you have any comments or suggestions how to make this better please post them :)

  3. Jeff Triplett Jun 4

    I spent my weekend playing with Gears and I was wondering who would be first to release such a library. Congrats and good job! If you added the ability to insert straight from JSON then that would be awesome.

  4. Uriel Katz Jun 4

    that can be done easily,first i want to get relations working,and also add to this blog RSS and proper titles :)

  5. PatrickQG Jun 5

    I'm going to have a look at this tonight - it would go quite nicely with my Gearshift ( idea.

  6. Uriel Katz Jun 5

    i was thinking about this problem also,it will be smart to integrate it with the models so it can detect how much different is the database comparing it to the model. also i see you have a schema_info table that could be easily a model so you don`t need to mess with the sql.

  7. Amit Aug 27

    It is not working with Internet Explorer 7 and 8.

  8. Uriel Katz Aug 27

    this is a very old version,check the new rewritten GearsORM,JStORM