MooseX::Role::DBIC - make your Moose class encapsulate one or more DBIC schemas


  ### 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',


  ### 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',



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.



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.


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.


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'   ],



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.


Class name of the schema to use for this slot. Required, unless you provide a default via accessor_options.


DBI DSN for your schema. Required.


Username for the schema.


Password for the schema.


Hashref of DBI attributes for the schema. Passed to DBIx::Class::Schema::connect, which passes them to DBI's connect()


Hashref of other attributes for the schema. Passed to DBIx::Class::Schema::connect.


Robert Buels <>


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.