The Plack::App::RapidApp::rDbic interface to RapidApp
Overview
In the previous article I introduced the RapidApp utility script rdbic.pl which provides instant CRUD interfaces for DBI databases. In this article we'll look at the internal module which rdbic.pl uses, Plack::App::RapidApp::rDbic
Mounting the rDbic Plack app
Consider the following rdbic.pl command which starts up the CRUD app using a built-in web server:
rdbic.pl dbi:mysql:mydb,root,''
Internally, this starts a PSGI application using Plack::App::RapidApp::rDbic which does the heavy lifting. The above rdbic.pl is roughly equivalent to the following (which could be specified in a .psgi file and started with plackup):
use Plack::App::RapidApp::rDbic; my $app = Plack::App::RapidApp::rDbic->new({ connect_info => { dsn => 'dbi:mysql:mydb', user => 'root', password => '' } })->to_app;
Under the hood, this generates a fully working RapidApp/Catalyst application into a temp directory, including generation of a DBIx::Class::Schema, Model::DBIC::Schema and Plugin::RapidDbic configuration using the RapidApp bootstrap system.
Alternatively, if you already have a DBIC schema class, you can tell rDbic to use it instead of auto-generating:
my $app = Plack::App::RapidApp::rDbic->new({ schema_class => 'My::Db::Schema', connect_info => { dsn => 'dbi:mysql:mydb', user => 'root', password => '' } })->to_app;
You can also supply an already connected schema object:
my $schema = My::Db::Schema->connect(); # ... my $app = Plack::App::RapidApp::rDbic->new({ schema => $schema })->to_app;
This makes it possible to do things like exposing the rDbic a CRUD interface within the structure of an existing Catalyst or PSGI app, such as:
use MyCatApp; my $app = MyCatApp->apply_default_middlewares(MyCatApp->psgi_app); use Plack::Builder; use Plack::App::RapidApp::rDbic; my $rDbic = Plack::App::RapidApp::rDbic->new({ schema => MyCatApp->model('DB')->schema }); builder { mount '/' => $app; mount '/rdbic/' => $rDbic->to_app; };
Configuration
rDbic is just a Plack wrapper for a generated RapidApp using Plugin::RapidDbic which provides powerful configuration options.
The RapidDbic plugin reads its config from the 'RapidDbic'
key in DBIC::Schema model,
and this can be set via the model_config
param to rDbic:
my $app = Plack::App::RapidApp::rDbic->new({ schema => $schema, model_config => { RapidDbic => { # join all columns of all first-level relationships in all grids: grid_params => { '*defaults' => { include_colspec => ['*','*.*'] } }, # Set the display_column for the source named Account to 'fname': TableSpecs => { Account => { display_column => 'fname' }, } # ... # ... } } })->to_app;
RapidDbic allows for all kinds of scenarios and customization. For more info, see the Chinook demo on the RapidApp website:
Summary
Plack::App::RapidApp::rDbic provides a convenient Plack-based interface to a runtime loaded RapidApp using Plugin::RapidDbic. This is a great place to get started with RapidApp, although it only scratches the surface of what can be done.
To learn more or get involved, see the project website at www.rapidapp.info or visit us on IRC in the #rapidapp
channel on irc.perl.org
.
Author
Henry Van Styn vanstyn@cpan.org