MightyNetwork::DB - DB abstraction layer
Disclaimer: the objects that are mentionned here are objects as in Object-oriented programming, not as ActivityPub objects.
Provides common methods to objects models.
Uses Mojo::Pg for database querying.
MightyNetwork::DB implements the following attributes.
A Mojolicious instance, needed to access helpers (like the database accessor)
my $app = $object->app;
$object->app(Mojolicious->new());
Boolean, indicates if the object has been registered in database
my $record = $object->record;
$object->record(1);
The name of the DB table used by the object
my $table = $object->table;
$object->table('foo');
MightyNetwork::DB inherits all methods from Mojo::Base and implements the following new ones.
Private method.
Transform an object in a hash table.
Attributes app
, record
and table
are removed from the hash table.
my $hash = $object->_as_struct();
Transform an object in a JSON hash table.
Attributes listed in @to_delete
argument will be removed from the JSON hash table.
my $json = $object->to_json('foo', 'bar');
Insert $hash
values into the object table and return the object with the values of the created record.
The returned object has record
attribute to 1.
my $created_object = $object->create({ foo => 'bar' });
Write the data of the object in database. Create the record if not existing, update it otherwise.
Returns the object with record
attribute set to 1.
$object->write();
Update an object attributes and update its database record.
Returns the updated object.
$object->update($hash);
Delete the object from database.
$object->delete();
Wrapper of find_by_fields method.
Find records whose $field
attribute is equal to $value
.
Returns an object with record
attribute set to 1 if $collection
is not true and if there is only one record matching.
Returns a collection of matching objects if $collection
is true.
Returns undef
if no record is found.
my $object = $c->find_by('foo', 'bar');
my $collection = $c->find_by('foo', 'bar', 1);
Find records whose attributes matches the hash table argument.
Returns an object with record
attribute set to 1 if $collection
is not true and if there is only one record matching.
Returns a collection of matching objects if $collection
is true.
Returns undef
if no record is found.
my $object = $c->find_by({'foo' => 'bar'});
my $collection = $c->find_by({'foo' => 'bar'}, 1);
Map each entry of given hash table to object attributes.
Returns an object.
my $object = $c->map_fields_to_attr({'foo' => 'bar'});
This is the class hierarchy of MightyNetwork::DB.