[Dbix-class] Setting up database connection at run-time

Matt S Trout dbix-class at trout.me.uk
Mon Jan 14 01:03:26 GMT 2008


On Sun, Jan 13, 2008 at 02:10:49PM -0500, Juan Camacho wrote:
> This question is somewhere between DBix::Class and Catalyst. I'm
> starting a catalyst application where each account will have their own
> database. The schema is identical for each database and there is one
> master database where username and database mappings are stored.
> After the user logs in I'm able to determine which database the user
> will interact with and I'm setting the $c->session->{database}
> accordingly.

So on the DBIC side you just ->connect a new $schema instance with the
appropriate info; nothing difficult there.

On the Catalyst side use Catalyst::Component::InstancePerContext and
something approximating

my $new = $self->new(%$self);
$new->schema($self->schema->connect(<get connect info from $c>);

I do something similar quite a lot except that rather than changing the
DB connection I'm calling restrict_with_object (provided by
DBIx::Class::Schema::RestrictWithObject) to impose a security context.
Same basic principle though.

-- 
      Matt S Trout       Need help with your Catalyst or DBIx::Class project?
   Technical Director                    http://www.shadowcat.co.uk/catalyst/
 Shadowcat Systems Ltd.  Want a managed development or deployment platform?
http://chainsawblues.vox.com/            http://www.shadowcat.co.uk/servers/



More information about the DBIx-Class mailing list