Monday, April 14th, 2008
Joose expands with new ORM
Malte has continued to work on Joose, his meta object system for JavaScript. He has added a lot of documentation, including cookbooks that tell the story nicely. He also told us about a feature that is near and dear to my Google heart:
Joose now includes a simple object relational mapper for the Gears database in the examples section. If you have Gears installed, you can run it.
It will run its test suite and then display a class browser that displays the contents of a table for classes that represent a database
tables. The example has two entities (Car and Person in the MyEntities module).Declaring the entities with this proof-of-concept OR-mapper looks like
this:JAVASCRIPT:
Module("MyEntities", function (m) { Class("Car", { isa: ORM.Entity, has: { owner: { metaclass: ORM.HasOne, isa: function () { return m.Person } } }, classMethods: { tableName: function () { return "car" } } }) Class("Person", { isa: ORM.Entity, classMethods: { tableName: function () { return "person" } }, has: { mother: { metaclass: ORM.HasOne, isa: function () { return m.Person } }, cars: { metaclass: ORM.HasMany, isa: function () { return m.Car }, foreignKey: "owner" } } }); });And some example usage:
JAVASCRIPT:
// Create the mother var mother = new MyEntities.Person(); mother.name("elke"); mother.city("Elmshorn"); mother.save(); // Create the son var person = new MyEntities.Person(); person.name("malte"); person.city("Hamburg"); person.mother(mother); // set the mother person.save(); // Give the son 10 cars :) for(var i = 0; i <10; i++) { var car = new MyEntities.Car(); car.model("3."+i); car.brand("bmw"); car.owner(person); car.save(); } // refetch the person from the db var personFromDb = Entities.Person.newFromId(person.rowid()); alert(personFromDb.mother().name()) // will alert 'elke' alert(personFromDb.cars()[0].brand()) // will alert 'bmw'












Leave a comment