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?