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', 'username', 'password');
my $db = $couch->db('books');
$db->create_db; # Create the database on the server
# Make a document
my $book = {
title => 'Nineteen Eighty Four',
author => 'George Orwell'
};
# Save your document to the database
$book = $db->save($book);
# If _id is assigned to a hashref, save will update rather than create
say $book->{_id}; # Assigned when saving or getting
$book->{title} = 'Dune';
$book->{author} = 'Frank Herbert'
# Re-save to update the document
$book = $db->save($book);
# Get the document as a hashref
my $dune = $db->get($book->{_id});
# You can also save many documents at a time
my $books = $db->save_many([{title => 'book', author => 'John'}, { title => 'foo', author => 'bar' }])->{docs};
$ cpanm Mojo::CouchDB
This is an example of a Mojolicious::Lite application using Mojo::CouchDB.
use v5.36;
use Mojolicious::Lite -signatures;
use Mojo::CouchDB;
my $couch = Mojo::Couch->new('http://127.0.0.1:5984', 'username', 'password');
helper user_db => sub { state $user_db = $couch->db('users') };
get '/:user' => sub {
my $c = shift;
my $user = $c->user_db->get($c->param('user'))
|| return $c->rendered(404);
return $c->render(json => $user);
};
app->start;
It is recommended to use a helper when using Mojo::CouchDB with Mojolicious.
Rawley Fowler
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.