Mojo::CouchDB

A Mojolicious wrapper around Mojo::UserAgent that makes using CouchDB from Perl, a lot of fun.

use Mojo::CouchDB;

# Create a CouchDB instance
my $couch = Mojo::CouchDB->new('http://localhost:6984/books', 'username', 'password');

# Make a document
my $book = {
    title => 'Nineteen Eighty Four',
    author => 'George Orwell'
};

# Save your document to the database
my $save = $couch->save($book);

$book->{_id} = $save->{_id};
$book->{_rev} = $save->{_rev};

# If _id is assigned to a hashref, save will update rather than create
say $book->{_id};
say $book->{_rev};

$book->{title} = 'Dune';
$book->{author} = 'Frank Herbert'

# Re-save to update the document
$save = $couch->save($book);

say $save->{_id}; # Same id as above

# Get the document as a hashref
my $dune = $couch->find({ _id => $book->{_id} })->{docs}->[0];

Installation

$ cpanm Mojo::CouchDB

Basics

This is an example of a Mojolicious::Lite application using Mojo::CouchDB.

use Mojolicious::Lite -signatures;
use Mojo::CouchDB;

helper user_db => sub {
       state $user_db = Mojo::Couch->new('http://127.0.0.1:5984/users', 'username', 'password');
};

get '/:user' => sub {
    my $c    = shift;
    my $user = $c->user_db->find({ _id => $c->param('user') })->{docs}->[0]
       || return $c->rendered(404);
    return $c->render(json => $user);
};

app->start;

It is recommended to use a helper when using Mojo::CouchDB with Mojolicious.

Author

Rawley Fowler

Credits

Sebastion Riedel (the creator of Mojolicious).

The Apache Foundation for making CouchDB.

Copyright (C) 2023, Rawley Fowler

This library is free software; you may distribute, and/or modify it under the terms of the Artistic License version 2.0.