Skip to content

rawleyfowler/Mojo-CouchDB

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

22 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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', '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};

Installation

$ cpanm Mojo::CouchDB

Basics

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.

Author

Rawley Fowler

Credits

Sebastion Riedel (the creator of Mojolicious).

The Apache Foundation for making CouchDB.

Copyright and License

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.

About

A Mojolicious wrapper for CouchDB connections

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages