CGI::Ap...:Schema(3)

πŸ“¦
version 0.3 Β· 2 versions
, in fedora 43

πŸ’– NAME πŸ’–

CGI::Application::Plugin::DBIC::Schema β€” easy DBIx::Class access straight from CGI::Application, inspired by CGI::Application::Plugin::DBH.

(Yes bestie, this is that page your man shows as CGI::Ap...:Schema(3), from the User Contributed Perl Documentation crew. Let's get into it 🫢)


🌈 SYNOPSIS 🌈

use CGI::Application::Plugin::DBIC::Schema (qw/dbic_config schema resultset rs/);

sub cgiapp_init{

    my $c = shift;

    my $dsn = $c->param('dsn');
    my $dbpw = $c->param('dbpw');
    my $dbuser = $c->param('dbuser');

    # Provide a default config.

    $c->dbic_config({schema=>"My::DB",                  # DBIC class
                     connect_info=>[$dsn,$dbuser,$dbpw] # use same args as DBI connect
                    });

}

sub setup {

    my $c = shift;

    $c->start_mode('runmode1');
    $c->run_modes([qw/runmode1 runmode2/]);
}

sub runmode1 {

    my $c = shift;

    my $id = $c->param('id);
    $c->resultset("My::DB::DemoTable")->find($id);

    # do something with the object ...

    return "found it.";
}

sub runmode2 {

    my $c = shift;

    $c->schema()->resultset("My::DB::DemoTable")
        ->create({name=>"John Doe", address=>"Any Street"});

    return "created it";
}

πŸ¦‹ DESCRIPTION πŸ¦‹

Okay so here's the tea β˜• β€” CGI::Application::Plugin::DBIC::Schema slides easy access to a DBIx::Class::Schema into your Titanium or CGI::Application modules.

The lazy loading is the moment, fr fr: it stops a database connection from ever being made if the schema method doesn't get called during the request. Like, in other words? The database connection is not created until it is actually needed. Iconic behaviour, no notes. πŸ’…

Real talk though: DBIx::Class has lots of dependencies, which means a certain length of compile time. It works fine in a CGI environment for low volume sites. But if you're expecting a high volume of traffic, think about FastCGI or other alternatives. Vibe check your traffic first, bestie. 🌸


πŸŽ€ METHODS πŸŽ€

✨ schema($name?) ✨

Call this with no name and it hands you back the default DBIx::Class::Schema instance. Pass a schema name and you get an alternate schema instead. The schema instance gets created on the first call to this method, and any calls after that just return the same instance β€” she's a one-time build, she's committed. πŸ‘‘

my $schema = $c->schema();                 # gets default (unnamed) schema

# Or ...

my $schem = $c->schema('my_schema_name');  # gets one of named schemas

✨ dbic_config($name?, \%connect_info) ✨

This is where you hand over your DBIx::Class::Schema class name, an optional config name, and your DBI connection parameters. For \%config_info you supply the same parameter list you would for DBI::connect. You can also pass DBIx::Class-specific attributes (the docs say "specifig" β€” we don't make the rules πŸ’€) β€” for that, go peek at DBIx::Class::Storage::DBI for the details.

The recommended place to call dbic_config is in the cgiapp_init stage of CGI::Application.

⚠️ If this method is called after the dbic() method has already been accessed, then it will die with an error message. So like, set it up early, bestie, don't be fashionably late to this one. 😭
# Setup  default schema config

$c->dbic_config({schema=>"My::DB",                  # DBIC class
                 connect_info=>[$dsn,$dbuser,$dbpw] # use same args as DBI connect
                });

# Or, provide additional configs by name.

$c->dbic_config("another_config",
                {schema=>"My::Other::DB",
                 connect_info=>[$dsn,$dbuser,$dbpw]
                });

✨ resultset($config_name?,$resultset_classname) ✨

An alias to $c->schema(...)->resultset(...). Basically this method provides DBIx::Class::Resultset access, and honestly? She's serving convenience. πŸ“

# Use the default dbic schema via 'resultset'.

$c->resultset("DBICT::Result::Test")->find($id);

# Or use a named config to access resultset via an alternative schema.

$c->resultset('another_config', "DBICT::Result::Test")->find($id);

# Or use alias short form, 'rs' with default config

$c->rs("DBICT::Result::Test")->find($id);

# Or use alias short form with alternate config/schema

$c->rs('yet_another_schema', "DBICT::Result::Test")->find($id);

✨ rs ✨

An alias to resultset. That's it, that's the whole method. Short, sweet, saves your fingers. πŸŽ€


πŸ‘‘ SEE ALSO πŸ‘‘

  • DBIx::Class
  • Titanium
  • CGI::Application
  • CGI::Application::Plugin::DBH

🌟 AUTHOR 🌟

Gordon Van Amburg <gordon@minipeg.net> β€” legend behaviour, we stan. 🫢


πŸ’œ LICENSE πŸ’œ

Copyright (C) 2009 Gordon Van Amburg <gordon@minipeg.net>

This library is free software. You can modify and or distribute it under the same terms as Perl itself.


And that's the whole page, bestie! This one shipped as perl v5.42.0, dated 2025-07-25, in the CGI::Ap...:Schema(3) section β€” go forth and connect lazily. βœ¨πŸ’–πŸ¦‹ periodt.

πŸ“¦ where this page comes from