File Coverage

blib/lib/Search/Typesense/Document.pm
Criterion Covered Total %
statement 42 43 97.6
branch 3 4 75.0
condition n/a
subroutine 10 10 100.0
pod 5 5 100.0
total 60 62 96.7


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