Although I cut my “programming teeth” on functional database APIs, I happily gave them up years ago in favor of ORM—and I have no desire to go back.
Naturally, the iPhone a certain NDA-bound platform of indeterminate nature has no ORM layer for the bundled SQLite library.
So I was pretty thrilled to stumble onto Jeff LaMarche‘s SQLite Persistent Objects. Think Active Record, except instead of the database telling your model what it’s supposed to look like, the model tells the database what it should look like. Jeff calls the pattern “reverse Active Record”, but I think it looks a lot like Data Mapper.
The best part is that it works exactly like you’d expect:
person1.givenName = @"John";
person1.familyName = @"Doe";
person1.birthdate = [NSDate dateWithString: @"1908-11-24 04:12:00 -0800"];
[person1 save]; // Inserts a new row
[person1 release];
Person *person2 = [[Person alloc] init];
person2.givenName = @"John";
person2.familyName = @"Smith";
person2.birthdate = [NSDate dateWithString: @"1945-09-13 03:54:00 -0800"];
[person2 save]; // Inserts a new row
[person2 release];
// Oh wait, they both go by Jack
NSArray *people = [Person findByGivenName: @"John"];
for (Person *person in people) {
person.givenName = @"Jack";
[person save];
}
It’s still in the very, very early phases of development (it’s at revision 5), so there are some issues to work out, like what happens when the schema changes.
There are also currently a few bugs preventing SQLite Persistent Objects from, uh, compiling at all. I’ve submitted a patch (removed) that addresses these issues. I’ll update this entry when it’s been applied to trunk. (Update: It appears to have been patched.)
Regardless, it’s worth keeping an eye on, even if you don’t use it right away. Seriously, download the source and check it out.