Created
April 26, 2013 17:24
-
-
Save powerman/5468849 to your computer and use it in GitHub Desktop.
Using AnyEvent::DBI::MySQL with Mojolicious.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use AnyEvent::DBI::MySQL; | |
sub startup { | |
my $app = shift; | |
$app->config(db => {dsn=>…, login=>…, pass=>…}); | |
$app->helper(dbh => sub { shift->{dbh} }); | |
$app->helper(new_dbh => sub { | |
state $db = shift->app->config('db') or return; | |
return AnyEvent::DBI::MySQL->connect(@{$db}{qw(dsn login pass)}, | |
{mysql_enable_utf8 => 1}); | |
}); | |
$app->hook(before_routes => sub { | |
my $c = shift; | |
# each connection have own dbh | |
$c->{dbh} = $c->new_dbh; | |
}); | |
… | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment