How to pass Mojo::Pg db handler to controller? (mojolicious)

Viewed 119

I try to experiment with Mojolicious and decided to try Mojo::Pg. But I can't pass db (not pg) handler to the controller with the helper. App:

use Mojo::Base 'Mojolicious', -signatures;
sub startup ($self) {
  my $pg = Mojo::Pg->new("postgresql://$config->{db_user}:$config->{db_pass}\@/$config->{db_db}");
  my $db = $pg->db;

  $self->helper( db =>
    sub {
      print STDERR Dumper($db);
      return $db;
    }
  );
}

It actually returns db handler:

$VAR1 = bless( {
                 'pg' => bless( {
==================== here's connection info and other staff =======
                                }, 'Mojo::Pg' ),
                 'dbh' => bless( {}, 'DBI::db' )
               }, 'Mojo::Pg::Database' ); 

At controller:

sub welcome ($self) {
  $self->log->debug(Dumper($self->db));
}

gives me

$VAR1 = bless( {
                 'pg' => undef,
                 'dbh' => bless( {}, 'DBI::db' )
               }, 'Mojo::Pg::Database' );

If I pass just $pg handler (not db) and do $pg->db inside the controller, it works just fine. But I just don't want to do it at every controller. Is there anything I can do about it?

1 Answers

It seems to work fine here. I generated a new Mojolicious app with mojo generate app My::App, then edited lib/My/App.pm:

package My::App;
use Mojo::Base 'Mojolicious', -signatures;
use Mojo::Pg;
use Data::Dumper qw(Dumper);

# This method will run once at server start
sub startup ($self) {

  # Load configuration from config file
  my $config = $self->plugin('NotYAMLConfig');

  # Configure the application
  $self->secrets($config->{secrets});

  my $pg = Mojo::Pg->new("postgresql://$config->{db_user}:$config->{db_pass}\@/$config->{db_db}");
  my $db = $pg->db;

  $self->helper( db =>
    sub {
      print STDERR Dumper($db);
      return $db;
    }
  );
  # Router
  my $r = $self->routes;
  $self->log->info("Hello world");
  # Normal route to controller
  $r->get('/')->to('Example#welcome');
}

1;

and lib/My/App/Controller/Example.pm :

package My::App::Controller::Example;
use Mojo::Base 'Mojolicious::Controller', -signatures;
use Data::Dumper qw(Dumper);

# This action will render a template
sub welcome ($self) {
  $self->log->info("Controller: Hello");
  my $db = $self->db;
  $self->log->info(Dumper($db));
  # Render template "example/welcome.html.ep" with message
  $self->render(msg => 'Welcome to the Mojolicious real-time web framework!');
}

1;

The log output I get is:

$ morbo my_app/script/my_app -l http://127.0.0.1:3001
Web application available at http://127.0.0.1:3001
[2021-07-12 08:57:41.74832] [341218] [info] Hello world
[2021-07-12 08:57:50.42756] [341218] [debug] [D37tTCmA9X9M] GET "/"
[2021-07-12 08:57:50.42800] [341218] [debug] [D37tTCmA9X9M] Routing to controller "My::App::Controller::Example" and action "welcome"
[2021-07-12 08:57:50.42806] [341218] [info] [D37tTCmA9X9M] Controller: Hello
$VAR1 = bless( {
                 'pg' => undef,
                 'dbh' => bless( {}, 'DBI::db' )
               }, 'Mojo::Pg::Database' );
[2021-07-12 08:57:50.42834] [341218] [info] [D37tTCmA9X9M] $VAR1 = bless( {
                 'pg' => undef,
                 'dbh' => bless( {}, 'DBI::db' )
               }, 'Mojo::Pg::Database' );

[2021-07-12 08:57:50.42875] [341218] [debug] [D37tTCmA9X9M] Rendering template "example/welcome.html.ep"
[2021-07-12 08:57:50.42993] [341218] [debug] [D37tTCmA9X9M] Rendering template "layouts/default.html.ep"
[2021-07-12 08:57:50.43060] [341218] [debug] [D37tTCmA9X9M] 200 OK (0.003039s, 329.056/s)
Related