why is required to use a helper to use DBIx::Connector?

39 views
Skip to first unread message

Alceu Rodrigues de Freitas Junior

unread,
May 13, 2017, 4:22:46 PM5/13/17
to Mojolicious
Hello folks,

My name is Alceu and I'm giving baby steps with Mojolicious.

I just got an helping hand in the IRC channel to make the code below to work, but I would like to understand better the details.

When I using "bare" DBIx::Connector, right after I created a new instance of it, it would go straight to the DESTROY block... and all I would get was a empty reference.

After adding it as a helper ("helper conn" below), it worked as expected.

Why is it required to use a helper for that? I wasn't able to find any documentation about it.

Considering that I will need to use this connection for my Models, what is the best practice to use it?

The code follows below... thanks!

use Mojolicious::Lite;
use Mojolicious::Validator;
use Set::Tiny 0.04;
use File::Spec::Functions qw( catdir catfile );
use File::Share 0.25 'dist_dir';
use Mojolicious::Plugin::Config;
use Carp;
use DBIx::Connector;
use Data::Dumper;

my $config = {
    dsn  => "dbi:mysql:metabase;host=127.0.0.1;port=30306",
    user => 'cpantesters',
    pass => '',
};

helper conn => sub {
    state $conn = DBIx::Connector->new( $config->{dsn}, $config->{user},
        $config->{pass}, { RaiseError => 1, AutoCommit => 1 } );
    $conn->mode('fixup');
    return $conn;
};

get '/' => sub {
    my $c   = shift;
    my $ref = $c->conn->run(
        fixup => sub {
            my $query = q{SELECT * FROM cpanstats.osname};
            my $sth   = $_->prepare($query);
            $sth->execute();
            return $sth->fetchall_arrayref;
        }
    );

    $c->render( text => Dumper($ref) );
};

app->start;


sri

unread,
May 13, 2017, 5:28:15 PM5/13/17
to Mojolicious
Considering that I will need to use this connection for my Models, what is the best practice to use it?

As i told you on IRC, the recommended modules Mojo::Pg/SQLite... actually contain example apps to demonstrate best practices.


--
sebastian 

Alceu Rodrigues de Freitas Junior

unread,
May 13, 2017, 7:06:51 PM5/13/17
to mojol...@googlegroups.com

Hello Sebastian,

Thanks for answering again, I think I missed your post in the IRC channel.

I guess the example that we are talking about would be:

# Model in Blog package
  $self->helper(pg => sub { state $pg = Mojo::Pg->new(shift->config('pg')) });
  $self->helper(
    posts => sub { state $posts = Blog::Model::Posts->new(pg => shift->pg) });

In the model (which uses Mojo::Base, which I understand is not a requirement, but useful), I just pass the reference stored by the pg (DB) helper.

And looks like that's it... the model doesn't have to do anything else. It seems the DB handle is implicit managed by Mojo::Pg::Database (including the connection), so it does look like what DBIx::Controller does under the hood. And probably that's why I wasn't being able to use it with Mysql.

So, my guess is that if I need to use a different DB supported by DBI, I would need to make sure the equivalent of Mojo::Pg::Database would need to implement a DESTROY block and wrap a instance of it around with the a helper.

Did I get it correctly?

Thanks!

Alceu

--
You received this message because you are subscribed to a topic in the Google Groups "Mojolicious" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/mojolicious/LgXKq2KA_HE/unsubscribe.
To unsubscribe from this group and all its topics, send an email to mojolicious...@googlegroups.com.
To post to this group, send email to mojol...@googlegroups.com.
Visit this group at https://groups.google.com/group/mojolicious.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages