NAME
MooseX::Role::DBIC - make your Moose class encapsulate one or more DBIC schemas
SYNOPSIS
### simplest case
package MyClass;
use Moose;
with 'MooseX::Role::DBIC';
package main;
my $x = MyClass->new( dbic_class => 'My::Schema',
dbic_user => 'chris',
dbic_password => 'monkeys',
);
$x->dbic_schema->resultset('Foo')->search(...);
##############
### a more complicated use case:
### BigClass has 2 different schemas, an 'itchy_schema' and a
### 'scratchy_schema', each with convenient default schema names.
package BigClass;
use Moose;
with 'MooseX::Role::DBIC' => {
schema_name => 'itchy',
accessor_options => {
itchy_class => [ default => 'Itchy::Schema' ],
},
};
with 'MooseX::Role::DBIC' => {
schema_name => 'scratchy',
accessor_options => {
scratchy_class => [ default => 'Scratchy::Schema' ],
},
};
# 2 database connections can take a lot of parameters ...
my $c = BigClass->new(
itchy_dsn => 'dbi:Pg:dbname=foo;host=bar',
itchy_user => 'mikey',
itchy_password => 'seekrit',
itchy_attrs => { AutoCommit => 1 },
itchy_schema_options => {
on_connect_do => 'set search_path=foo,bar,public',
},
scratchy_dsn => 'dbi:SQLite:dbname=somefile',
);
$c->itchy_schema->resultset(...);
$c->scratchy_schema->resultset(...);
DESCRIPTION
Generic parameterized Moose role to give your class accessors for managing one or more DBIx::Class::Schema objects.
Can be composed with MooseX::Role::DBIx::Connector to share the same dsn, user, password, and connection attributes.
ROLE PARAMETERS
schema_name
Optional name for this connection, which is the prefix for all the generated accessors. Default 'dbic', which means that you get the accessors dbic_dsn
, dbic_schema
, etc.
schema_description
Optional plaintext description of this connection. Only used in generating documentation
metadata for each of the generated accessors. Defaults to the schema_name with underscores replaced by spaces.
accessor_options
Optional hashref of additional options to pass to the generated accessors, e.g.
package MyClass;
use Moose;
with 'MooseX::Role::DBIC' => {
schema_name => 'itchy',
accessor_options => {
'itchy_dsn' => [ traits => ['GetOpt'] ],
'itchy_user' => [ default => 'minnie_the_moocher' ],
},
};
ATTRIBUTES
(schema_name)_schema
Get a DBIx::Connector schema object for the given schema info. This is the most important one. It's a lazy accessor, meaning the schema will not be created until the accessor is called.
Conveniently, you can set new values of any of the connection attributes, and this schema attribute will be cleared, causing a new schema with the correct attributes to be created on the next call to (schema_name)_schema
.
(schema_name)_class
Class name of the schema to use for this slot. Required, unless you provide a default via accessor_options
.
(schema_name)_dsn
DBI DSN for your schema. Required.
(schema_name)_user
Username for the schema.
(schema_name)_password
Password for the schema.
(schema_name)_attrs
Hashref of DBI attributes for the schema. Passed to DBIx::Class::Schema::connect, which passes them to DBI's connect()
(schema_name)_schema_options
Hashref of other attributes for the schema. Passed to DBIx::Class::Schema::connect.
AUTHOR
Robert Buels <rbuels@cpan.org>
COPYRIGHT AND LICENSE
This software is copyright (c) 2011 by Robert Buels.
This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.