1Dancer2::Plugin::DBIC(3U)ser Contributed Perl DocumentatiDoanncer2::Plugin::DBIC(3)
2
3
4
6 Dancer2::Plugin::DBIC - DBIx::Class interface for Dancer2 applications
7
9 version 0.0100
10
12 use Dancer2;
13 use Dancer2::Plugin::DBIC;
14
15 get '/users/:user_id' => sub {
16 my $user = schema('default')->resultset('User')->find(param 'user_id');
17
18 # If you are accessing the 'default' schema, then all the following
19 # are equivalent to the above:
20 $user = schema->resultset('User')->find(param 'user_id');
21 $user = resultset('User')->find(param 'user_id');
22 $user = rset('User')->find(param 'user_id');
23
24 template user_profile => {
25 user => $user
26 };
27 };
28
29 dance;
30
32 This plugin makes it very easy to create Dancer2 applications that
33 interface with databases. It automatically exports the keyword
34 "schema" which returns a DBIx::Class::Schema object. It also exports
35 the keywords "resultset" and "rset". You just need to configure your
36 database connection information. For performance, schema objects are
37 cached in memory and are lazy loaded the first time they are accessed.
38
39 This plugin is a thin wrapper around DBICx::Sugar.
40
42 Configuration can be done in your Dancer2 config file. This is a
43 minimal example. It defines one database named "default":
44
45 plugins:
46 DBIC:
47 default:
48 dsn: dbi:SQLite:dbname=some.db
49
50 In this example, there are 2 databases configured named "default" and
51 "foo":
52
53 plugins:
54 DBIC:
55 default:
56 dsn: dbi:SQLite:dbname=some.db
57 schema_class: MyApp::Schema
58 foo:
59 dsn: dbi:mysql:foo
60 schema_class: Foo::Schema
61 user: bob
62 password: secret
63 options:
64 RaiseError: 1
65 PrintError: 1
66
67 Each database configured must at least have a dsn option. The dsn
68 option should be the DBI driver connection string. All other options
69 are optional.
70
71 If you only have one schema configured, or one of them is named
72 "default", you can call "schema" without an argument to get the only or
73 "default" schema, respectively.
74
75 If a schema_class option is not provided, then
76 DBIx::Class::Schema::Loader will be used to dynamically load the schema
77 by introspecting the database corresponding to the dsn value. Remember
78 that you need DBIx::Class::Schema::Loader installed to take advantage
79 of that.
80
81 The schema_class option, should be a proper Perl package name that
82 Dancer2::Plugin::DBIC will use as a DBIx::Class::Schema class.
83 Optionally, a database configuration may have user, password, and
84 options parameters as described in the documentation for "connect()" in
85 DBI.
86
87 You may also declare your connection information in the following
88 format (which may look more familiar to DBIC users):
89
90 plugins:
91 DBIC:
92 default:
93 connect_info:
94 - dbi:mysql:foo
95 - bob
96 - secret
97 -
98 RaiseError: 1
99 PrintError: 1
100
102 schema
103 my $user = schema->resultset('User')->find('bob');
104
105 The "schema" keyword returns a DBIx::Class::Schema object ready for you
106 to use. If you have configured only one database, then you can simply
107 call "schema" with no arguments. If you have configured multiple
108 databases, you can still call "schema" with no arguments if there is a
109 database named "default" in the configuration. With no argument, the
110 "default" schema is returned. Otherwise, you must provide "schema()"
111 with the name of the database:
112
113 my $user = schema('foo')->resultset('User')->find('bob');
114
115 resultset
116 This is a convenience method that will save you some typing. Use this
117 only when accessing the "default" schema.
118
119 my $user = resultset('User')->find('bob');
120
121 is equivalent to:
122
123 my $user = schema->resultset('User')->find('bob');
124
125 rset
126 my $user = rset('User')->find('bob');
127
128 This is simply an alias for "resultset".
129
131 There are two approaches for generating schema classes. You may
132 generate your own DBIx::Class classes and set the corresponding
133 "schema_class" setting in your configuration as shown above. This is
134 the recommended approach for performance and stability.
135
136 It is also possible to have schema classes dynamically generated if you
137 omit the "schema_class" configuration setting. This requires you to
138 have DBIx::Class::Schema::Loader installed. The "v7" naming scheme
139 will be used for naming the auto generated classes. See "naming" in
140 DBIx::Class::Schema::Loader::Base for more information about naming.
141
142 For generating your own schema classes, you can use the dbicdump
143 command line tool provided by DBIx::Class::Schema::Loader to help you.
144 For example, if your app were named Foo, then you could run the
145 following from the root of your project directory:
146
147 dbicdump -o dump_directory=./lib Foo::Schema dbi:SQLite:/path/to/foo.db
148
149 For that example, your "schema_class" setting would be "Foo::Schema".
150
152 · DBICx::Sugar
153
155 · Alexis Sukrieh <sukria@sukria.net>
156
157 · Dagfinn Ilmari Mannsåker <<https://github.com/ilmari>>
158
159 · David Precious <davidp@preshweb.co.uk>
160
161 · ennio <<https://github.com/scriplit>>
162
163 · Fabrice Gabolde <<https://github.com/fgabolde>>
164
165 · Franck Cuny <franck@lumberjaph.net>
166
167 · Steven Humphrey <<https://github.com/shumphrey>>
168
169 · Yanick Champoux <<https://github.com/yanick>>
170
172 Naveed Massjouni <naveed@vt.edu>
173
175 This software is copyright (c) 2013 by Naveed Massjouni.
176
177 This is free software; you can redistribute it and/or modify it under
178 the same terms as the Perl 5 programming language system itself.
179
180
181
182perl v5.30.1 2020-01-29 Dancer2::Plugin::DBIC(3)