Google Gears ORM v0.2

UPDATE:a new version is here.

New features in this version:

  • transactions - used inside GearsORM itself
  • self relations(i.e. a relation to the same table/model) - very useful when working with Tree-like structures in the database
  • count method - so now u can do Person.count() instead of Person.select().toArray().length

The most important update in this version is of course transaction support.
SQLite is known for really bad write preformence when not working with transactions.
While developing transaction support for GearsORM i had to write a test,in that test it execute 100 inserts and 100 updates,this test take about 15 seconds for the inserts and about 10 seconds for the updates without transactions,when using transactions for each set it takes about 377ms for the inserts and 200ms for the updates that is about 39 times faster!

To use transactions just wrap all the code that you want to be in a transaction into a function and call GearsORM.Transaction with that function like this:

GearsORM.Transaction(function()
{
  new Person({name:"John"}).save();
  new Person({name:"Doe"}).save();
});

This will execute a BEGIN query before running the function and a COMMIT query if everything went ok ,else it will execute ROLLBACK.
This make all the queries between BEGIN to COMMIT to run in a transaction and to run faster :)

Self relations are a great way to use a tree structure in a database,this is how it look:

var TreeNode = new GearsORM.Model({
	name:"TreeNode",
	fields:
	{
		data:new GearsORM.Fields.String({maxLength:25}),
		parent:new GearsORM.Fields.OneToMany(
                          {related:"TreeNode",allowNull:true}),
		childs:new GearsORM.Fields.ManyToOne(
                         {related:"TreeNode"})
	}
});

this model have both ManyToOne and OneToMany,the only required is OneToMany since it is a db field,the ManyToOne comes only to provide a easy way to get the child nodes.
one new option added to OneToMany is the allowNull this add a way to say that it is not related to any one,this is very important with self relations since the first row inserted to a table with self relation can`t be related to any row because there is no rows!
lets create a few nodes:

var root = new TreeNode({data:"i am the root",parent:null}).save();
var node1 = new TreeNode({data:"i am node 1",parent:root}).save();
var node2 = new TreeNode({data:"i am node 2",parent:root}).save();

in this example we have the root node which doesn`t have a parent,node1 and node2 both have a parent which is root.
this means that both node1 and node2 are childs of root.
getting the childs of root is like using a normal ManyToOne relation:

root.childs.select().each(function(node)
{
 console.log(node.data);
});

this will output to the console(if using firebug):

i am node1 i am node2.

we can also delete from a self relation:

root.childs.remove("data = ?",["i am node1"]);

this will remove node1 but it will make sure that node1 was related to root,if there was another node that had data set to i am node1 it wouldn`t be deleted since it is not a child of root.

a now for count,count is method to retrive how much objects(i.e. rows) match a query.
for example Person.count() will return how much pepole there is in the database,count can also use a where clause,like this:

Person.count("lastName = ?",["Katz"])

this will return the number of pepole that have the lastName set to Katz.
the difference between doing Person.select().toArray().length to using Person.count() is that count use the COUNT sql function therefore it is alot more efficient than doing a select then creating a array and then getting the count.

link to packed version here.
more information in the project homepage.
feature requests are always welcome :) (as well bug reports)

Leave a comment