NAME

Mojo::CouchDB

SYNOPSIS

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
$book = $couch->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 = $couch->save($book);

# Get the document as a hashref
my $dune = $couch->get($book->{_id});

# You can also save many documents at a time
my $books = $couch->save_many([{title => 'book', author => 'John'}, { title => 'foo', author => 'bar' }])->{docs};

all_docs

$couch->all_docs({ limit => 10, skip => 5});

Retrieves a list of all of the documents in the database. This is packaged as a hashref with offset: the offset of the query, rows: the documents in the page, and total_rows: the number of rows returned in the dataset.

Optionally, can take a hashref of query parameters that correspond with the CouchDB view query specification.

all_docs_p

$couch->all_docs_p({ limit => 10, skip => 5 });

See \"all_docs", except returns the result in a Mojo::Promise.

create_db

$couch->create_db

Create the database, returns 1 if succeeds or if it already exsits, else returns undef.

find

$couch->find($search_criteria);

Searches for documents based on the search criteria specified. The search criteria hashref provided for searching must follow the CouchDB _find specification.

Returns a hashref with two fields: docs and execution_stats. docs contains an arrayref of found documents, while execution_stats contains a hashref of various statistics about the query you ran to find the documents.

find_p

$couch->find_p($search_criteria);

See \"find", except returns the result asynchronously in a Mojo::Promise.

get

$couch->get($id);

Finds a document by a given id. Dies if it can't find the document. Returns the document in hashref form.

get_p

$couch->get_p($id);

See \"get", except returns the result asynchronously in a Mojo::Promise.

index

$couch->index($idx);

Creates an index, where $idx is a hashref following the CouchDB mango specification. Returns the result of the index creation.

index_p

$couch->index_p($idx);

See \"index", except returns the result asynchronously in a Mojo::Promise.

new

my $url   = 'https://127.0.0.1:5984/my_database';
my $couch = Mojo::CouchDB->new($url, $username, $password);

Creates an instance of \"Mojo::CouchDB". The URL specified must include the protocol either http or https as well as the port your CouchDB instance is using, and the name of the database you want to manipulate.

save

$couch->save($document);

Saves a document (hashref) to the database. If the _id field is provided, it will update if it already exists. If you provide both the _id and _rev field, that specific revision will be updated. Returns a hashref that corresponds to the CouchDB POST /{db} specification.

save_p

$couch->save_p($document);

Does the same as \"save" but instead returns the result asynchronously in a Mojo::Promise.

save_many

$couch->save_many($documents);

Saves an arrayref of documents (hashrefs) to the database. Each document follows the same rules as \"save". Returns an arrayref of the documents you saved with the _id and _rev fields filled.

save_many_p

$couch->save_many_p($documents);

See \"save_many", except returns the result asynchronously in a Mojo::Promise.

API

AUTHOR

Rawley Fowler, rawleyfowler@proton.me.

CREDITS

LICENSE

Copyright (C) 2023, Rawley Fowler and contributors.

This program is free software, you can redistribute it and/or modify it under the terms of the Artistic License version 2.0.

SEE ALSO

https://mojolicious.org.