File Coverage

blib/lib/Search/Typesense/Collection.pm
Criterion Covered Total %
statement 38 39 97.4
branch 5 8 62.5
condition 1 2 50.0
subroutine 9 9 100.0
pod 5 5 100.0
total 58 63 92.0


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