I use CouchDB extensively in numerous Mojo webapps.  I've found the 
combination to be very fast and powerful.

I use the built-in UserAgent in the running event loop rather than any kind 
of driver.  The amount of control you have using the raw UA and 
Mojo::IOLoop functions rather than a driver just can't be beat.  The Couch 
RESTful API is relatively consistent in its response, and once you're used 
to it, it codes fast.

I had thought of contributing something to the Mojo Wiki for CouchDB, but 
just haven't had the time yet!


Here's some code examples to help you get a feel for things (all executing 
in a running event loop):

Execute a view and retrieve all of its document contents returning the JSON 
response to the callers 'delay'.

sub load_view_with_documents() {

        my $delay=shift;
        my $end=$delay->begin(0);

        my $doc_retrieve_delay=Mojo::IOLoop->delay(sub {
                my ($delay, $view_json)=@_;

                if(! $view_json) {
                        $end->(0);
                }

                my $ids=[];
                foreach my $row (@{$view_json->{'rows'}}) {
                        push($ids, $row->{'id'});
                }

                $ua->ua->post("/db/_all_docs?include_docs=true" => json => 
{ 'keys' => $ids } => sub {
                        my ($ua, $tx)=@_;

                        if($tx->error || $tx->res->code != 200) {
                                $end->(0);
                        } else {
                                $end->($tx->res->json);
                        }
                });
        });

        my $doc_retrieve_end=$doc_retrieve_delay->begin(0);
        $ua->ua->get("/db/_design/request/_view/new" => sub {
                my ($ua, $tx)=@_;

                if($tx->error || $tx->res->code != 200) {
                        $doc_retrieve_end->(0);
                } else {
                        $doc_retrieve_end->($tx->res->json);
                }
        });
}

Which would be called with the callers 'delay' to create an artificial 
'block' waiting on these results before proceeding.  However it is run 
non-blocking and won't jam up your web server.

A RESTful Mojo App rendering JSON:
sub get_event() {

        my $self=shift;

        my $key=$self->stash("key");

        
$self->ua->get("/db/_design/events/_view/by_key?key=".Mojo::JSON->encode($key)."&include_docs=true"
 
=> sub {
                my ($ua, $tx)=@_;

                if($tx->error() || $tx->res->code != 200) {
                        $self->app->log->error("View by_key failed: 
".Dumper($tx));
                        return($self->render(
                                json => {       "success" => 
Mojo::JSON->false,
                                                "message" => "Internal 
error retrieving key '$key' from database",
                                },
                                status => 500,
                        ));
                }

                # Return an empty $doc hash or the contents of our row 
document if found.
                my $doc={};
                if(defined $tx->res->json->{'rows'}->[0]->{'doc'}) {
                        $doc=$tx->res->json->{'rows'}->[0]->{'doc'};
                }

                $self->app->log->info("Returned event content for $key");
                return($self->render(
                        json => {       "success" => Mojo::JSON->true,
                                        "message" => "Successfully 
retrieved key '$omni_key' from database",
                                        "data"    => $doc,
                        },
                        status => 200,
                ));
        });

        $self->render_later();
}

The permutations are as deep as your imagination.  I have some code that 
runs 3 requests in parallel using 'delay', feeding to 1 request, then into 
2, then finally rendering a result.  All of it non-blocking and using 
Mojo::UserAgent.

So in my opinion encapsulating the flexibility the built-in UA provides 
would be a real challenge, and when you get used to the UA, IOLoop, and 
Couch native API the combination is fast, powerful and scalable.

-- 
You received this message because you are subscribed to the Google Groups 
"Mojolicious" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/mojolicious.
For more options, visit https://groups.google.com/groups/opt_out.

Reply via email to