aks-tel/couchdb_tools

A Perl module for interaton with CouchDB

โ˜… 0Forks 0Perl 6GitHub โ†—Compare

Project website โ†—

couchdbperl5

README

Complete documentation about the CouchDB API available here:
https://docs.couchdb.org/en/stable/api/index.html

Perl module:

CouchDB::CouchManager

  • list_dbs()
    Get a list of available databases

  • open_db(db_name)
    'Connect' to the DB and returns CouchDB::CouchDB

  • exists_db(db_name)
    Returns true if the DB exists on the server;

  • create_db(db_name)
    Create a new DB and return true

  • delete_db(db_name)
    Delete the specified DB with documents

  • get_db_info(db_name)
    Gets information about the database

CouchDB::CouchDB

  • exists_doc(doc_id) Returns true if document exists in the DB

  • add_doc(doc_id, doc_obj)
    Add a new document into DB

  • update_doc(doc_id, doc_obj)
    Update the document

  • delete_doc(doc_id, doc_revision)
    Delete the document

  • get_doc(doc_id)
    Get document by id

  • find_doc(mando_query)
    Find document (/_find)

  • view_doc(view_name, view_params...)
    Find document (/_view))

  • list_docs(params...) Get a list of all documents (/_all_docs)

Example:

#!/usr/bin/perl

use CouchDB;
use Try::Tiny;
use Data::Dumper;

my $mgr = CouchDB::CouchManager->new(host => '127.0.0.1', username=>'admin', password=>'admin');
try {
    my $dbs = $mgr->list_dbs();
    print("Available bases: ".Dumper($dbs)."\n");

    unless($mgr->exists_db('testdb2')) {
        $mgr->create_db('testdb2');

        my $db = $mgr->open_db("testdb2");
        $db->add_doc('001', {type => "post", title => "record 1", ival => 100, sid => time()});
        $db->add_doc('002', {type => "post", title => "record 2", ival => 200, sid => time()});
        $db->add_doc('003', {type => "post", title => "record 3", ival => 300, sid => time()});
        $db->add_doc('004', {type => "post", title => "record 4", ival => 400, sid => time()});

        $db->add_doc('005', {type => "user", title => "User One"});
        $db->add_doc('006', {type => "user", title => "User Two"});
        $db->add_doc('007', {type => "user", title => "Test Test"});

    }

    my $db = $mgr->open_db("testdb2");
    my $q = { selector => {'$and' => [ { type => 'user' }, { title => { '$regex' => "User*" } } ]} };
    my $tt = $db->find_doc($q);
    print("Found: ".Dumper($tt)."\n");

} catch {
    warn $_;
}

Issues