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->find({ _id => $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
C<offset>: the offset of the query, C<rows>: the documents in the page, and C<total_rows>: the number of rows returned in the dataset.
Optionally, can take a hashref of query parameters that correspond with the CouchDB L<view query specification|https://docs.couchdb.org/en/stable/api/ddoc/views.html#db-design-design-doc-view-view-name>.
all_docs_p
$couch->all_docs_p({ limit => 10, skip => 5 });
See L<\"all_docs">, except returns the result in a L<Mojo::Promise>.
create_db
$couch->create_db
Create the database, returns C<1> if succeeds or if it already exsits, else returns C<undef>.
find
$couch->find($search_criteria);
Searches for records based on the search criteria specified. The search criteria
hashref provided for searching must follow the CouchDB L<_find specification|https://docs.couchdb.org/en/stable/api/database/find.html#selector-syntax>.
Returns a hashref with two fields: C<docs> and C<execution_stats>. C<docs> contains an arrayref of found documents, while
C<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 L<\"find">, except returns the result asynchronously in a L<Mojo::Promise>.
index
$couch->index($idx);
Creates an index, where C<$idx> is a hashref following the CouchDB L<mango specification|https://docs.couchdb.org/en/stable/api/database/find.html#db-index>.
Returns the result of the index creation.
index_p
$couch->index_p($idx);
See L<\"index">, except returns the result asynchronously in a L<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 L<\"Mojo::CouchDB">. The URL specified must include the protocol either C<http> or C<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 C<_id> field is provided, it will update if it already exists.
If you provide both the C<_id> and C<_rev> field, that specific revision will be updated. Returns a hashref that corresponds
to the CouchDB C<POST /{db}> specification.
save_p
$couch->save_p($document);
Does the same as L<\"save"> but instead returns the result asynchronously in a L<Mojo::Promise>.
save_many
$couch->save_many($documents);
Saves an arrayref of documents (hashrefs) to the database. Each document follows the same rules as L<\"save">. Returns an
arrayref of the documents you saved with the C<_id> and C<_rev> fields filled.
save_many_p
$couch->save_many_p($documents);
See L<\"save_many">, except returns the result asynchronously in a L<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.