MightyNetwork :: DB (source, CPAN)

CONTENTS

NAME

MightyNetwork::DB - DB abstraction layer

Disclaimer: the objects that are mentionned here are objects as in Object-oriented programming, not as ActivityPub objects.

DESCRIPTION

Provides common methods to objects models.

Uses Mojo::Pg for database querying.

ATTRIBUTES

MightyNetwork::DB implements the following attributes.

app

A Mojolicious instance, needed to access helpers (like the database accessor)

my $app = $object->app;
$object->app(Mojolicious->new());

record

Boolean, indicates if the object has been registered in database

my $record = $object->record;
$object->record(1);

table

The name of the DB table used by the object

my $table = $object->table;
$object->table('foo');

METHODS

MightyNetwork::DB inherits all methods from Mojo::Base and implements the following new ones.

_as_struct()

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();

to_json(@to_delete)

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');

create($hash)

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()

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($hash)

Update an object attributes and update its database record.

Returns the updated object.

$object->update($hash);

delete()

Delete the object from database.

$object->delete();

find_by($field, $value, $collection)

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_by_fields($hash, $collection)

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_fields_to_attr($hash)

Map each entry of given hash table to object attributes.

Returns an object.

my $object = $c->map_fields_to_attr({'foo' => 'bar'});

REFERENCE

This is the class hierarchy of MightyNetwork::DB.

MightyNetwork::DB
MightyNetwork::DB::ActivityPub
MightyNetwork::DB::ActivityPub::Activity
MightyNetwork::DB::ActivityPub::Actor
MightyNetwork::DB::ActivityPub::Collection
MightyNetwork::DB::ActivityPub::Follow
MightyNetwork::DB::Note
MightyNetwork::DB::Notification
MightyNetwork::DB::User

SEE ALSO

MightyNetwork, Mojolicious::Base, Mojo::Pg