Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Seeking guidance for web/db application

by neilwatson (Priest)
on Mar 13, 2014 at 18:29 UTC ( #1078222=perlquestion: print w/replies, xml ) Need Help??
neilwatson has asked for the wisdom of the Perl Monks concerning the following question:

Greetings,

I seldom venture in the web end of the Perl pool, but am doing so now and I seek your advice. I'm using Mojolicious and I'm thinking about how to divide up my code. I was thinking of using a module for database functions, but am wondering how I'm going to pass DB handles back and forth. For example:

DB module:

sub new { bless {}; shift } sub connect { my $self = shift; my %param = @_; my $table = "cf_status_log"; my $dbh = DBI->connect( "DBI:Pg:dbname=$param{'db_name'}; host=$param{'db_host'}", "$param{'db_user'}", "$param{'db_pass'}", { RaiseError => 1 } ) or die "Could not connect to database"; return $dbh; }; sub inventory_query { my $dbh = shift; my $result = $dbh->prepare( "SELECT * FROM dr_inventory_classes" ) || die "Could not prepare" ; } 1;

And the main app:

use lib 'lib'; use DeltaR; my $dr = DeltaR->new; sub dbh { my $self = shift; my $dbh = $dr->connect( db_name => "postgres", db_user => "postgres", db_pass => "", db_host => "127.0.0.1" ); return $dbh; }; get '/inventory' => sub { my $self = shift; my $dbh = dbh; my $rows = $dr->query_inventory( $dbh ); $self->stash( title => "Inventory report", record_limit => $record_limit, rows => $rows, columns => [ 'Count', 'Class' ] ); } => "rtable"; app->start;

I don't want to make unneeded connections because not all pages will require the database. I don't know if handle passing like this is the best approach. How would you do it? Passing $dbh around is a bit fiddly. Is there a better way?

Neil Watson
watson-wilson.ca

Replies are listed 'Best First'.
Re: Seeking guidance for web/db application
by davido (Archbishop) on Mar 13, 2014 at 20:21 UTC

    Make the database handle an attribute stored in the Mojolicious application class. Better yet, make a DBIx::Connector object an attribute stored in the application class. Either way, that attibute will be available from within the controller, and can then be passed to your model. See the following Mojolicious documentation that shows examples both for full Mojolicious apps, and for Mojolicious::Lite apps:

    https://github.com/kraih/mojo/wiki/Hypnotoad-prefork-web-server


    Dave

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1078222]
Approved by Limbic~Region
help
Chatterbox?
[erix]: then you might as well send that patch to the DBIC guys :)
[Corion]: erix: Yeah, I just found that it has no documentation at all on how to circumvent/ eliminate "1+n SELECTs" by building a local hash... I guess I have to make ->has_many do the hash lookup instead of doing the SQL query. But as the problem ...
[Corion]: ... has only manifested itself so far through the puzzled questions of other bystanders, I won't go deeper at this time. But the DBIx::Class documentation could well do with a document on how to make "it" (that is, ORMs in general) faster ;)
[Corion]: I find that DBIx::Class, like most ORMs makes things easy until they become performance critical and then makes it horribly hard to change things because the design is highly inflexible if you don't already know about the problems of 1+n :-/

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (7)
As of 2017-09-25 11:06 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    During the recent solar eclipse, I:









    Results (279 votes). Check out past polls.

    Notices?