Archive for April, 2009

You are currently browsing the memamsa archives for April, 2009.


Make your S’s

I managed to catch in one of the last weekends of skiing at Mount Hood.

I have skied a total of 6 days in the last 10 years, and I went for the first time on the “blue” slope. To a greenie, the slope is intimidatingly steep and interminably long. After the first loss of control at high speed, you realize why they call it a “wipeout”.

Once at the top of the mountain, in such a situation, you only have two dignified alternatives:

  1. Get down on your butt all the way.
  2. Fall down on your butt a few times.

I chose the latter because it is faster, and its the only way to get better.

And getting better means retaining control, which requires zig-zagging, which at that speed really requires right techniques, which means you focus and put all effort into the next S-curve, and then the next, and the next, until suddenly you are coasting at the bottom to queue again for the ski lift to take you up.  

Make all the effort you can to make things effortless. 

Designing an ActionScript ORM

How do we get the best of DataMapper and ActiveRecord within the constraints of ActionScript?

First off, the design goals and desires:

  1. Easily create persistent objects that map to database storage.
  2. Flexible methods to abstract data manipulation and queries.
  3. Model DB associations through object properties and relations.
  4. Support run-time schema extensibility (migrations)

ActionScript is dynamic, object-oriented, and does have support for limited introspection. It also imposes several restrictions such as strong typing, Java-style single class inheritance and the concomitant Class vs. Interface distinction.

Items 1) and 2) are straightforward using object inheritance.

import com.memamsa.datamapper.*;
dynamic public class Post extends Modeler {
   // Post objects are now DB models.
}

Being a data model automatically allows Post objects to do database operations. There is one weakness, if you are a purist. Since ActionScript does not allow static methods of a class to be inherited, some methods (such as find and create) which make sense as class level static methods end up having to be defined as object methods.

For associations, I use reflection using a combination of class meta-data and the describeType functionality within the Flex framework. The declaration itself is straightforward.

[Association(name="comments", className="Comment", type="has_many")];
dynamic public class Post extends Modeler {
}

Which means, you can now add, access, and update comments associated with the Post objects like so:

var post:Post = new Post();
// add a new comment to the post
post.comments.push(new Comment({author: 'cooldude', text: '1st'}));
// how many comments do we have?
post.comments.list().length;
// find comments matching criteria
post.comments.find({conditions: 'author like %cool%', order: 'created_at ASC'});

I really like Datamapper style declaration of fields and properties within the class definition. ActiveRecord does better with schema versioning and the ability to migrate selectively without loss of data and with  optional data transforms. The ability to extend the schema during development, or as part of application updates is invaluable. This is how we handle it in our ActionScript ORM.

import com.memamsa.datamapper.*;
dynamic public class Post extends Modeler {
  // describe the migrations
  // The Migrator takes this class references, options and an array of
  // migration functions.
  private static const migrations:Migrator = new Migrator(
    Post,
    {id: true},
    [
      function(my:Migrator):void {
        my.createTable(function():void {
          my.column('title', DB.Field.VarChar, {limit: 128});
          my.column('author', DB.Field.VarChar, {limit: 40});
          my.columnTimestamps();
        }
      }
    ]);
    //.. other class methods and properties...
}

When necessary, additional migration functions can be added to the array of migrations. The Migrator tracks schema versions on a per-table basis to make sure changes are automatically reflected in the schema.

Finally, we utilize the ECMAScript prototype mechanism to set schema properties on each of the Modeler sub-classes, to allow usage such as this:

var p:Post = new Post();
p.load({id: 2});
p['title'] = 'New Title';
p.save();

One more thing, add the following compiler setting.

  • -keep-as3-metadata+=Association

And we now have a usable ORM for ActionScript.

Hello world!

First off, thanks to the Wordpress team! You guys are showcasing the power of continuous improvement. Wordpress keeps getting better, while making it easier to note down and share thoughts and ideas. 

This is it. First post. Expect more. 

Namaste