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

23f4d5d797a91b6d17d627b90b5a42d9?s=128

Kentaro Kuribayashi

March 17, 2012
Tweet

Transcript

  1. 3.
  2. 4.
  3. 11.

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

    Each Other http://martinfowler.com/eaaCatalog/dataMapper.html
  4. 12.

    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
  5. 22.

    Adapter Retrieves Data from Somewhere and Returns It as a

    HashRef Data Hash-based Object Knows Schema Info
  6. 23.

    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()
  7. 24.
  8. 25.

    my $handler = DBIx::Handler->new(...); my $adapter = Data::Mapper::Adapter::DBI->new({ driver =>

    sub { $handler->dbh } }); my $mapper = My::Mapper->new({ adapter => $adapter }); Instanciate
  9. 27.

    my $data = $mapper->create(user => { name => 'kentaro', age

    => 34 }); #=> $data is a Data::Mapper::Data- based object Create
  10. 28.

    $data = $mapper->find(user => { name => 'kentaro' }); #=>

    $data is a Data::Mapper::Data- based object Retrieve
  11. 29.

    $result = $mapper->search(user => { age => 34 }, {

    order_by => 'id DESC' }); #=> $result is an ArrayRef of Data::Mapper::Data-based object Search
  12. 30.

    # Updates params like below $data->param(age => 35); my $sth

    = $mapper->update($data); $sth->rows; #=> 1 Update
  13. 32.

    # 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
  14. 34.

    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
  15. 36.

    # 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)
  16. 37.

    # 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)
  17. 38.

    # 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)
  18. 41.

    Data::Mapper: Recap • Is a Simple ORM • Just Defines

    Minimum Conventions • Deals with Any Data from Anywhere