DBIx-QuickORM
view release on metacpan or search on metacpan
my %params = @_;
my $link = $params{link};
return "obtain_" . $link->other_table if $params{link}->unique;
return "select_" . $link->other_table . "s";
};
# You can provide custom names for field accessors when using autorow
autoname field_accessor => sub {
my %params = @_;
return "get_$params{name}";
};
};
};
};
## YOUR APP CODE
package My::App;
use My::Orm qw/orm/;
# Get a connection to the orm
# Note: This will return the same connection each time, no need to cache it yourself.
# See DBIx::QuickORM::Connection for more info.
my $orm = orm('my_orm');
# See DBIx::QuickORM::Handle for more info.
my $h = $orm->handle('people', {surname => 'smith'});
for my $person ($handle->all) {
print $person->field('first_name') . "\n"
}
my $new_h = $h->limit(5)->order_by('surname')->omit(@large_fields);
my $iterator = $new_h->iterator; # Query is actually sent to DB here.
while (my $row = $iterator->next) {
...
}
# Start an async query
my $async = $h->async->iterator;
while (!$async->ready) {
do_something_else();
}
while (my $item = $iterator->next) {
...
}
See [DBIx::QuickORM::Connection](https://metacpan.org/pod/DBIx%3A%3AQuickORM%3A%3AConnection) for details on the object returned by
`my $orm = orm('my_orm');`.
See [DBIx::QuickORM::Handle](https://metacpan.org/pod/DBIx%3A%3AQuickORM%3A%3AHandle) for more details on handles, which are similar to
ResultSets from [DBIx::Class](https://metacpan.org/pod/DBIx%3A%3AClass).
# RECIPES
## DEFINE DB LATER
In some cases you may want to define your orm/schema before you have your
database credentials. Then you want to add the database later in an app/script
bootstrap process.
Schema:
package My::Schema;
use DBIx::QuickORM;
orm MyORM => sub {
autofill;
};
Bootstrap process:
package My::Bootstrap;
use DBIx::QuickORM only => [qw/db db_name host port user pass/];
use My::Schema;
sub import {
# Get the orm (the `orm => ...` param is required to prevent it from attempting a connection now)
my $orm = qorm(orm => 'MyORM');
return if $orm->db; # Already bootstrapped
my %db_params = decrypt_creds();
# Define the DB
my $db = db {
db_name 'quickdb';
host $db_params{host};
port $db_params{port};
user $db_params{user};
pass $db_params{pass};
};
# Set the db on the ORM:
$orm->db($db);
}
Your app:
package My::App;
# Get the qorm() subroutine
use My::Schema;
# This will do the db bootstrap
use My::Bootstrap;
# Connect to the database with the ORM
my $con = qorm('MyORM');
## RENAMING EXPORTS
When importing [DBIx::QuickORM](https://metacpan.org/pod/DBIx%3A%3AQuickORM) you can provide
`rename => { name => new_name }` mapping to rename exports.
package My::ORM;
use DBIx::QuickORM rename => {
pass => 'password',
user => 'username',
This will override all previous attributes, it does not merge.
db mydb => sub {
attributes { foo => 1 };
};
Or:
db mydb => sub {
attributes foo => 1;
};
- `host $HOSTNAME`
- `hostname $HOSTNAME`
Provide a hostname or IP address for database connections
db mydb => sub {
host 'mydb.mydomain.com';
};
- `port $PORT`
Provide a port number for database connection.
db mydb => sub {
port 1234;
};
- `socket $SOCKET_PATH`
Provide a socket instead of a host+port
db mydb => sub {
socket '/path/to/db.socket';
};
- `user $USERNAME`
- `username $USERNAME`
provide a database username
db mydb => sub {
user 'bob';
};
- `pass $PASSWORD`
- `password $PASSWORD`
provide a database password
db mydb => sub {
pass 'hunter2'; # Do not store any real passwords in plaintext in code!!!!
};
- `creds sub { return \%CREDS }`
Allows you to provide a coderef that will return a hashref with all the
necessary database connection fields.
This is mainly useful if you credentials are in an encrypted YAML or JSON file
and you have a method to decrypt and read it returning it as a hash.
db mydb => sub {
creds sub { ... };
};
- `connect sub { ... }`
- `connect \&connect`
Instead of providing all the other fields, you may specify a coderef that
returns a [DBI](https://metacpan.org/pod/DBI) connection.
**IMPORTANT:** This function must always return a new [DBI](https://metacpan.org/pod/DBI) connection it
**MUST NOT** cache it!
sub mydb => sub {
connect sub { ... };
};
- `dsn $DSN`
Specify the DSN used to connect to the database. If not provided then an
attempt will be made to construct a DSN from other parameters, if they are
available.
db mydb => sub {
dsn "dbi:Pg:dbname=foo";
};
- `server $NAME => sub { ... }`
Used to define a server with multiple databases. This is a way to avoid
re-specifying credentials for each database you connect to.
You can use `db('server_name.db_name')` to fetch the database.
Basically this allows you to specify any database fields once in the server, then
define any number of databases that inherit them.
Example:
server pg => sub {
host 'pg.myapp.com';
user $USER;
pass $PASS;
attributes { work_well => 1 }
db 'myapp'; # Points at the 'myapp' database on this db server
db 'otherapp'; # Points at the 'otherapp' database on this db server
# You can also override any if a special db needs slight modifications.
db special => sub {
attributes { work_well => 0, work_wrong => 1 };
};
};
orm myapp => sub {
db 'pg.myapp';
...;
};
orm otherapp => sub {
db 'pg.otherapp';
...;
};
- `schema $NAME => sub { ... }`
- `$schema = schema($NAME)`
- `$schema = schema($NAME => sub { ... })`
Used to either fetch or define a schema.
When called with only 1 argument it will fetch the schema with the given name.
When used inside an ORM builder it will set the schema for the ORM (all ORMs
have exactly one schema).
When called with 2 arguments it will define the schema using the coderef as a
builder.
When called in a non-void context it will return the compiled schema, otherwise
it adds it to the ORM class.
# Define the 'foo' schema:
schema foo => sub {
table a => sub { ... };
table b => sub { ... };
};
# Fetch it:
my $foo = schema('foo');
# Define and compile one:
( run in 1.054 second using v1.01-cache-2.11-cpan-39bf76dae61 )