UPDATE:Adobe AIR support is in svn trunk
I am happy to announce the first beta of JStORM,a complete rewrite of GearsORM to support multiple backends,currently supporting Jaxer(MySQL and Sqlite) and Google Gears(Sqlite).
This version is a complete rewrite of GearsORM,it changed the way it works internally and also the API.
The first change is that every model have it own connection to the database,this is in order to enable different models connected to different databases.
Another interesting change is the JStORM.Query class,this class allows for easily querying the models.
a query instance can be obtain by calling the all method of the model,lets define a simple model:
var Person = new JStORM.Model({
name:"Person",
fields:
{
firstName:new JStORM.Field({type:"String",maxLength:25}),
lastName:new JStORM.Field({type:"String",maxLength:25}),
},
connection:"default"
});
For example to print all the persons in the database:
Person.all().each(function(person)
{
console.log(person.firstName);
});
What the all function do is to create a new instance of JStORM.Query for Person model.
Other function that return a query is filter,filter return a query filtered by a criteria,for example:
var katzFamily = Person.filter("lastName = ?","Katz");
katzFamily.each(function(person)
{
console.log(person.firstName);
});
A thing to note about Query is that it is lazy evaluated,so invoking the method filter or all doesn`t query the database until you start iterating over the result.
Queries can be filtered many times to create a AND expression between criterias(note that each call to filter create a new query object):
var uriels = Person.filter("firstName = ?","Uriel");
var urielKatzs = uriels.filter("lastName = ?","Katz");
console.log("People with first name Uriel: " + uriels.count());
console.log("People with name Uriel Katz: " + urielKatzs.count());
Query objects have various methods for example the count method:
var numberOfMembers = Person.filter("lastName = ?","Katz").count();
console.log("Katz family have " +numberOfFamilyMembers + " members");
The count method return the number of objects that matched the criteria of the Query object,it use the SQL COUNT function.
There is also the remove method,which delete all the objects that match the criteria:
Person.filter("firstName = ?","Uriel").remove();
The remove method uses the SQL DELETE statment.
This early release is to show the new api and support for Jaxer,there is some stuff lacking that were in GearsORM:
- Many to many relations - still can be done but need to define tables manually(will be added in 0.4)
- Foreign key constrains - this are a pain in Sqlite since they are implemented with triggers,i am considering dropping support for this
I can`t show the all api in this post,currently there is no documentation,so you can see examples for api usage in the unit tests.
In the next weeks i will focus on testing,writing documentation and improving JStORM for final release, keep checking the blog for updates on JStORM.
links:
P.S.:As always bug reports and fixes are welcome,also if anyone wants to help either with the code and documentation(especially in this,since my english isn`t that good).