AnyEvent-CouchDB

 view release on metacpan or  search on metacpan

Changes  view on Meta::CPAN

UNRELEASED
  * see if we can load new attachment data asynchronously.
  * write more tests
  * need to improve startkey/endkey/key JSON encoding.

1.31 2013-06-05
  - merged Maroun NAJM's patch for bulk_doc properties
    http://wiki.apache.org/couchdb/HTTP_Bulk_Document_API#Transactional_Semantics_with_Bulk_Updates

1.30
  - finally fixed that annoying connection timeout problem (thanks to Walter Werner)

eg/async-via-cb  view on Meta::CPAN


use strict;
use warnings;
use AnyEvent::CouchDB;
use Data::Dump 'pp';

my $db = couchdb('bavl');

for (1..8) {
  # By giving the condvar our own callback, we can use AnyEvent::CouchDB in 
  # a truly asynchronous way.  The client will not block when called like this.
  $db->all_docs->cb(sub {
    my $data = $_[0]->recv;
    print pp($data), "\n";
  });
}

my $w  = AnyEvent->timer(after => 2, cb => sub { exit });
my $cv = AnyEvent->condvar;
$cv->recv;

lib/AnyEvent/CouchDB.pm  view on Meta::CPAN

    $headers->{'Authorization'} = $self->{http_auth};
  }

  return $headers;
}

# return a condvar and callback 
#
# - The condvar is what most of our methods return.
#   You can call recv on them to get data back, or
#   you can call cb on them to assign an asynchronous callback to
#   run WHEN the data comes back
#
# - The callback is the code that handles the 
#   generic part of every CouchDB response.  This is given
#   to AnyEvent::HTTP.
#   
sub cvcb {
  my ($options, $status, $json) = @_;
  $status ||= 200;
  $json   ||= $default_json;

lib/AnyEvent/CouchDB.pm  view on Meta::CPAN

Work with individual CouchDB documents;

  my $user = $db->open_doc('~larry')->recv;
  $user->{name} = "larry";
  $db->save_doc($user)->recv;

Query a view:

  $db->view('users/all', { startkey => 'b', endkey => 'bZZZ' })->recv

Finally, an asynchronous example:

  # Calling cb allow you to set a callback that will run when results are available.
  $db->all_docs->cb(sub {
    my ($cv) = @_;
    print pp( $cv->recv ), "\n";
  });

  # However, you have to be in an event loop at some point in time.
  AnyEvent->condvar->recv;

=head1 DESCRIPTION

AnyEvent::CouchDB is a non-blocking CouchDB client implemented on top of the
L<AnyEvent> framework.  Using this library will give you the ability to run
many CouchDB requests asynchronously, and it was intended to be used within
a L<Coro>+L<AnyEvent> environment.  However, it can also be used synchronously
if you want.

Its API is based on jquery.couch.js, but we've adapted the API slightly so that
it makes sense in an asynchronous Perl environment.

=head2 AnyEvent condvars

The main thing you have to remember is that all the data retrieval methods
return an AnyEvent condvar, C<$cv>.  If you want the actual data from the
request, there are a few things you can do.

You may have noticed that many of the examples in the SYNOPSIS call C<recv>
on the condvar.  You're allowed to do this under 2 circumstances:



( run in 0.240 second using v1.01-cache-2.11-cpan-0d8aa00de5b )