File Coverage

blib/lib/Search/Typesense/Collection.pm
Criterion Covered Total %
statement 11 46 23.9
branch 0 12 0.0
condition 0 2 0.0
subroutine 4 9 44.4
pod 5 5 100.0
total 20 74 27.0


line stmt bran cond sub pod time code
1             package Search::Typesense::Collection;
2              
3 4     4   68 use v5.16.0;
  4         17  
4              
5 4     4   23 use Moo;
  4         9  
  4         26  
6             with qw(
7             Search::Typesense::Role::Request
8             Search::Typesense::Role::UserAgentInterface
9             );
10              
11 4     4   1823 use Carp 'croak';
  4         11  
  4         292  
12 4         52 use Search::Typesense::Types qw(
13             HashRef
14             InstanceOf
15             NonEmptyStr
16             Str
17             compile
18 4     4   53 );
  4         12  
19              
20             =head1 NAME
21              
22             Search::Typesense::Collection - CRUD for Typesense collections
23              
24             =head1 SYNOPSIS
25              
26             my $typesense = Search::Typesense->new(
27             host => $host,
28             api_key => $key,
29             );
30             my $collections = $typesense->collections;
31              
32             The instantiation of this module is for internal use only. The methods are
33             public.
34              
35             =head2 C<get>
36              
37             if ( my $collections = $typesense->collections->get ) {
38             # returns all collections
39             }
40             if ( my $collections = $typesense->collections->get($collection_name) ) {
41             # returns collection matching $collection_name, if any
42             }
43              
44             Response shown at L<https://typesense.org/docs/0.19.0/api/#retrieve-collection>
45              
46             =cut
47              
48             our $VERSION = '0.06';
49              
50             =head2 C<search>
51              
52             my $results = $typesense->collections->search($collection_name, {q => 'London'});
53              
54             The parameters for C<$query> are defined at
55             L<https://typesense.org/docs/0.19.0/api/#search-collection>, as are the results.
56              
57             Unlike other methods, if we find nothing, we still return the data structure
58             (instead of C<undef> instead of a 404 exception).
59              
60             =cut
61              
62             sub search {
63 0     0 1   my ( $self, $collection, $query ) = @_;
64 0           state $check = compile( NonEmptyStr, HashRef );
65 0           ( $collection, $query ) = $check->( $collection, $query );
66              
67 0 0         unless ( exists $query->{q} ) {
68 0           croak("Query parameter 'q' is required for searching");
69             }
70 0 0         unless ( exists $query->{query_by} ) {
71 0           $query->{query_by} = 'search';
72             }
73 0 0         my $tx = $self->_GET(
74             path => [ 'collections', $collection, 'documents', 'search' ],
75             request => $query,
76             return_transaction => 1,
77             ) or return;
78 0           my $response = $tx->res->json;
79 0           foreach my $hit ( @{ $response->{hits} } ) {
  0            
80 0 0         if ( exists $hit->{document}{json} ) {
81 0           $hit->{document}{json} = decode_json( $hit->{document}{json} );
82             }
83             }
84 0           return $response;
85             }
86              
87             sub get {
88 0     0 1   my ( $self, $collection ) = @_;
89 0           state $check = compile(Str);
90 0   0       my @collection = $check->( $collection // '' );
91 0           return $self->_GET( path => [ 'collections', @collection ] );
92             }
93              
94             =head2 C<create>
95              
96             my $collection = $typesense->collections->create(\%definition);
97              
98             Arguments and response as shown at
99             L<https://typesense.org/docs/0.19.0/api/#create-collection>
100              
101             =cut
102              
103             sub create {
104 0     0 1   my ( $self, $collection_definition ) = @_;
105 0           state $check = compile(HashRef);
106 0           ($collection_definition) = $check->($collection_definition);
107 0           my $fields = $collection_definition->{fields};
108              
109 0           foreach my $field (@$fields) {
110 0 0         if ( exists $field->{facet} ) {
111             $field->{facet} =
112 0 0         $field->{facet} ? Mojo::JSON->true : Mojo::JSON->false;
113             }
114             }
115              
116 0           return $self->_POST(
117             path => ['collections'],
118             request => $collection_definition
119             );
120             }
121              
122             =head2 C<delete>
123              
124             my $response = $typesense->collections->delete($collection_name);
125              
126             Response shown at L<https://typesense.org/docs/0.19.0/api/#drop-collection>
127              
128             =cut
129              
130             sub delete {
131 0     0 1   my ( $self, $collection ) = @_;
132 0           state $check = compile(NonEmptyStr);
133 0           ($collection) = $check->($collection);
134 0           return $self->_DELETE( path => [ 'collections', $collection ] );
135             }
136              
137             =head2 C<delete_all>
138              
139             $typesense->collections->delete_all;
140              
141             Deletes everything from Typesense. B<Use with caution>!
142              
143             =cut
144              
145             sub delete_all {
146 0     0 1   my ($self) = @_;
147 0           my $collections = $self->get;
148 0           foreach my $collection (@$collections) {
149 0           my $name = $collection->{name};
150 0           $self->delete($name);
151             }
152             }
153              
154             1;
155