NAME
Mojolicious::Plugin::DbAccess - Mojolicious helpers for the D DB access layer
SYNOPSIS
# Mojolicious application
$app->plugin('DbAccess' => {
schema => 'MyApp::Schema',
# other DbAccess configuration keys...
});
# Controller
my $rs = $c->dbdata('User'); # guarded resultset
my $user = $c->resource; # row by param('id')
my $raw = $c->dbdata('User', 'insecure'); # unguarded resultset
DESCRIPTION
This plugin wires D into a Mojolicious application.
It installs helpers for:
- Accessing a cached schema handle - Running atomic changes with a transaction scope guard - Getting guarded/unguarded resultsets - Fetching a resource by id request parameter
CONFIGURATION
Configuration is stored under the DbAccess key.
The plugin tries to merge the passed config via $app->merge_config(DbAccess => $conf) and falls back to C::config->{DbAccess}.
The key schema is required and must be a DBIx::Class schema class name.
HELPERS
db
my $schema = $c->db;
Return the cached schema handle ("db" in D).
atomic_change
my $guard = $c->atomic_change;
Return a transaction scope guard ("txn" in D).
dbdata
my $rs = $c->dbdata;
my $rs = $c->dbdata($table_name);
my $rs = $c->dbdata($table_name, $strict);
Return a resultset for $table_name.
If $table_name is omitted, it is derived from the caller package name:
App::Controller::User -> User
If $strict is not insecure, the resultset is passed through $rs->guard($c).
resource
my $row = $c->resource;
my $row = $c->resource($table_name, $strict);
Fetch a row by the id request parameter, using "dbdata".
If called without extra arguments, the result is cached on the controller object for the duration of the request.
On missing id or a missing row, sets an error message in stash and replies with not_found.
SEE ALSO
D, Mojolicious, Mojolicious::Command::db_env, Mojolicious::Command::clear_database