Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Data::Mapper

 Data::Mapper

My presentation about Data::Mapper at Kyoto.pm #1 on 2012-03-17. Data::Mapper is a simple ORM and a implementation of Data Mapper Pattern described in PofEAA.

Data::Mapper is available at https://github.com/kentaro/data-mapper

Kentaro Kuribayashi

March 17, 2012
Tweet

More Decks by Kentaro Kuribayashi

Other Decks in Technology

Transcript

  1. Maps Data Between DB and In-memory Data Isolating Them from

    Each Other http://martinfowler.com/eaaCatalog/dataMapper.html
  2. An object that wraps a row in a database table

    or view, encapsulates the database access, and adds domain logic on that data. http://martinfowler.com/eaaCatalog/activeRecord.html
  3. Adapter Retrieves Data from Somewhere and Returns It as a

    HashRef Data Hash-based Object Knows Schema Info
  4. Table <-> Data user <-> User user_info <-> UserInfo Table

    <-> Data Class user <-> D::M::Data::User user_info <-> D::M::Data::UserInfo (Customizable by D::M#data_class()
  5. my $handler = DBIx::Handler->new(...); my $adapter = Data::Mapper::Adapter::DBI->new({ driver =>

    sub { $handler->dbh } }); my $mapper = My::Mapper->new({ adapter => $adapter }); Instanciate
  6. my $data = $mapper->create(user => { name => 'kentaro', age

    => 34 }); #=> $data is a Data::Mapper::Data- based object Create
  7. $data = $mapper->find(user => { name => 'kentaro' }); #=>

    $data is a Data::Mapper::Data- based object Retrieve
  8. $result = $mapper->search(user => { age => 34 }, {

    order_by => 'id DESC' }); #=> $result is an ArrayRef of Data::Mapper::Data-based object Search
  9. # Updates params like below $data->param(age => 35); my $sth

    = $mapper->update($data); $sth->rows; #=> 1 Update
  10. # Data to Table sub mapped_params { my ($self, $data)

    = @_; my $table = $self- >to_table_name($data); my $schema = $self->adapter->schemata- >{$table} or Carp::croak("no such table: $table"); # snip } Schema
  11. sub create { my ($self, $name, $args) = @_; my

    $content_type = File::MimeInfo::Magic::mimetype($args->{filename}); # $self->driver is a Fule::S3 object my $res = $self->driver->create_object_from_file( $name, $args->{key}, $args->{filename}, { content_type => $content_type, 'x-amz-acl' => $args->{acl}, } ); +{ bucket => $name, key => $args->{key}, type => $content_type, } } Adapter for External API
  12. # Mapper sub map_data { # snip ... my $obj

    = $data_class->new($data); $obj->SOMESPECIALMETHOD($self) if $obj->isa('Data::Mapper::Data'); $obj; } # Data sub SOMESPECIALMETHOD { my ($self, $mapper) = @_; $self->{author} = $mapper->find(author => { id => $self->param('author_id') }); } Relation (Idea 1)
  13. # Data sub mapper { my ($self, $mapper) = @_;

    $self->{_mapper} = $mapper if defined $mapper; $self->{_mapper}; } # Mapper sub map_data { my $self = shift; my $data = $self->SUPER::map_data(@_); $data->mapper($self); $data; } Relation (Idea 2)
  14. # Data sub mapper { my ($self, $mapper) = @_;

    $self->{_mapper} = $mapper if defined $mapper; $self->{_mapper}; } # Mapper sub map_data { my $self = shift; my $data = $self->SUPER::map_data(@_); $data->mapper($self); $data; } Relation (Idea 2)
  15. Data::Mapper: Recap • Is a Simple ORM • Just Defines

    Minimum Conventions • Deals with Any Data from Anywhere