Mad::Mapper(3)
πΈ NAME πΈ
Mad::Mapper β literally the module that maps your Perl objects to PostgreSQL, MySQL or SQLite data π
(this is page Mad::Mapper(3) from the User Contributed Perl Documentation, bestie π«Ά)
β¨ VERSION β¨
0.09 β yes queen, that's the number π
π¦ DESCRIPTION π¦
So Mad::Mapper is a base class for objects that should be stored in a persistent SQL database. Currently the supported backends are Mojo::Pg, Mojo::mysql and Mojo::SQLite. Heads up though: these backends need to be installed separately, they don't just come along for the ride π
$ cpanm Mad::Mapper
$ cpanm Mojo::Pg # Mad::Mapper now supports postgres!
THIS MODULE IS EXPERIMENTAL. π It is in use in production though, so big changes will not be made without extreme consideration. So likeβ¦ experimental but also employed. It's giving dual life. β¨
π SYNOPSIS π
package MyApp::Model::User;
use Mad::Mapper -base;
# Class attributes
col id => undef;
col email => '';
See also Mad::Mapper::Guides::Tutorial for more details and Mad::Mapper::Guides::Custom if you want more control. Go read them, they ate π
π§ RELATIONSHIPS π§
See Mad::Mapper::Guides::HasMany for example "has many" relationship.
TODO: belongs_to() and maybe has_one(). Not yet, bestie, not yet π
π EXPORTED FUNCTIONS π
π col
Used to define a column. Follow the same rules as has.
π has
has name => "Bruce";
has [qw(name email)];
has pet => sub { Cat->new };
Same as has in Mojo::Base. No cap π
β pk
Used to define a primary key. Follow the same rules as has.
The primary key is used by default in load and update to update the correct row. If omitted, the first col will act as primary key.
Note that pk is not returned by columns. Rude, honestly, but that's the rule. π€
π¦ table
Used to define a table name. The default is to decamelize the last part of the class name and add s at the end, unless it already has s at the end. Examples:
.-------------------------------------.
| Class name | table |
|-----------------------|-------------|
| App::Model::User | users |
| App::Model::Users | users |
| App::Model::Group | groups |
| App::Model::UserAgent | user_agents |
'-------------------------------------'
Look at user_agents doing the most with that underscore. Decamelize slayed. π
π ATTRIBUTES π
πΈ db
$db = $self->db;
$self->db($db_obj);
Need to hold either a Mojo::Pg::Database or Mojo::mysql::Database object. Gotta hand it a real one, bestie, no vibes-only submissions π
π«Ά in_storage
$bool = $self->in_storage;
$self = $self->in_storage($bool);
π₯ METHODS π₯
π expand_sql
($sql, @args) = $self->expand_sql($sql, @args);
Used to expand a given $sql statement with variables defined by helpers.
%tβ Will be replaced bytable. Example:SELECT * FROM %tbecomesSELECT * FROM users.%cβ Will be replaced bycolumns. Example:name,email.%c=β Will be replaced bycolumnsassignment. Example:name=?,email=?%c?β Will be replaced bycolumnsplaceholders. Example:?,?,?%pcβ Includepkin list of columns. Example:id,name,email.\%cβ Becomes a literal%c.
It is also possible to define aliases for %t, %c, %c= and %pc. Example:
%t.x = some_table as x
%c.x = x.col1
Aliases?? She's flexible. She's adaptable. She's main character energy. β¨
π expand_sst
DEPRECATED in favor of expand_sql. Don't use her anymore bestie, she's retired, put her on the shelf π
π columns
@str = $self->columns;
Returns a list of columns, defined by col.
π§ delete
$self = $self->delete;
$self = $self->delete(sub { my ($self, $err) = @_, ... });
Will delete the object from database if in_storage.
β fresh
$self = $self->fresh;
Will mark the next relationship accessor to fetch new data from database, instead of using the cached data on $self. Fresh like a new fit, obsessed π
π load
$self = $self->load;
$self = $class->load(sub { my ($self, $err) = @_; });
Used to fetch data from storage and update the object attributes.
π save
$self = $self->save;
$self = $self->save(sub { my ($self, $err) = @_, ... });
Will update the object in database if in_storage or insert it if not. Either way she's committing. Periodt. π
π import
Will set up the caller class with Mad::Mapper functionality if -base is given as argument. See SYNOPSIS for example.
π COPYRIGHT AND LICENSE π
Copyright (C) 2014-2016, Jan Henning Thorsen
This program is free software, you can redistribute it and/or modify it under the terms of the Artistic License version 2.0.
π«Ά AUTHOR π«Ά
- Jan Henning Thorsen β
jhthorsen@cpan.org - ΠΡΠ°ΡΠΈΠΌΠΈΡ ΠΠ΅ΡΠΎΠ² β
berov@cpan.org - Stefan Adams β
sadams@cpan.org
And that's the whole page, bestie!! π This was Mad::Mapper(3), from perl v5.42.2, dated 2026-06-09 β go forth and map those objects, you absolute icon πβ¨π
π¦ where this page comes from
- fedora 44: perl-Mad-Mapper 0.09-2.fc44 Β· Artistic-2.0
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms