NAME
Mojolicious::Plugin::PgAsync - Mojolicious Plugin for asynchronous work with PostgreSQL
SYNOPSIS
# Mojolicious::Lite
plugin
PgAsync
=> {
dbi
=> [
'dbi:Pg:dbname=test'
,
'postgres'
,
''
, {
AutoCommit
=> 1,
RaiseError
=> 1}]};
# in controller
$self
->pg(
'SELECT 3 as id, pg_sleep(?)'
,
undef
, 3,
sub
{
my
$db
=
shift
;
my
$info
=
$db
->sth->fetchall_arrayref({});
$self
->render(
text
=>
$info
->[0]{id});
}
);
DESCRIPTION
Mojolicious::Plugin::PgAsync is a plugin for Mojolicious apps to work asynchronous with PostgreSQL using DBD::Pg, include listen feature. Plugin uses own connections pool.
HELPERS
Mojolicious::Plugin::PgAsync contains two helpers: pg and pg_listen.
pg
Like DBI method DBI#do or DBI#selectall_arrayref execute a single statement. Callback return object Mojolicious::Plugin::PgAsync::Db contains methods dbh and sth for fetch result or commit transaction.
$self
->pg(
'UPDATE test SET name=?'
,
undef
,
'foo'
,
sub
{
my
$db
=
shift
;
my
$rv
=
$db
->sth->rows;
$db
->dbh->commit
if
$rv
== 1;
}
);
pg_listen
Listen for a notification.
$self
->pg_listen(
'foo'
,
sub
{
my
$notify
=
shift
;
$self
->render(
text
=>
'channel '
.
$notify
->{channel});
});
Callback return hashref with keys channel(alias name), pid and payload.
ATTRIBUTES
Mojolicious::Plugin::PgAsync contains the following attributes:
dbi
Arrayref of DBI parameters for connect to PostgreSQL DB.
ttl
Time to life for idle connections, seconds. Default - 30.
AUTHOR
Alexander Romanenko <romanenko@cpan.org>
COPYRIGHT & LICENSE
Copyright (C) 2013 by Alexander Romanenko.
This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.