pgc-www/lib/MyModel.pm

37 lines
757 B
Perl

package MyModel;
use feature qw( signatures );
use Moose;
use Moose::Util::TypeConstraints;
use Mojo::Log;
use Book::Schema;
no warnings qw( experimental::signatures );
# Keep rows-per-page within a somewhat-sane range.
subtype 'RowsPerPage',
as 'Int',
where { 1 <= $_ && $_ <= 1000 };
has 'rows_per_page' => (
is => 'rw',
isa => 'RowsPerPage',
required => 1,
default => 20,
);
our $dbname = "pg_book_catalog";
our $logger = Mojo::Log->new;
# Share this among all model instances.
our $schema;
sub schema ($self) {
if (!defined $schema) {
$logger->info("Connecting to database $dbname.");
$schema = Book::Schema->connect("dbi:Pg:dbname=$dbname");
}
return $schema;
}
no Moose;
__PACKAGE__->meta->make_immutable;