AnyEvent::CouchDB::Database(3p)

NAME
AnyEvent::CouchDB::Database - an object representing a CouchDB database

SYNOPSIS
  use AnyEvent::CouchDB;
  $db = couchdb('bavl');
  my $map = 'function(doc){
    if(doc.type == "Phrase"){ emit(null, doc) }
  }';
  my $phrases = $db->query($map)->recv;
  my $recordings = $db->view('recordings/all')->recv;

DESCRIPTION
Objects of this class represent a single CouchDB database. This object is used create and drop databases as well as operate on the documents within the database.

API

  General
$db = AnyEvent::CouchDB::Database->new($name, $uri)

This method takes a name and a URI, and constructs an object representing a CouchDB database. The name should be conservative in the characters it uses, because it needs to be both URI friendly and portable across filesystems. Also, the URI that you pass in should contain a trailing slash.

$db->name

This method returns the name of the database.

$db->uri

This method returns the base URI of the database.

$db->json_encoder([ $json_encoder ])

This method is a mutator for setting a custom JSON encoder. You should pass in an object that responds to "encode" and "decode". Instances of JSON and JSON::XS are good candidates.

  Options
All the methods that accept an optional hashref of options can set an "headers" key, wich will be added to all the requests. So you can add basic authentication to your requests if needed:

  my $couchdb = couch("http://127.0.0.1:5984/");
  my $db      = $couchdb->db("mydb");
  my $auth    = encode_base64('user:s3kr3t', '');

  my $res = $db->create({headers => {'Authorization' => 'Basic '.$aut}})->recv;

UPDATE: You can now make authenticated requests by placing the username and password in the URI.

  my $db = couchdb('http://user:s3kr3t@127.0.0.1:5984/mydb');

  Database Level Operations
$cv = $db->create([ \%options ])

This method is used to create a CouchDB database. It returns an AnyEvent condvar.

$cv = $db->drop([ \%options ])

This method is used to drop a CouchDB database, and it returns a condvar.

$cv = $db->info([ \%options ])

This method is used to request a hashref of info about the current CouchDB database, and it returns a condvar.

$cv = $db->compact([ \%options ])

This method is used to request that the current CouchDB database be compacted, and it returns a condvar.

  Document Level Operations
$cv = $db->open_doc($id, [ \%options ])

This method is used to request a single CouchDB document by its "id", and it returns a condvar.

$cv = $db->open_docs($ids, [ \%options ])

This method is used to request multiple CouchDB documents by their "ids", and it returns a condvar.

$cv = $db->save_doc($doc, [ \%options ])

This method can be used to either create a new CouchDB document or update an existing CouchDB document. It returns a condvar.

Note that upon success, $doc will have its "_id" and "_rev" keys updated. This allows you to save $doc repeatedly using the same hashref.

$cv = $db->remove_doc($doc, [ \%options ])

This method is used to remove a document from the database, and it returns a condvar.

$cv = $db->attach($doc, $attachment, \%options)

This method adds an attachment to a document, and it returns a condvar. Note that the %options are NOT optional for this method. You must provide a "src" for the data which should be a path that can be understood by IO::All. You must also provide a MIME content "type" for this data. If none is provided, it'll default to "text/plain".

Example:

  $db->attach($doc, "issue.net", {
    src  => '/etc/issue.net',
    type => 'text/plain'
  })->recv;

$cv = $db->detach($doc, $attachment, [ \%options ])

This method removes an attachment from a document, and it returns a condvar.

Example:

  $db->detach($doc, "issue.net")->recv;

$cv = $db->open_attachment($doc, $attachment)

This method retrieves an attachment and returns the contents as a condvar.

Example:

  my($body, $headers) = $db->open_attachment($doc, "issue.net")->recv;
  my $content_type    = $headers->{'content-type'};

$cv = $db->bulk_docs(\@docs, [ \%options ])

This method requests that many create, update, and delete operations be performed in one shot. You pass it an arrayref of documents, and it'll return a condvar.

  Database Queries
$cv = $db->view($name, [ \%options ])

This method lets you query views that have been predefined in CouchDB design documents. You give it a name which is of the form "$design_doc/$view", and you may pass in "\%options" as well to manipulate the result-set.

This method returns a condvar.

$cv = $db->all_docs([ \%options ])

This method is used to request a hashref that contains an index of all the documents in the database. Note that you DO NOT get the actual documents. Instead, you get their "id"s, so that you can fetch them later. To get the documents in the result, set parameter "include_docs" to 1 in the "\%options".

$cv = $db->all_docs_by_seq([ \%options ])

This method is similar to the "all_docs" method, but instead of using document ids as a key, it uses update sequence of the document instead. (The update_seq is an integer that is incremented every time the database is updated. You can get the current update_seq of a database by calling "info".)

This method returns a condvar.

$cv = $db->query($map, [ $reduce ], [ $language ], [ \%options ])

This method lets you send ad-hoc queries to CouchDB. You have to at least give it a map function. If you pass in a string, it'll assume the function is written in JavaScript (unless you tell it otherwise). If you pass in a coderef, it will be turned into a string, and you had better have a Perl-based view server (like CouchDB::View) installed. The same goes for the optional reduce function. The 3rd parameter lets you explicitly tell this method what language the map and reduce functions are written in. The final parameter, "\%options", can be used to manipulate the result-set in standard ways.

This method returns a condvar.

  Generic HTTP Methods
$cv = $db->head($path, [ \%options ])

$cv = $db->get($path, [ \%options ])

$cv = $db->post($path, [ \%options ])

$cv = $db->put($path, [ \%options ])

$cv = $db->delete($path, [ \%options ])

AUTHOR
John BEPPU <beppu@cpan.org>

COPYRIGHT
Copyright (c) 2008-2011 John BEPPU <beppu@cpan.org>.

  The "MIT" License
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.