AIR ActiveRecord is Open Source

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:

package
{
	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;
	}
}
package
{
	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.

var employee:Employee = new Employee();
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):

var employee = new Employee();
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!

23 Responses to “AIR ActiveRecord is Open Source”

  1. AIR Active Record Says:

    [...] Update: AIR Active Record is open sourced. [...]

  2. Alvaro Says:

    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 :)

  3. AIRActiveRecord is cool Says:

    [...] 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 [...]

  4. Alvaro Says:

    Cool! :)

  5. Campbell Anderson Says:

    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.

  6. Jacob Wright Says:

    Thanks Campbell!

    I’m looking forward to ActionScript 4 which should allow proxy capabilities on any classes, not just subclasses of proxy.

  7. Sunil Says:

    Wow, you rock!

    ActiveRecord is the best thing since sliced bread. Everybody loves AIR. What a great combination…

  8. 360|Flex slides for “AIR SQLite: An optimization conversation”, a bunch of Words, punctuated - by Paul Robertson (http://probertson.com/) Says:

    [...] ActiveRecord source and blog post explaining its usage (Jacob [...]

  9. noirbizarre Says:

    Great tool!!
    Just what I was looking for!!

    I wanted to contribute, can you add me on the google code projet?
    I already have done some fix and enhancement (I can send you before comit).

  10. Doug Hays Says:

    First off, this ActiveRecord implementation for AIR has saved me a ton of time. So, thank you.

    But my question is this… I’ve just now bound an ArrayCollection of objects that extend ActiveRecord to a datagrid. The data appears just as I’d expect but something decidedly odd is happening. The roll-over highlighting and selecting on the datagrid is not working.

    When I switch to an ArrayCollection of objects that don’t extend ActiveRecord, it works fine.

    Has any one run into this issue? I’ve never seen anything like it in my years of Flex development.

    Thanks again!
    Doug.

  11. Jacob Wright Says:

    That is weird. I’ve not run into it, but I haven’t used the DataGrid with ActiveRecord either. You could step through the datagrid code on a roll over if you dare. :)

  12. Divmon Says:

    I’m playing with this in gumbo, when I try to get an array of the data in a table using findAll() I get an ‘Error #3137: An index specified for a parameter was out of range.’, details:’variable number must be between ?0 and ?-1′ error (I’m not using any params).

    There are two records in the DB.

    Any ideas or am I going the wrong way in getting an array (or ideally arraycollection) of the data in a table

    Cheers

  13. Divmon Says:

    Just commented out the params section in ActiveRecord.loadItems and findAll now works.

    Hmmmmn…

  14. Stef Says:

    Is this project still active?

  15. Jacob Wright Says:

    I’m not working on it, but if you’d like to take over/contribute you’re welcome to.

  16. Stef Says:

    Hello Jacob

    What is the difference between AirActiveRecord and AirOrm. As a contributor to AirOrm and project maintainer of AirActiveRecord, which one will you recommand?
    I’ve found a corrected one bug in ActiveRecord. If you can confirm that AirActiveRecord as an interest I will contribute.

    Stef

  17. Jacob Wright Says:

    AirOrm was a fork of AirActiveRecord and has been worked on more than mine. I would look into that, though you could take either as a starting point if you want.

  18. Stef Says:

    Hello Jacob,
    Can you give me access to the google code project?

  19. Osjunior Says:

    Hi Jacob

    I am using AIR Active Record for develop a AIR Flex project, i need to implement a many to many relationships ,but I don’t know how i can to do it, i would like that you show a example where you can to implement this relationships type

    Thanks

  20. Jacob Wright Says:

    Osjunior, to use a many-to-many relationship you should use the same metadata for a one-to-many (with “multiple” in the metadata).

    [RelatedTo(name="tasks", className="com.mycompany.Task", multiple)]

    Unfortunately the TableCreator doesn’t support this, but if you add a tablename1_tablename2 join table into your database it should pick it up and work correctly.

  21. Osjunior Says:

    Hi Jacob

    Thanks for you help,but i am having a problem when i try to implement any relation type as one to many in this case a create the tables

    //create table
    var user : User = new User();
    var org : Organization = new Organization();
    TableCreator.updateTable(user);
    TableCreator.updateTable(org);

    var org : Organization = new Organization();
    org.name = “Google”;
    org.created = new Date();
    org.save();

    var user : User = new User();
    user.age = 26;
    user.name = “Osjunior”;
    user.birthday = new Date();
    org.users.push(user);

    I got this message error :

    TypeError: Error #1009: Cannot access a property or method of a null object reference.
    at flight.db.activeRecord::ActiveRecord/http://withincode.com/sql/db::getDBProperties()[/Users/jacob/Workspaces/gumbo/Stealth/AIRActiveRecord/src/flight/db/activeRecord/ActiveRecord.as:538]
    at flight.db.activeRecord::ActiveRecord/save()[/Users/jacob/Workspaces/gumbo/Stealth/AIRActiveRecord/src/flight/db/activeRecord/ActiveRecord.as:135]
    at Controllers::EstudianteController/btn_create()[/Users/oscar/Desktop/Example/Android/src/Controllers/OrganizationController.as:64]

    Where the line 64 on the OrganizationController is when i tried to create the Organization

    org.save();

    This problem happen the firts time, when there not database and it is created by firts time, but i close the app and reopen the app again i don’t get this error.

    I need to help, i don’t know which can to be the problem

    Thanks.

  22. Jacob Wright Says:

    The error is happening because the getSchema call returns null. It can’t find the newly created table in the database. Perhaps the connection is asynchronous or the schema is cached. Try creating the tables in one function then later create the objects. I’m not sure without taking more time to look into it.

    // create tables
    … code here …

    setTimeout(createObjects, 1000); // create the objects in this method 1 second later

  23. Prashant Says:

    Hey, I am getting error when calling find(id:unit) method on the active record object…
    TypeError: Error #1034: Type Coercion failed: cannot convert Object@a4f4e99 to flight.db.activeRecord.ActiveRecord.
    Also, would like to tell you that i started developing something like this a month ago..I called it ActiveObject….but when i found about your ActiveRecord…i was fascinated..and started using it..also i have built a wrapper around it that works like rake db:drop rake db:create and migrate in rails….But currently i am stuck with this error…I also tried creating objects with class factory…but that turned out into some new errors..
    Thanks for making it open source anyways

Leave a Reply