| 1 | package Mojo::CouchDB; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 2 | ▶ | use Mojo::Base -base; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 2 | ▶ | use Mojo::CouchDB::DB; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 2 | ▶ | use Carp qw(croak); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 2 | ▶ | use MIME::Base64 qw(encode_base64); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | 2 | our $VERSION = '1.0'; | |||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | 2 | has 'url'; | |||||||||||||||||||||||||||
| 11 | 2 | has 'auth'; | |||||||||||||||||||||||||||
| 12 | 2 | ▶ | has dbs => sub { state $dbs = {} }; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | 2 | ▶ | sub db { | ||||||||||||||||||||||||||
|
Subroutine
db: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||||||||||||||||||||
| 15 | 2 | my $self = shift; | |||||||||||||||||||||||||||
| 16 | 2 | my $db_name = shift; | |||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | 2 | ▶ | return $self->dbs->{$db_name} if exists $self->dbs->{$db_name}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | 2 | my $db = Mojo::CouchDB::DB->new($self->url->path($db_name), $self->auth); | |||||||||||||||||||||||||||
| 21 | 2 | $self->dbs->{$db_name} = $db; | |||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | 2 | return $db; | |||||||||||||||||||||||||||
| 24 | } | ||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | 2 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=6 SCAR=19.1
Pod
documented
| |||||||||||||||||||||||||||||
| 27 | 2 | my $self = shift->SUPER::new; | |||||||||||||||||||||||||||
| 28 | 2 | my $str = shift; | |||||||||||||||||||||||||||
| 29 | 2 | my $username = shift; | |||||||||||||||||||||||||||
| 30 | 2 | my $password = shift; | |||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | 2 | ▶ | return $self unless $str; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | 2 | ▶ | chop $str if substr($str, -1) eq '/'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | 2 | my $url = Mojo::URL->new($str); | |||||||||||||||||||||||||||
| 37 | 2 | ▶ | croak "Invalid CouchDB URI string $str" unless $url->protocol =~ /^http(?:s)?$/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | 2 | ▶ | chomp($self->{auth} = 'Basic ' . encode_base64("$username:$password")) | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$username and $password
MC/DC0%0/2
$username and $password
$username
$password
Truth table33%1/3
A and B
A= $usernameB= $password
| |||||||||||||||||||||||||||||
| 40 | if $username and $password; | ||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | 2 | $self->{url} = $url; | |||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | 2 | return $self; | |||||||||||||||||||||||||||
| 45 | } | ||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | 2 | 1; | |||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | =encoding utf8 | ||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | =head1 NAME | ||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | Mojo::CouchDB | ||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | use Mojo::CouchDB; | ||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | # Create a CouchDB instance | ||||||||||||||||||||||||||||
| 60 | my $couch = Mojo::CouchDB->new('http://localhost:6984', 'username', 'password'); | ||||||||||||||||||||||||||||
| 61 | my $db = $couch->db('books'); | ||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | $db->create_db; # Create the database on the server | ||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | # Make a document | ||||||||||||||||||||||||||||
| 66 | my $book = { | ||||||||||||||||||||||||||||
| 67 | title => 'Nineteen Eighty Four', | ||||||||||||||||||||||||||||
| 68 | author => 'George Orwell' | ||||||||||||||||||||||||||||
| 69 | }; | ||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | # Save your document to the database | ||||||||||||||||||||||||||||
| 72 | $book = $db->save($book); | ||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | # If _id is assigned to a hashref, save will update rather than create | ||||||||||||||||||||||||||||
| 75 | say $book->{_id}; # Assigned when saving or getting | ||||||||||||||||||||||||||||
| 76 | $book->{title} = 'Dune'; | ||||||||||||||||||||||||||||
| 77 | $book->{author} = 'Frank Herbert' | ||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||
| 79 | # Re-save to update the document | ||||||||||||||||||||||||||||
| 80 | $book = $db->save($book); | ||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | # Get the document as a hashref | ||||||||||||||||||||||||||||
| 83 | my $dune = $db->get($book->{_id}); | ||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | # You can also save many documents at a time | ||||||||||||||||||||||||||||
| 86 | my $books = $db->save_many([{title => 'book', author => 'John'}, { title => 'foo', author => 'bar' }])->{docs}; | ||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | =head2 db | ||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | my $db = $couch->db('books'); | ||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||
| 92 | Create an instance of L<"Mojo::CouchDB::DB"> that corresponds to the database name specified in the first parameter. | ||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | =head2 new | ||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | my $url = 'https://127.0.0.1:5984'; | ||||||||||||||||||||||||||||
| 97 | my $couch = Mojo::CouchDB->new($url, $username, $password); | ||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||
| 99 | 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. | ||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||
| 101 | =head1 API | ||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||
| 103 | =over 2 | ||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | =item * L<Mojo::CouchDB> | ||||||||||||||||||||||||||||
| 106 | =item * L<Mojo::CouchDB::DB> | ||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | =back | ||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||
| 110 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||
| 112 | Rawley Fowler, C<rawleyfowler@proton.me>. | ||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | =head1 CREDITS | ||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||
| 116 | =over 2 | ||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||
| 118 | =back | ||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||
| 120 | =head1 LICENSE | ||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||
| 122 | Copyright (C) 2023, Rawley Fowler and contributors. | ||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||
| 124 | This program is free software, you can redistribute it and/or modify it under the terms of the Artistic License version 2.0. | ||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||
| 126 | =head1 SEE ALSO | ||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||
| 128 | L<https://mojolicious.org>. | ||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||
| 130 | =cut | ||||||||||||||||||||||||||||