File Coverage

blib/lib/Search/Typesense/Document.pm
Criterion Covered Total %
statement 11 43 25.5
branch 0 4 0.0
condition n/a
subroutine 4 10 40.0
pod 5 5 100.0
total 20 62 32.2


line stmt bran cond sub pod time code
1             package Search::Typesense::Document;
2              
3 4     4   49 use v5.16.0;
  4         15  
4              
5 4     4   20 use Moo;
  4         7  
  4         25  
6             with qw(Search::Typesense::Role::Request);
7              
8 4     4   1619 use Mojo::JSON qw(decode_json encode_json);
  4         8  
  4         292  
9 4         37 use Search::Typesense::Types qw(
10             ArrayRef
11             Enum
12             HashRef
13             InstanceOf
14             NonEmptyStr
15             Str
16             compile
17 4     4   404 );
  4         10  
18              
19             =head1 NAME
20              
21             Search::Typesense::Document - CRUD for Typesense documents
22              
23             =head1 SYNOPSIS
24              
25             my $typesense = Search::Typesense->new(
26             host => $host,
27             api_key => $key,
28             );
29             my $documents = $typesense->documents;
30              
31             The instantiation of this module is for internal use only. The methods are
32             public.
33              
34             =cut
35              
36             our $VERSION = '0.07';
37              
38             =head2 C<create>
39              
40             my $document = $typesense->documents->create($collection, \%data);
41              
42             Arguments and response as shown at L<https://typesense.org/docs/0.19.0/api/#index-document>
43              
44             =cut
45              
46             sub create {
47 0     0 1   my ( $self, $collection, $document ) = @_;
48 0           state $check = compile( NonEmptyStr, HashRef );
49 0           ( $collection, $document ) = $check->( $collection, $document );
50 0           return $self->_POST(
51             path => [ 'collections', $collection, 'documents' ],
52             body => $document
53             );
54             }
55              
56             =head2 C<upsert>
57              
58             my $document = $typesense->documents->upsert($collection, \%data);
59              
60             Arguments and response as shown at L<https://typesense.org/docs/0.19.0/api/#upsert>
61              
62             =cut
63              
64             sub upsert {
65 0     0 1   my ( $self, $collection, $document ) = @_;
66 0           state $check = compile( NonEmptyStr, HashRef );
67 0           ( $collection, $document ) = $check->( $collection, $document );
68              
69 0           return $self->_POST(
70             path => [ 'collections', $collection, 'documents' ],
71             body => $document,
72             query => { action => 'upsert' },
73             );
74             }
75              
76             =head2 C<update>
77              
78             my $document = $typesense->documents->update($collection, $document_id, \%data);
79              
80             Arguments and response as shown at L<https://typesense.org/docs/0.19.0/api/#update-document>
81              
82             =cut
83              
84             sub update {
85 0     0 1   my ( $self, $collection, $document_id, $updates ) = @_;
86 0           state $check = compile( NonEmptyStr, NonEmptyStr, HashRef );
87 0           ( $collection, $document_id, $updates )
88             = $check->( $collection, $document_id, $updates );
89 0           return $self->_PATCH(
90             path => [ 'collections', $collection, 'documents', $document_id ],
91             body => $updates
92             );
93             }
94              
95             =head2 C<delete>
96              
97             my $document = $typesense->documents->delete($collection_name, $document_id);
98              
99             Arguments and response as shown at L<https://typesense.org/docs/0.19.0/api/#delete-document>
100              
101             =cut
102              
103             sub delete {
104 0     0 1   my ( $self, $collection, $document_id ) = @_;
105 0           state $check = compile( NonEmptyStr, NonEmptyStr );
106 0           ( $collection, $document_id ) = $check->( $collection, $document_id );
107 0           return $self->_DELETE(
108             path => [ 'collections', $collection, 'documents', $document_id ] );
109             }
110              
111             =head2 C<export>
112              
113             my $export = $typesense->documents->export($collection_name);
114              
115             Response as shown at L<https://typesense.org/docs/0.19.0/api/#export-documents>
116              
117             (An arrayref of hashrefs)
118              
119             =cut
120              
121             sub export {
122 0     0 1   my ( $self, $collection ) = @_;
123 0           state $check = compile(NonEmptyStr);
124 0           ($collection) = $check->($collection);
125 0 0         my $tx = $self->_GET(
126             path => [ 'collections', $collection, 'documents', 'export' ],
127             return_transaction => 1
128             ) or return; # 404
129 0           return [ map { decode_json($_) } split /\n/ => $tx->res->body ];
  0            
130             }
131              
132             =head2 C<import>
133              
134             my $response = $typesense->documents->import(
135             $collection_name,
136             $action,
137             \@documents,
138             );
139              
140             Response as shown at L<https://typesense.org/docs/0.19.0/api/#import-documents>
141              
142             C<$action> must be one of C<create>, C<update>, or C<upsert>.
143              
144             =cut
145              
146             sub import {
147 0     0     my $self = shift;
148 0           state $check = compile(
149             NonEmptyStr,
150             Enum [qw/create upsert update/],
151             ArrayRef [HashRef],
152             );
153 0           my ( $collection, $action, $documents ) = $check->(@_);
154 0           my $body = join "\n" => map { encode_json($_) } @$documents;
  0            
155              
156 0           my $tx = $self->_POST(
157             path => [ 'collections', $collection, 'documents', "import" ],
158             body => $body,
159             query => { action => $action },
160             return_transaction => 1,
161             );
162 0           my $response = $tx->res->json;
163 0 0         if ( exists $response->{success} ) {
164 0           $response->{success} += 0;
165             }
166 0           return $response;
167             }
168              
169             1;
170