I wrote about an Active Record implementation for the Adobe Integrated Runtime using it's SQLite database functionality. I put up all the code on Google Code under the name AIR Active Record. Please check it out, let me know of bugs or features, or better yet, submit fixes and add-ons. If you're interested in being an active developer on it let me know.
Update:
To use the ActiveRecord (sorry for the lack of documentation), you need to extend it with a class for each table you'll use. For example, if I wanted an employee table I would create an Employee class like this:
{
import flight.db.activeRecord.ActiveRecord;
[RelatedTo(name="tasks", className="Task", multiple)]
public dynamic class Employee extends ActiveRecord
{
public var name:String;
public var position:String;
public var hireDate:Date;
public var salary:Number;
public var created:Date;
public var modified:Date;
}
}
{
import flight.db.activeRecord.ActiveRecord;
[RelatedTo(name="employee", className="Employee")]
public dynamic class Task extends ActiveRecord
{
public var employeeId:uint;
public var todo:String;
public var created:Date;
public var modified:Date;
public function Task(todo:String = null)
{
this.todo = todo;
}
}
}
In the metadata, the name is the property name which will be auto-generated and auto-populated (when you access it) for the relation. The className is the full class path (e.g. com.foo.Bar). multiple is a flag that specifies whether it's a one-to-many relationship. You can have many-to-many relationships as well.
If you aren't creating the database yourself there is a handy-dandy feature which will do it for you off of code introspection. Many-to-many is not supported for creation this way unless you have a class for that join table and run it on that.
TableCreator.updateTable(employee);
This will create the table if it doesn't exist, but it is also useful for updating the table if you've added new properties to the class. (right now it doesn't delete columns if you remove properties)
Then, you use it. You'll need to look at the code comments, or maybe generate the AS3 docs off of it to see all you can do, but this is some of what you can do (let's say you have an Employee class and a Task class):
employee.loadBy("username = ?", "bobTheBuilder");
// accessing tasks will autoload them from the database
for each (var task:Task in employee.tasks)
trace(task);
task = new Task("Call your mother");
employee.task.push(task);
employee.saveTasks();
employee.firstName = "Bobby";
employee.save();
var employee2:Employee = new Employee();
employee2.firstName = "Sue";
....
employee2.save();
trace(employee2.id); // new id
Pretty cool stuff. Maybe someone should document it! :D Anyone interested in figuring out and documenting super-cool code, please apply!
April 29th, 2008 at 11:24 pm
[...] Update: AIR Active Record is open sourced. [...]
June 7th, 2008 at 2:41 am
Hi, can u post an example of the usage of this library. Because I tried to integrate it in my project, but I falied. There where a lot of problem with the proxy implementation.
I really want to use this, but I still don’t get how to use it :$
Feel free to contact me to my email address.
Thanks :)
June 12th, 2008 at 6:46 pm
[...] went back and wrote up some very minimal documentation on using AIRActiveRecord on my post about it and remembered how cool it is. True, more documentation would sure be nice, and [...]
June 12th, 2008 at 9:06 pm
Cool! :)
June 13th, 2008 at 4:07 am
sweet implementation dude. I looked at proxies for this a while ago and its true that they are a bit problematic, but without pre-processors on the compiler there are few other choices.
Well done on this man a lot of people have been waiting for something like this.
June 13th, 2008 at 11:23 am
Thanks Campbell!
I’m looking forward to ActionScript 4 which should allow proxy capabilities on any classes, not just subclasses of proxy.