| 1 |
NAME |
| 2 |
Dancer::Plugin::DBIC - DBIx::Class interface for Dancer applications |
| 3 |
|
| 4 |
VERSION |
| 5 |
version 0.2104 |
| 6 |
|
| 7 |
SYNOPSIS |
| 8 |
use Dancer; |
| 9 |
use Dancer::Plugin::DBIC qw(schema resultset rset); |
| 10 |
|
| 11 |
get '/users/:user_id' => sub { |
| 12 |
my $user_id = param 'user_id'; |
| 13 |
my $user; |
| 14 |
|
| 15 |
# all of the following are equivalent: |
| 16 |
$user = schema('default')->resultset('User')->find($user_id); |
| 17 |
$user = schema->resultset('User')->find($user_id); |
| 18 |
$user = resultset('User')->find($user_id); |
| 19 |
$user = rset('User')->find($user_id); |
| 20 |
|
| 21 |
template user_profile => { |
| 22 |
user => $user |
| 23 |
}; |
| 24 |
}; |
| 25 |
|
| 26 |
dance; |
| 27 |
|
| 28 |
DESCRIPTION |
| 29 |
This plugin makes it very easy to create Dancer applications that |
| 30 |
interface with databases. It automatically exports the keyword "schema" |
| 31 |
which returns a DBIx::Class::Schema object. You just need to configure |
| 32 |
your database connection information. For performance, schema objects |
| 33 |
are cached in memory and are lazy loaded the first time they are |
| 34 |
accessed. |
| 35 |
|
| 36 |
This plugin is now just a thin wrapper around DBICx::Sugar. |
| 37 |
|
| 38 |
CONFIGURATION |
| 39 |
Configuration can be done in your Dancer config file. |
| 40 |
|
| 41 |
simple example |
| 42 |
Here is a simple example. It defines one database named "default": |
| 43 |
|
| 44 |
plugins: |
| 45 |
DBIC: |
| 46 |
default: |
| 47 |
dsn: dbi:SQLite:dbname=myapp.db |
| 48 |
schema_class: MyApp::Schema |
| 49 |
|
| 50 |
multiple schemas |
| 51 |
In this example, there are 2 databases configured named "default" and |
| 52 |
"foo": |
| 53 |
|
| 54 |
plugins: |
| 55 |
DBIC: |
| 56 |
default: |
| 57 |
dsn: dbi:SQLite:dbname=myapp.db |
| 58 |
schema_class: MyApp::Schema |
| 59 |
foo: |
| 60 |
dsn: dbi:Pg:dbname=foo |
| 61 |
schema_class: Foo::Schema |
| 62 |
user: bob |
| 63 |
password: secret |
| 64 |
options: |
| 65 |
RaiseError: 1 |
| 66 |
PrintError: 1 |
| 67 |
|
| 68 |
Each database configured must at least have a dsn option. The dsn option |
| 69 |
should be the DBI driver connection string. All other options are |
| 70 |
optional. |
| 71 |
|
| 72 |
If you only have one schema configured, or one of them is named |
| 73 |
"default", you can call "schema" without an argument to get the only or |
| 74 |
"default" schema, respectively. |
| 75 |
|
| 76 |
If a schema_class option is not provided, then |
| 77 |
DBIx::Class::Schema::Loader will be used to dynamically load the schema |
| 78 |
by introspecting the database corresponding to the dsn value. You need |
| 79 |
DBIx::Class::Schema::Loader installed for this to work. |
| 80 |
|
| 81 |
WARNING: Dynamic loading is not recommended for production environments. |
| 82 |
It is almost always better to provide a schema_class option. |
| 83 |
|
| 84 |
The schema_class option should be the name of your DBIx::Class::Schema |
| 85 |
class. See "SCHEMA GENERATION" Optionally, a database configuration may |
| 86 |
have user, password, and options parameters as described in the |
| 87 |
documentation for "connect()" in DBI. |
| 88 |
|
| 89 |
connect_info |
| 90 |
Alternatively, you may also declare your connection information inside |
| 91 |
an array named "connect_info": |
| 92 |
|
| 93 |
plugins: |
| 94 |
DBIC: |
| 95 |
default: |
| 96 |
schema_class: MyApp::Schema |
| 97 |
connect_info: |
| 98 |
- dbi:Pg:dbname=foo |
| 99 |
- bob |
| 100 |
- secret |
| 101 |
- |
| 102 |
RaiseError: 1 |
| 103 |
PrintError: 1 |
| 104 |
|
| 105 |
replicated |
| 106 |
You can also add database read slaves to your configuration with the |
| 107 |
"replicated" config option. This will automatically make your read |
| 108 |
queries go to a slave and your write queries go to the master. Keep in |
| 109 |
mind that this will require additional dependencies: |
| 110 |
DBIx::Class::Optional::Dependencies#Storage::Replicated See |
| 111 |
DBIx::Class::Storage::DBI::Replicated for more details. Here is an |
| 112 |
example configuration that adds two read slaves: |
| 113 |
|
| 114 |
plugins: |
| 115 |
DBIC: |
| 116 |
default: |
| 117 |
schema_class: MyApp::Schema |
| 118 |
dsn: dbi:Pg:dbname=master |
| 119 |
replicated: |
| 120 |
balancer_type: ::Random # optional |
| 121 |
balancer_args: # optional |
| 122 |
auto_validate_every: 5 # optional |
| 123 |
master_read_weight:1 # optional |
| 124 |
# pool_type and pool_args are also allowed and are also optional |
| 125 |
replicants: |
| 126 |
- |
| 127 |
- dbi:Pg:dbname=slave1 |
| 128 |
- user1 |
| 129 |
- password1 |
| 130 |
- |
| 131 |
quote_names: 1 |
| 132 |
pg_enable_utf8: 1 |
| 133 |
- |
| 134 |
- dbi:Pg:dbname=slave2 |
| 135 |
- user2 |
| 136 |
- password2 |
| 137 |
- |
| 138 |
quote_names: 1 |
| 139 |
pg_enable_utf8: 1 |
| 140 |
|
| 141 |
alias |
| 142 |
Schema aliases allow you to reference the same underlying database by |
| 143 |
multiple names. For example: |
| 144 |
|
| 145 |
plugins: |
| 146 |
DBIC: |
| 147 |
default: |
| 148 |
dsn: dbi:Pg:dbname=master |
| 149 |
schema_class: MyApp::Schema |
| 150 |
slave1: |
| 151 |
alias: default |
| 152 |
|
| 153 |
Now you can access the default schema with "schema()", |
| 154 |
"schema('default')", or "schema('slave1')". This can come in handy if, |
| 155 |
for example, you have master/slave replication in your production |
| 156 |
environment but only a single database in your development environment. |
| 157 |
You can continue to reference "schema('slave1')" in your code in both |
| 158 |
environments by simply creating a schema alias in your development.yml |
| 159 |
config file, as shown above. |
| 160 |
|
| 161 |
FUNCTIONS |
| 162 |
schema |
| 163 |
my $user = schema->resultset('User')->find('bob'); |
| 164 |
|
| 165 |
The "schema" keyword returns a DBIx::Class::Schema object ready for you |
| 166 |
to use. If you have configured only one database, then you can simply |
| 167 |
call "schema" with no arguments. If you have configured multiple |
| 168 |
databases, you can still call "schema" with no arguments if there is a |
| 169 |
database named "default" in the configuration. With no argument, the |
| 170 |
"default" schema is returned. Otherwise, you must provide "schema()" |
| 171 |
with the name of the database: |
| 172 |
|
| 173 |
my $user = schema('foo')->resultset('User')->find('bob'); |
| 174 |
|
| 175 |
resultset |
| 176 |
This is a convenience method that will save you some typing. Use this |
| 177 |
only when accessing the "default" schema. |
| 178 |
|
| 179 |
my $user = resultset('User')->find('bob'); |
| 180 |
|
| 181 |
is equivalent to: |
| 182 |
|
| 183 |
my $user = schema->resultset('User')->find('bob'); |
| 184 |
|
| 185 |
rset |
| 186 |
my $user = rset('User')->find('bob'); |
| 187 |
|
| 188 |
This is simply an alias for "resultset". |
| 189 |
|
| 190 |
SCHEMA GENERATION |
| 191 |
Setting the schema_class option and having proper DBIx::Class classes is |
| 192 |
the recommended approach for performance and stability. You can use the |
| 193 |
dbicdump command line tool provided by DBIx::Class::Schema::Loader to |
| 194 |
help you. For example, if your app were named Foo, then you could run |
| 195 |
the following from the root of your project directory: |
| 196 |
|
| 197 |
dbicdump -o dump_directory=./lib Foo::Schema dbi:SQLite:/path/to/foo.db |
| 198 |
|
| 199 |
For this example, your "schema_class" setting would be 'Foo::Schema'. |
| 200 |
|
| 201 |
SEE ALSO |
| 202 |
* DBICx::Sugar |
| 203 |
|
| 204 |
CONTRIBUTORS |
| 205 |
* Alexis Sukrieh <[email protected]> |
| 206 |
|
| 207 |
* Dagfinn Ilmari Mannsåker <<https://github.com/ilmari>> |
| 208 |
|
| 209 |
* David Precious <[email protected]> |
| 210 |
|
| 211 |
* Fabrice Gabolde <<https://github.com/fgabolde>> |
| 212 |
|
| 213 |
* Franck Cuny <[email protected]> |
| 214 |
|
| 215 |
* Steven Humphrey <<https://github.com/shumphrey>> |
| 216 |
|
| 217 |
* Yanick Champoux <<https://github.com/yanick>> |
| 218 |
|
| 219 |
AUTHORS |
| 220 |
* Al Newkirk <[email protected]> |
| 221 |
|
| 222 |
* Naveed Massjouni <[email protected]> |
| 223 |
|
| 224 |
COPYRIGHT AND LICENSE |
| 225 |
This software is copyright (c) 2010 by awncorp. |
| 226 |
|
| 227 |
This is free software; you can redistribute it and/or modify it under |
| 228 |
the same terms as the Perl 5 programming language system itself. |
| 229 |
|