Friday, December 17, 2010

Day 17: nice schema definition for DBIx::Class

This post is for DBIx::Class users only. :)

So, if you are using another ORM, you have to wait for tomorrow ...

As a side note: I was using Rose::DB::Object (RDBO) for years. But I'm slowly changing to DBIx::Class (DBIC). I really liked RDBO and it is a solid ORM. But the community strongly prefers DBIC and I wanted to gain experience with it.

Usually a schema definition looks like this:
package MyDB::Schema::Result::Artist;
use base qw/DBIx::Class::Core/;

use strict;
use warnings;

__PACKAGE__->table('artist');
__PACKAGE__->add_columns(qw/id name/);
__PACKAGE__->set_primary_key('id');
__PACKAGE__->has_many(cds => 'MyDB::Schema::Result::CD');

1;

With DBIx::Class::Candy it changes to:
package MyDB::Schema::Result::Artist;
use DBIx::Class::Candy;

table 'artist';
column id   => {data_type => 'int', is_auto_increment => 1};
column name => {data_type => 'varchar', size => 25};
primary_key 'id';

has_many cds => 'MyDB::Schema::Result::CD', 'id';

1;

I did not like all these __PACKAGE__ calls. This "Moose inspired" syntax is much nicer in my opinion.

Links:

8 comments:

  1. Fey was not around when I decided to use RDBO. I looked at Fey::ORM and after Dave's talk at the YAPC::EU 2010 I decided it's no fit for me. I do not remember the exact reasons for this, but I remember clearly me decision. I was glad to put a "checkmark" behind this topic. Especially because the full Moose integration was/is very attractive to me.

    ReplyDelete
  2. For what its worth, I use Moose with DBIC all the time and find it very useful for my result and resultset classes. I've actually used MooseX::Declare with that because I find the method signatures very useful for my domain logic.

    ReplyDelete
  3. John, can you point me to some example code?
    Thanks.

    ReplyDelete
  4. A good reason to stick with RDBO: https://code.google.com/p/rose/wiki/RDBOBenchmarks

    ReplyDelete
  5. Yeah, I know. RDBO is really fast. And it really is a very good ORM. I was a happy user all the time. There were a lot of bugs in the beginning, but John fixed them instantly. The documentation is very good, too.

    It's just that my priorities have changed. I will explain (and discuss) that in a later blog entry (when the Advent calendar is over).

    ReplyDelete
  6. Still waiting to know why you came to prefer DBIC over RDBO .. ;)

    ReplyDelete
  7. You are absolutely right. I promised (and until now failed to deliver) three things: an Advent calendar reflection, an update to my UUID benchmark and my motivation for switching to RDBO.

    In one sentence, I switched because DBIC is the community standard and nowadays I prefer mainstream over fringe group.

    I will try to express this a little better in a bigger blog post, but I will write them in above order.

    ReplyDelete