ArangoDB2

 view release on metacpan or  search on metacpan

lib/ArangoDB2/Document.pm  view on Meta::CPAN

package ArangoDB2::Document;

use strict;
use warnings;

use base qw(
    ArangoDB2::Base
);

use Data::Dumper;
use JSON::XS;

my $JSON = JSON::XS->new->utf8;



# create
#
# POST /_api/document
#
# Query Parameters
#
# collection: The collection name.
# createCollection: If this parameter has a value of true or yes, then the collection is created if it does not yet exist. Other values will be ignored so the collection must be present for the operation to succeed.
#
# return self on success, undef on failure
sub create
{
    my($self, $data, $args) = @_;
    # require data
    die "Invlalid args"
        unless ref $data eq 'HASH';
    # process args
    $args = $self->_build_args($args, ['createCollection','waitForSync']);
    # set collection name as query param
    $args->{collection} = $self->collection->name;
    # make request
    my $res = $self->arango->http->post(
        $self->api_path($self->_class),
        $args,
        $JSON->encode($data),
    ) or return;
    # copy response data to instance
    $self->_build_self($res, []);
    # set data pointer to passed in doc, which will
    # be updated by future object ops
    $self->{data} = $data;
    # register
    my $register = $self->_register;
    $self->collection->$register->{$self->name} = $self;

    return $self;
}

# createCollection
#
# get/set createCollection
sub createCollection { shift->_get_set_bool('createCollection', @_) }

# delete
#
# DELETE /_api/document/{document-handle}
sub delete
{
    my($self, $args) = @_;
    # process args
    $args = $self->_build_args($args, ['policy', 'rev', 'waitForSync']);
    # make request
    my $res = $self->arango->http->delete(
        $self->api_path($self->_class, $self->collection->name, $self->name),
        $args,
    ) or return;
    # empty data
    if (my $data = $self->data) {
        %$data = ();
    }
    # unregister
    my $register = $self->_register;
    delete $self->collection->$register->{$self->name};

    return $res;
}

# edges
#
# GET /_api/edges/{collection-id}
#
# even though this is under the edge API it is the edges for a document
# so it makes more since for this to be a method on the document that the
# edges are being retrieved for.
#
# the edges method must be called with the edge collection that the edges
# are being retrieved from.
sub edges
{
    my($self, $collection, $args) = @_;
    # set default args
    $args ||= {};
    # require valid args
    die 'Invalid Args'
        unless ref $args eq 'HASH';
    # get the edges from this document
    $args->{vertex} = join('/', $self->collection->name, $self->name);

    return $self->arango->http->get(
        $self->api_path('edges', $collection->name),



( run in 2.189 seconds using v1.01-cache-2.11-cpan-f56aa216473 )