NAME

Mojo::DB::Connector - Create and cache DB connections using common connection info

STATUS

Coverage Status

SYNOPSIS

use Mojo::DB::Connector;

# use default connection info or use connection info
# set in environment variables
my $connector  = Mojo::DB::Connector->new;
my $connection = $connector->new_connection('my_database');
my $results    = $connection->db->query(...);

# pass connection info in
my $connector  = Mojo::DB::Connector->new(host => 'batman.com', userinfo => 'sri:s3cret');
my $connection = $connector->new_connection('my_s3cret_database');
my $results    = $connection->db->query(...);

# cache connections using Mojo::DB::Connector::Role::Cache
my $connector = Mojo::DB::Connector->new->with_roles('+Cache');

# fresh connection the first time
my $connection = $connector->cached_connection('my_database');

# later somewhere else...
# same connection (Mojo::mysql or Mojo::Pg object) as before
my $connection = $connector->cached_connection('my_database');

DESCRIPTION

Mojo::DB::Connector is a thin wrapper around Mojo::mysql and Mojo::Pg that is useful when you want to connect to different databases on the same server using the same connection info. It also allows you to easily connect using different settings in different environments by using environment variables to connect (see "ATTRIBUTES"). This can be useful when developing using something like Docker, which easily allows you to set different environment variables in dev/prod.

See Mojo::DB::Connector::Role::Cache for the ability to cache connections.

ATTRIBUTES

env_prefix

my $connector = Mojo::DB::Connector->new(env_prefix => 'MOJO_DB_CONNECTOR_');

my $env_prefix = $connector->env_prefix;
$connector     = $connector->env_prefix('MOJO_DB_CONNECTOR_');

The prefix that will be used for environment variables names when checking for default values. The prefix will go before:

"env_prefix" allows you to use different Mojo::DB::Connector objects to easily generate connections for different connection settings.

Default is MOJO_DB_CONNECTOR_.

scheme

my $scheme = $connector->scheme;
$connector = $connector->scheme('postgresql');

The "scheme" in Mojo::URL that will be used for generating the connection URL. Allowed values are mariadb, mysql, and postgresql. The scheme will determine whether a Mojo::mysql or Mojo::Pg instance is returned. mariadb and mysql indicate Mojo::mysql, and postgresql indicates Mojo::Pg.

This can also be set with the environment variable MOJO_DB_CONNECTOR_SCHEME.

Default is $ENV{MOJO_DB_CONNECTOR_SCHEME} and falls back to postgresql.

userinfo

my $userinfo = $connector->userinfo;
$connector   = $connector->userinfo('sri:s3cret');

The "userinfo" in Mojo::URL that will be used for generating the connection URL.

This can also be set with the environment variable MOJO_DB_CONNECTOR_USERINFO.

Default is $ENV{MOJO_DB_CONNECTOR_USERINFO} and falls back to no userinfo (empty string).

host

my $host   = $connector->host;
$connector = $connector->host('localhost');

The "host" in Mojo::URL that will be used for generating the connection URL.

This can also be set with the environment variable MOJO_DB_CONNECTOR_HOST.

Default is $ENV{MOJO_DB_CONNECTOR_HOST} and falls back to localhost.

port

my $port   = $connector->port;
$connector = $connector->port(5432);

The "port" in Mojo::URL that will be used for generating the connection URL.

This can also be set with the environment variable MOJO_DB_CONNECTOR_PORT.

Default is $ENV{MOJO_DB_CONNECTOR_PORT} and falls back to 5432.

strict_mode

my $strict_mode = $connector->strict_mode;
$connector      = $connector->strict_mode(1);

"strict_mode" determines if connections should be created in "strict_mode" in Mojo::mysql.

Note that this only applies to Mojo::mysql and does not apply to Mojo::Pg. If a Mojo::Pg connection is created, this will have no effect.

This can also be set with the environment variable MOJO_DB_CONNECTOR_STRICT_MODE.

Default is $ENV{MOJO_DB_CONNECTOR_STRICT_MODE} and falls back to 1

METHODS

new_connection

my $connection = $connector->new_connection('my_database');
my $results    = $connection->db->query(...);

# use options
my $connection = $connector->new_connection('my_database', PrintError => 1, RaiseError => 0);
my $results    = $connection->db->query(...);

"new_connection" creates a new connection (Mojo::mysql or Mojo::Pg instance) using the connection info in "ATTRIBUTES".

"new_connection" requires a database name, and accepts optional options to be passed as key/value pairs. See "options" in Mojo::mysql or "options" in Mojo::Pg.

SEE ALSO

LICENSE

This software is copyright (c) 2020 by Adam Hopkins

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

AUTHOR

Adam Hopkins <srchulo@cpan.org>