Dancer...::DBIC(3pm)
π Dancer::Plugin::DBIC π
okay bestie, welcome to Dancer::Plugin::DBIC(3pm) β that's the manual page, the label at the bottom is perl v5.40.1 from 2025-08-15, and it lives in the User Contributed Perl Documentation corner of the world. this plugin? the whole entire point is making your Dancer app talk to a database without you crying. it's giving "we got you." β¨
version: 0.2104 π
β¨ SYNOPSIS β¨
use Dancer;
use Dancer::Plugin::DBIC qw(schema resultset rset);
get '/users/:user_id' => sub {
my $user_id = param 'user_id';
my $user;
# all of the following are equivalent:
$user = schema('default')->resultset('User')->find($user_id);
$user = schema->resultset('User')->find($user_id);
$user = resultset('User')->find($user_id);
$user = rset('User')->find($user_id);
template user_profile => {
user => $user
};
};
dance;
like. look at that. four whole ways to do the exact same thing and every single one of them is valid. we love options π
πΈ DESCRIPTION πΈ
so this plugin is literally here to make building Dancer apps that talk to databases super easy. it automatically exports the keyword schema, and schema hands you back a DBIx::Class::Schema object. you just configure your database connection info and go.
and for performance? schema objects are cached in memory and they're lazy loaded β so they don't even get built until the first time you actually touch them. main character energy, honestly. π
also real talk: this plugin is now just a thin wrapper around DBICx::Sugar. that's it. that's the tea π
π CONFIGURATION π
you do all your configuring right in your Dancer config file. let's go π¦
π simple example π
here's a lil baby example. it defines one database named default:
plugins:
DBIC:
default:
dsn: dbi:SQLite:dbname=myapp.db
schema_class: MyApp::Schema
one db, one config, slay β¨
π multiple schemas π
in this example there are 2 databases configured, named default and foo:
plugins:
DBIC:
default:
dsn: dbi:SQLite:dbname=myapp.db
schema_class: MyApp::Schema
foo:
dsn: dbi:Pg:dbname=foo
schema_class: Foo::Schema
user: bob
password: secret
options:
RaiseError: 1
PrintError: 1
okay so important: every database you configure must have at least a dsn option. the dsn is supposed to be the DBI driver connection string. everything else? optional, babe. truly optional.
if you only have one schema configured β or one of them is named default β then you can just call schema with no argument and you'll get the only one, or the default one respectively. no arguments needed, we're chill like that π«Ά
and if you don't provide a schema_class option? then DBIx::Class::Schema::Loader gets used to dynamically load the schema by introspecting the database that matches your dsn value. fair warning though: you need DBIx::Class::Schema::Loader installed for that to even work. π«
π¨ WARNING: dynamic loading is not recommended for production environments. it is almost always better to provide a schema_class option. periodt.
the schema_class option should be the name of your DBIx::Class::Schema class. see "SCHEMA GENERATION" for more. optionally, a database configuration can also have user, password, and options parameters as described in the documentation for connect() in DBI. iykyk π
π§ connect_info π§
alternatively β and this is iconic β you may also declare your connection information inside an array named connect_info:
plugins:
DBIC:
default:
schema_class: MyApp::Schema
connect_info:
- dbi:Pg:dbname=foo
- bob
- secret
-
RaiseError: 1
PrintError: 1
look at her. an array. a whole vibe. π
π replicated π
you can also add database read slaves to your configuration with the replicated config option!! this automatically makes your read queries go to a slave and your write queries go to the master. obsessed.
keep in mind though β this requires additional dependencies: DBIx::Class::Optional::Dependencies#Storage::Replicated. see DBIx::Class::Storage::DBI::Replicated for more deets. here's an example config that adds two read slaves:
plugins:
DBIC:
default:
schema_class: MyApp::Schema
dsn: dbi:Pg:dbname=master
replicated:
balancer_type: ::Random # optional
balancer_args: # optional
auto_validate_every: 5 # optional
master_read_weight:1 # optional
# pool_type and pool_args are also allowed and are also optional
replicants:
-
- dbi:Pg:dbname=slave1
- user1
- password1
-
quote_names: 1
pg_enable_utf8: 1
-
- dbi:Pg:dbname=slave2
- user2
- password2
-
quote_names: 1
pg_enable_utf8: 1
two slaves. one master. balance. we stan a symmetrical setup βοΈβ¨
πΈ alias πΈ
schema aliases let you reference the same underlying database by multiple names. so basically, one database, several nicknames. very "we go by many names" of it. for example:
plugins:
DBIC:
default:
dsn: dbi:Pg:dbname=master
schema_class: MyApp::Schema
slave1:
alias: default
now you can access the default schema with schema(), schema('default'), or schema('slave1'). π
this comes in clutch if, like, you have master/slave replication in production but only a single database in development. you can keep referencing schema('slave1') in your code in both environments by just creating a schema alias in your development.yml config file, exactly like what's shown above. chef's kiss π
π FUNCTIONS π
β schema β
my $user = schema->resultset('User')->find('bob');
the schema keyword returns a DBIx::Class::Schema object, all ready for you to use. π
if you configured only one database, you can simply call schema with no arguments β easy. if you configured multiple databases, you can still call schema with no arguments as long as there's a database named default in the config. with no argument, the default schema is what comes back. otherwise? you must provide schema() with the name of the database:
my $user = schema('foo')->resultset('User')->find('bob');
name it or it's not coming home with you π«Ά
π¦ resultset π¦
this is a convenience method that'll save you some typing. use it only when you're accessing the default schema, okay?
my $user = resultset('User')->find('bob');
is equivalent to:
my $user = schema->resultset('User')->find('bob');
same thing, fewer characters, we love efficiency β¨
π rset π
my $user = rset('User')->find('bob');
this is simply an alias for resultset. that's it. that's the whole section. iconic brevity π
π SCHEMA GENERATION π
setting the schema_class option and having proper DBIx::Class classes is the recommended approach for performance and stability. do it. trust. π
you can use the dbicdump command line tool that comes with DBIx::Class::Schema::Loader to help you out. for example, if your app were named Foo, then you could run the following from the root of your project directory:
dbicdump -o dump_directory=./lib Foo::Schema dbi:SQLite:/path/to/foo.db
for this example, your "schema_class" setting would be 'Foo::Schema'. π
β¨ SEE ALSO β¨
DBICx::Sugar
π«Ά CONTRIBUTORS π«Ά
- Alexis Sukrieh
<sukria@sukria.net> - Dagfinn Ilmari MannsΓ₯ker
<<https://github.com/ilmari>> - David Precious
<davidp@preshweb.co.uk> - Fabrice Gabolde
<<https://github.com/fgabolde>> - Franck Cuny
<franck@lumberjaph.net> - Steven Humphrey
<<https://github.com/shumphrey>> - Yanick Champoux
<<https://github.com/yanick>>
π AUTHORS π
- Al Newkirk
<awncorp@cpan.org> - Naveed Massjouni
<naveed@vt.edu>
π COPYRIGHT AND LICENSE π
this software is copyright (c) 2010 by awncorp.
this is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself. πΈ
and that's the whole page bestie!! π this was Dancer::Plugin::DBIC(3pm) from the User Contributed Perl Documentation, sitting at version 0.2104, served up fresh on 2025-08-15 alongside perl v5.40.1. now go configure a database, you absolute legend π₯πβ¨
π¦ where this page comes from
- ubuntu resolute: libdancer-plugin-dbic-perl 0.2104-5 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu stonking: libdancer-plugin-dbic-perl 0.2104-5 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms