| 1 | package Mojolicious::Plugin::DbAccess; | ||
| 2 | use Mojo::Base 'Mojolicious::Plugin'; | ||
| 3 | |||
| 4 | |||
| 5 | use C; | ||
| 6 | use D; | ||
| 7 | |||
| 8 | |||
| 9 | |||
| 10 | sub register { | ||
| 11 | my( $plugin, $app, $conf ) = @_; | ||
| 12 | |||
| 13 | $conf = eval{ $app->merge_config( DbAccess => $conf ) } | ||
| 14 | // C::config->{ DbAccess }; | ||
| 15 | %$conf or return warn "No configuration for DbAccess plugin"; | ||
| 16 | |||
| 17 | $conf->{ schema } or die "{ DbAccess }{ schema } MUST BE configured"; | ||
| 18 | eval "use $conf->{ schema }"; | ||
| 19 | |||
| 20 | $app->helper( db => sub{ D::db() } ); | ||
| 21 | $app->helper( atomic_change => sub{ D::txn() } ); | ||
| 22 | |||
| 23 | $app->helper( dbdata => \&db_table ); | ||
| 24 | $app->helper( resource => \&resource ); | ||
| 25 | } | ||
| 26 | |||
| 27 | |||
| 28 | |||
| 29 | sub db_table { | ||
| 30 | my( $c, $table_name, $strict ) = @_; | ||
| 31 | $strict //= 'secure'; | ||
| 32 | |||
| 33 | # Get table name from last part of the caller package: | ||
| 34 | # App::Controller::User -> User | ||
| 35 | $table_name //= ((ref $c) =~ /([^:]*)$/)[0]; | ||
| 36 | my $db_table = $c->db->resultset( $table_name ); | ||
| 37 | |||
| 38 | # Developer should explicitly pass 'insecure' value to force full access | ||
| 39 | # Otherwise access will go through &guard | ||
| 40 | $strict eq 'insecure' or return $db_table->guard( $c ); | ||
| 41 | |||
| 42 | return $db_table; | ||
| 43 | } | ||
| 44 | |||
| 45 | |||
| 46 | |||
| 47 | sub resource { | ||
| 48 | my $c = shift; | ||
| 49 | |||
| 50 | my $id = $c->param( 'id' ) or do{ | ||
| 51 | $c->stash->{ error }{ message } = "Nothing is requested"; | ||
| 52 | $c->reply->not_found; | ||
| 53 | |||
| 54 | return undef; | ||
| 55 | }; | ||
| 56 | |||
| 57 | |||
| 58 | # DO NOT CACHE 'insecure' requests | ||
| 59 | return $c->dbdata( undef, @_ )->find( $id ) if @_; | ||
| 60 | |||
| 61 | |||
| 62 | |||
| 63 | $c->{ resource } //= $c->dbdata->find( $id ) or do{ | ||
| 64 | $c->stash->{ error }{ message } = "Requested resource ID:$id not_found"; | ||
| 65 | $c->reply->not_found; | ||
| 66 | |||
| 67 | return undef; | ||
| 68 | }; | ||
| 69 | |||
| 70 | return $c->{ resource }; | ||
| 71 | } | ||
| 72 | |||
| 73 | |||
| 74 | |||
| 75 | 1; | ||
| 76 | |||
| 77 | =encoding utf8 | ||
| 78 | |||
| 79 | =head1 NAME | ||
| 80 | |||
| 81 | Mojolicious::Plugin::DbAccess - Mojolicious helpers for the D DB access layer | ||
| 82 | |||
| 83 | =head1 SYNOPSIS | ||
| 84 | |||
| 85 | # Mojolicious application | ||
| 86 | $app->plugin('DbAccess' => { | ||
| 87 | schema => 'MyApp::Schema', | ||
| 88 | # other DbAccess configuration keys... | ||
| 89 | }); | ||
| 90 | |||
| 91 | # Controller | ||
| 92 | my $rs = $c->dbdata('User'); # guarded resultset | ||
| 93 | my $user = $c->resource; # row by param('id') | ||
| 94 | |||
| 95 | my $raw = $c->dbdata('User', 'insecure'); # unguarded resultset | ||
| 96 | |||
| 97 | =head1 DESCRIPTION | ||
| 98 | |||
| 99 | This plugin wires L<D> into a Mojolicious application. | ||
| 100 | |||
| 101 | It installs helpers for: | ||
| 102 | |||
| 103 | - Accessing a cached schema handle | ||
| 104 | - Running atomic changes with a transaction scope guard | ||
| 105 | - Getting guarded/unguarded resultsets | ||
| 106 | - Fetching a resource by C<id> request parameter | ||
| 107 | |||
| 108 | =head1 CONFIGURATION | ||
| 109 | |||
| 110 | Configuration is stored under the C<DbAccess> key. | ||
| 111 | |||
| 112 | The plugin tries to merge the passed config via | ||
| 113 | C<< $app->merge_config(DbAccess => $conf) >> and falls back to | ||
| 114 | C<C::config-E<gt>{DbAccess}>. | ||
| 115 | |||
| 116 | The key C<schema> is required and must be a DBIx::Class schema class name. | ||
| 117 | |||
| 118 | =head1 HELPERS | ||
| 119 | |||
| 120 | =head2 db | ||
| 121 | |||
| 122 | my $schema = $c->db; | ||
| 123 | |||
| 124 | Return the cached schema handle (L<D/db>). | ||
| 125 | |||
| 126 | =head2 atomic_change | ||
| 127 | |||
| 128 | my $guard = $c->atomic_change; | ||
| 129 | |||
| 130 | Return a transaction scope guard (L<D/txn>). | ||
| 131 | |||
| 132 | =head2 dbdata | ||
| 133 | |||
| 134 | my $rs = $c->dbdata; | ||
| 135 | my $rs = $c->dbdata($table_name); | ||
| 136 | my $rs = $c->dbdata($table_name, $strict); | ||
| 137 | |||
| 138 | Return a resultset for C<$table_name>. | ||
| 139 | |||
| 140 | If C<$table_name> is omitted, it is derived from the caller package name: | ||
| 141 | |||
| 142 | App::Controller::User -> User | ||
| 143 | |||
| 144 | If C<$strict> is not C<insecure>, the resultset is passed through | ||
| 145 | C<< $rs->guard($c) >>. | ||
| 146 | |||
| 147 | =head2 resource | ||
| 148 | |||
| 149 | my $row = $c->resource; | ||
| 150 | my $row = $c->resource($table_name, $strict); | ||
| 151 | |||
| 152 | Fetch a row by the C<id> request parameter, using L</dbdata>. | ||
| 153 | |||
| 154 | If called without extra arguments, the result is cached on the controller | ||
| 155 | object for the duration of the request. | ||
| 156 | |||
| 157 | On missing C<id> or a missing row, sets an error message in stash and replies | ||
| 158 | with C<not_found>. | ||
| 159 | |||
| 160 | =head1 SEE ALSO | ||
| 161 | |||
| 162 | L<D>, L<Mojolicious>, L<Mojolicious::Command::db_env>, | ||
| 163 | L<Mojolicious::Command::clear_database> | ||
| 164 | |||
| 165 | =cut |