NAME

Mojolicious::Plugin::QuickPg - Mojolicious Plugin that provided quick access methods for Mojo::Pg

SYNOPSIS

# Mojolicious::Lite

plugin 'QuickPg' => {dsn => 'postgresql://sri:123456@localhost/testdb'};

# Mojolicious (not Lite)
# in startup

$self->plugin('Mojolicious::Plugin::QuickPg' =>
              { dsn => 'postgresql://sri:123456@localhost/testdb',
                debug => 1 } );
                                                 
# in controller
# quick select
# returns array of hashes [{},{}]

my ($all_table) = $c->qselect('table_name');

# returns hash {}

my $one_row = $c->qselect('table_name');
# example with offset and limits
my ($array_ref) = $c->qselect('models', {},{
                                     limit => 10,
                                     offset => 0 }
                              );

# quick count
$c->qcount('table_name', {name => {like => 'Mos%'} } );

# quick insert
# returns value of primary key (like as last_insert_id on MySQL)

my $id = $c->qinsert('models', { name => 'Moscow',
                                 foto => 'https://www.flickr.com/search/?text=Moscow' } );
# or you can do like this

my $params = $c->req->json;

# Do not forget to validate $params before it:

my $id = $c->insert('models', $params);

# quick update
# returns numbers of updated rows

$c->qupdate('models', {id => $id}, { name => 'New York',
                                     foto => 'https://www.flickr.com/search/?text=New%20York'
                                    } );

# quick delete
# returns numbers of deleted rows

$c->qdelete('models', { id => $id });

# catch the errors on insert/delete methods

$c->qerror; # returns $@ value

# custom requests - returns Mojo::Pg::Results object

my $result = $s->qcustom('SELECT a.id, b.name
                          FROM table1 a, table2 b
                          WHERE a.id = b.id AND b.name = ?', $name);
my $arrays = $result->hashes->to_array;                              

DESCRIPTION

Mojolicous::Plugin::QuickPg is a plugin for Mojolicious apps thas provide simple access to Mojo::Pg. The most part of the code for plugin is taken from Dancer::Plugin::Database::Core::Handle (under Artistic License)

HELPERS

Mojolicious::Plugin::QuickPg contains next helpers: qselect, qinsert, qupdate, qdelete, qcustom, qerror, qcount.

qselect

my $one_row = $c->qselect('table_name', {id => 1}, { order_by => {desc => 'id'}, limit => 10, offset => 5, columns => qw[id name]});

For more examples see /examples/*

qinsert

For examples see /examples/*

qupdate

For examples see /examples/*

qdelete

For examples see /examples/*

qcustom

For examples see /examples/*

qcount

For examples see /examples/*

qerror

For more examples see /examples/*

CONFIG

Mojolicious::Plugin::QuickPg configuration support two keys.

  • dsn

    $self->plugin('Mojolicious::Plugin::QuickPg' =>
                               {dsn => 'postgresql://sri:123456@localhost/testdb'} );

    Set connection string

  • debug

    # Lite
    
    plugin 'QuickPg' => {dsn => 'postgresql://sri:123456@localhost/testdb', debug => 1};
    
    # Adults App :)
    
    $self->plugin('Mojolicious::Plugin::QuickPg' =>
                                      { dsn   => 'postgresql://sri:123456@localhost/testdb',
                                        debug => 1 } );
                                                     

    This key switches on|off printing on console SQL requests.

SEE ALSO

Mojo::Pg Mojolicious Mojolicious::Guides http://mojolicious.org.

AUTHOR

Pavel Kuptsov <pkuptsov@gmail.com>

THANKS

Alberto Simões <ambs@perl-hackers.net> Sebastian Riedel <sri@cpan.org>

BUGS

Please report any bugs or feature requests to bug-mojolicious-plugin-quickpg at rt.cpan.org, or through the web interface at http://rt.cpan.org/NoAuth/Bugs.html?Dist=Mojolicious-Plugin-QuickPg. We will be notified, and then you'll automatically be notified of progress on your bug as we make changes.

COPYRIGHT & LICENSE

Copyright (C) 2016 by Pavel Kuptsov.

This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.