Dancer2...in::DBIC(3)
π Dancer2::Plugin::DBIC
okay bestie, buckle up β this is the Dancer2::Plugin::DBIC page from the User Contributed Perl Documentation, and it's genuinely one of the most useful little plugins ever. DBIC. Database vibes. Slay. β¨
Version? version 0.0100. Tiny number, big energy. π
β¨ SYNOPSIS β¨
here's the whole vibe in one go, no cap:
use Dancer2;
use Dancer2::Plugin::DBIC;
get '/users/:user_id' => sub {
my $user = schema('default')->resultset('User')->find(param 'user_id');
# If you are accessing the 'default' schema, then all the following
# are equivalent to the above:
$user = schema->resultset('User')->find(param 'user_id');
$user = resultset('User')->find(param 'user_id');
$user = rset('User')->find(param 'user_id');
template user_profile => {
user => $user
};
};
dance;
likeβ¦ look at that. four whole ways to say the same thing and they're all valid. main character energy fr fr. π
πΈ DESCRIPTION πΈ
So this plugin makes it so easy to build Dancer2 apps that talk to databases. It literally automatically exports the keyword schema, which hands you back a DBIx::Class::Schema object. Obsessed. π«Ά
It also exports the keywords resultset and rset β more on those below.
All you gotta do is configure your database connection info, that's it. And for performance? The schema objects get cached in memory and are lazy loaded the first time you actually access them. The way that it doesn't do work until it has toβ¦ iconic. π
This plugin is a thin wrapper around DBICx::Sugar. Just so you know what's underneath the glitter. β¨
π¦ CONFIGURATION π¦
You do the config in your Dancer2 config file. Here's a minimal little example that defines one database named default:
plugins:
DBIC:
default:
dsn: dbi:SQLite:dbname=some.db
And in this example, there are 2 databases configured, named default and foo:
plugins:
DBIC:
default:
dsn: dbi:SQLite:dbname=some.db
schema_class: MyApp::Schema
foo:
dsn: dbi:mysql:foo
schema_class: Foo::Schema
user: bob
password: secret
options:
RaiseError: 1
PrintError: 1
okay so the rules, bestie:
Every database you configure must at least have a dsn option. That dsn option should be the DBI driver connection string. Everything else is optional β optional is the keyword, lowkey everyone forgets it's optional. π
If you only have one schema configured, or one of them is named default, you can call schema with no argument to get the only schema or the default schema respectively. Convenient? extremely. π
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 corresponding to the dsn value. Fair warning though: you need DBIx::Class::Schema::Loader installed to take advantage of that. It's giving "install your dependencies first." π
The schema_class option should be a proper Perl package name that Dancer2::Plugin::DBIC will use as a DBIx::Class::Schema class. Optionally, a database configuration may have user, password, and options parameters, as described in the documentation for connect() in DBI.
You may also declare your connection information in this format instead (which might look more familiar if you're a DBIC user already):
plugins:
DBIC:
default:
connect_info:
- dbi:mysql:foo
- bob
- secret
-
RaiseError: 1
PrintError: 1
two whole formats. choose your fighter. π₯
π FUNCTIONS π
π schema
my $user = schema->resultset('User')->find('bob');
The schema keyword returns a DBIx::Class::Schema object, ready for you to use. π
If you've configured only one database, you can simply call schema with no arguments. If you've configured multiple databases, you can still call schema with no arguments as long as there's a database named default in the configuration.
With no argument, the default schema is returned. Otherwise? You gotta pass schema() the name of the database:
my $user = schema('foo')->resultset('User')->find('bob');
π resultset
This is a convenience method that saves you some typing β and honestly, who among us isn't trying to type less. Use this only when you're accessing the default schema.
my $user = resultset('User')->find('bob');
is equivalent to:
my $user = schema->resultset('User')->find('bob');
same thing. shorter. slay. π
π rset
my $user = rset('User')->find('bob');
This is simply an alias for resultset. that's it. that's the whole function. π«Ά
π SCHEMA GENERATION π
There are two approaches for generating schema classes, and yes you have to pick one.
Approach one: generate your own DBIx::Class classes and set the corresponding schema_class setting in your configuration, like we showed above. This is the recommended approach for performance and stability. do this one, bestie, it's the glow-up path. π
Approach two: it's also possible to have schema classes dynamically generated if you omit the schema_class configuration setting. This requires you to have DBIx::Class::Schema::Loader installed. The v7 naming scheme will be used for naming the auto generated classes β see "naming" in DBIx::Class::Schema::Loader::Base for more info about naming, iykyk.
For generating your own schema classes, you can use the dbicdump command line tool provided by DBIx::Class::Schema::Loader to help you out. For example, if your app were named Foo, 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 that example, your schema_class setting would be Foo::Schema. cute, right? π
β SEE ALSO β
- DBICx::Sugar
π§ CONTRIBUTORS π§
- Alexis Sukrieh
<sukria@sukria.net> - Dagfinn Ilmari MannsΓ₯ker
<<https://github.com/ilmari>> - David Precious
<davidp@preshweb.co.uk> - ennio
<<https://github.com/scriplit>> - Fabrice Gabolde
<<https://github.com/fgabolde>> - Franck Cuny
<franck@lumberjaph.net> - Steven Humphrey
<<https://github.com/shumphrey>> - Yanick Champoux
<<https://github.com/yanick>>
look at this lineup. legends, all of them. π₯
π AUTHOR π
Naveed Massjouni <naveed@vt.edu>
π COPYRIGHT AND LICENSE π
This software is copyright (c) 2013 by Naveed Massjouni.
This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.
k that's the whole page, bestie π β this was perl v5.42.0, dated 2020-11-04, page Dancer2...in::DBIC(3), and that's the tea π¦ periodt.
π¦ where this page comes from
- opensuse-leap 16.0: perl-Dancer2-Plugin-DBIC 0.0100-bp160.1.10 Β· Artistic-1.0 OR GPL-1.0-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms