| 1 | package Search::Typesense::Document; | ||||||||||
| 2 | |||||||||||
| 3 | # ABSTRACT: CRUD for Typesense documents | ||||||||||
| 4 | |||||||||||
| 5 | 4 | ▶ | use v5.16.0; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | |||||||||||
| 7 | 4 | ▶ | use Moo; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | 4 | with qw(Search::Typesense::Role::Request); | |||||||||
| 9 | |||||||||||
| 10 | 4 | ▶ | use Mojo::JSON qw(decode_json encode_json); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 11 | 4 | ▶ | use Search::Typesense::Types qw( | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 12 | ArrayRef | ||||||||||
| 13 | Enum | ||||||||||
| 14 | HashRef | ||||||||||
| 15 | InstanceOf | ||||||||||
| 16 | NonEmptyStr | ||||||||||
| 17 | Str | ||||||||||
| 18 | compile | ||||||||||
| 19 | 4 | ); | |||||||||
| 20 | |||||||||||
| 21 | |||||||||||
| 22 | 4 | our $VERSION = '0.08'; | |||||||||
| 23 | |||||||||||
| 24 | |||||||||||
| 25 | 2 | ▶ | sub create { | ||||||||
|
Subroutine
create: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 26 | 2 | my ( $self, $collection, $document ) = @_; | |||||||||
| 27 | 2 | state $check = compile( NonEmptyStr, HashRef ); | |||||||||
| 28 | 2 | ( $collection, $document ) = $check->( $collection, $document ); | |||||||||
| 29 | 2 | return $self->_POST( | |||||||||
| 30 | path => [ 'collections', $collection, 'documents' ], | ||||||||||
| 31 | body => $document | ||||||||||
| 32 | ); | ||||||||||
| 33 | } | ||||||||||
| 34 | |||||||||||
| 35 | |||||||||||
| 36 | 2 | ▶ | sub upsert { | ||||||||
|
Subroutine
upsert: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 37 | 2 | my ( $self, $collection, $document ) = @_; | |||||||||
| 38 | 2 | state $check = compile( NonEmptyStr, HashRef ); | |||||||||
| 39 | 2 | ( $collection, $document ) = $check->( $collection, $document ); | |||||||||
| 40 | |||||||||||
| 41 | 2 | return $self->_POST( | |||||||||
| 42 | path => [ 'collections', $collection, 'documents' ], | ||||||||||
| 43 | body => $document, | ||||||||||
| 44 | query => { action => 'upsert' }, | ||||||||||
| 45 | ); | ||||||||||
| 46 | } | ||||||||||
| 47 | |||||||||||
| 48 | |||||||||||
| 49 | 1 | ▶ | sub update { | ||||||||
|
Subroutine
update: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 50 | 1 | my ( $self, $collection, $document_id, $updates ) = @_; | |||||||||
| 51 | 1 | state $check = compile( NonEmptyStr, NonEmptyStr, HashRef ); | |||||||||
| 52 | 1 | ( $collection, $document_id, $updates ) | |||||||||
| 53 | = $check->( $collection, $document_id, $updates ); | ||||||||||
| 54 | 1 | return $self->_PATCH( | |||||||||
| 55 | path => [ 'collections', $collection, 'documents', $document_id ], | ||||||||||
| 56 | body => $updates | ||||||||||
| 57 | ); | ||||||||||
| 58 | } | ||||||||||
| 59 | |||||||||||
| 60 | |||||||||||
| 61 | 1 | ▶ | sub delete { | ||||||||
|
Subroutine
delete: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 62 | 1 | my ( $self, $collection, $document_id ) = @_; | |||||||||
| 63 | 1 | state $check = compile( NonEmptyStr, NonEmptyStr ); | |||||||||
| 64 | 1 | ( $collection, $document_id ) = $check->( $collection, $document_id ); | |||||||||
| 65 | 1 | return $self->_DELETE( | |||||||||
| 66 | path => [ 'collections', $collection, 'documents', $document_id ] ); | ||||||||||
| 67 | } | ||||||||||
| 68 | |||||||||||
| 69 | |||||||||||
| 70 | 2 | ▶ | sub export { | ||||||||
|
Subroutine
export: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||
| 71 | 2 | my ( $self, $collection ) = @_; | |||||||||
| 72 | 2 | state $check = compile(NonEmptyStr); | |||||||||
| 73 | 2 | ($collection) = $check->($collection); | |||||||||
| 74 | 2 | ▶ | my $tx = $self->_GET( | ||||||||
|
Branch: 1 branch
| |||||||||||
| 75 | path => [ 'collections', $collection, 'documents', 'export' ], | ||||||||||
| 76 | return_transaction => 1 | ||||||||||
| 77 | ) or return; # 404 | ||||||||||
| 78 | 1 | return [ map { decode_json($_) } split /\n/ => $tx->res->body ]; | |||||||||
| 79 | } | ||||||||||
| 80 | |||||||||||
| 81 | |||||||||||
| 82 | 1 | ▶ | sub import { | ||||||||
|
Subroutine
import: called CC=2 SCAR=7.0
| |||||||||||
| 83 | 1 | my $self = shift; | |||||||||
| 84 | 1 | state $check = compile( | |||||||||
| 85 | NonEmptyStr, | ||||||||||
| 86 | Enum [qw/create upsert update/], | ||||||||||
| 87 | ArrayRef [HashRef], | ||||||||||
| 88 | ); | ||||||||||
| 89 | 1 | my ( $collection, $action, $documents ) = $check->(@_); | |||||||||
| 90 | 1 | my $body = join "\n" => map { encode_json($_) } @$documents; | |||||||||
| 91 | |||||||||||
| 92 | 1 | my $tx = $self->_POST( | |||||||||
| 93 | path => [ 'collections', $collection, 'documents', "import" ], | ||||||||||
| 94 | body => $body, | ||||||||||
| 95 | query => { action => $action }, | ||||||||||
| 96 | return_transaction => 1, | ||||||||||
| 97 | ); | ||||||||||
| 98 | 1 | my $response = $tx->res->json; | |||||||||
| 99 | 1 | ▶ | if ( exists $response->{success} ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 100 | 0 | $response->{success} += 0; | |||||||||
| 101 | } | ||||||||||
| 102 | 1 | return $response; | |||||||||
| 103 | } | ||||||||||
| 104 | |||||||||||
| 105 | 4 | 1; | |||||||||
| 106 | |||||||||||
| 107 | __END__ | ||||||||||