The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.

NAME

DBIx::CouchLike - DBI based CouchDB like document database library

SYNOPSIS

use DBIx::CouchLike;
use DBI;
$dbh   = DBI->connect($dsn);
$couch = DBIx::CouchLike->new({ dbh => $dbh, table => 'foo' });

# CREATE
$id = $couch->post({ name => 'animal', tags => ['dog', 'cat']});

# CREATE (with id)
$couch->post( $id => { name => 'animal', tags => ['dog', 'cat']} );
# or
$couch->post({ _id => $id, name => 'animal', tags => ['dog', 'cat']});

# RETRIEVE
$obj = $couch->get($id);

# UPDATE
$couch->put( $id, $obj );
# or
$couch->put( $obj ); # must be defined $obj->{_id}

# DELETE
$couch->delte($id);

# RETRIEVE all
@all     = $couch->all();
@grep    = $couch->all({ id_like => "foo%" });
@designs = $couch->all_designs();

# define VIEW
$map_sub_str = <<'END_OF_CODE';
sub {
    my ($obj, $emit) = @_;
    for my $tag ( @{ $obj->{tags} } ) {
        $emit->( $tag => $obj->{name} );
    }
}
END_OF_CODE

$reduce_sub_str = <<'END_OF_CODE';
sub {
    my ($keys, $values) = @_;
    return scalar @$values;
}
END_OF_CODE

$couch->post({
    _id      => "_design/find",
    views    => {
        tags => {
            map => $map_sub_str,
        },
        tags_count => {
            map    => $map_sub_str,
            reduce => $reduce_sub_str,
        },
    },
});
# get VIEW
@result = $couch->view("find/tags");
# is_deeply \@result => [ { key => "dog", value => "animal" },
#                         { key => "cat", value => "animal" },
#                       ]

@result = $couch->view("find/tags", { key => "cat" });
@result = $couch->view("find/tags", { include_docs => 1 });

@result = $couch->view("find/tags_count");
# is_deeply \@result => [ { key => "dog", value => 1, id => $id },
#                         { key => "cat", value => 1, id => $id },
#                       ]

# get VIEW using iterator
$itr = $couch->view("find/tags");
$result_1 = $itr->next;
$result_2 = $itr->next;

# post / put with specified views
$couch->put_with_views("find")->($obj);
$couch->post_with_views("foo", "bar")->($obj1, $obj2);

DESCRIPTION

DBIx::CouchLike is DBI based CouchDB like document database library.

METHODS

view
$itr = $couch->view( $view_name, \%options );

options:
key           => "foo",             # key = "foo"
key           => ["foo", "bar"],    # key = "foo" OR key = "bar"
key           => { "<" => "10" },   # key < 10
key_like      => "foo%",            # key LIKE "foo%"
key_reverse   => 1,                 # ORDER BY key DESC
value_reverse => 1,                 # ORDER BY value DESC
include_docs  => 1,                 # return original document with key, value pair (map only)

AUTHOR

FUJIWARA <fujiwara.shunichiro gmail.com>

SEE ALSO

LICENSE

This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.