File Coverage

blib/lib/Catmandu/Store.pm
Criterion Covered Total %
statement 39 39 100.0
branch 2 2 100.0
condition 16 23 69.5
subroutine 12 12 100.0
pod 3 4 75.0
total 72 80 90.0


line stmt bran cond sub pod time code
1             package Catmandu::Store;
2              
3 19     19   126955 use Catmandu::Sane;
  19         46  
  19         135  
4              
5             our $VERSION = '1.2020';
6              
7 19     19   727 use Hash::Util::FieldHash qw(fieldhash);
  19         1057  
  19         1137  
8 19     19   196 use Catmandu::Util qw(require_package);
  19         49  
  19         820  
9 19     19   120 use Moo::Role;
  19         54  
  19         134  
10 19     19   7998 use MooX::Aliases;
  19         3275  
  19         139  
11 19     19   8611 use namespace::clean;
  19         68  
  19         138  
12              
13             with 'Catmandu::Logger';
14              
15             has bag_class => (is => 'ro', default => sub {ref($_[0]) . '::Bag'},);
16              
17             has default_bag => (is => 'lazy');
18             has default_plugins => (is => 'ro', default => sub {[]},);
19             has default_options => (is => 'ro', default => sub {+{}},);
20             has bag_options => (is => 'ro', init_arg => 'bags', default => sub {+{}},);
21             has key_prefix => (is => 'lazy', default => sub {'_'},);
22             has id_key => (is => 'lazy', alias => 'id_field');
23              
24             sub key_for {
25 30     30 1 559 $_[0]->key_prefix . $_[1];
26             }
27              
28             sub _build_id_key {
29 26     26   312 $_[0]->key_for('id');
30             }
31              
32             sub _build_default_bag {
33 30     30   1134 'data';
34             }
35              
36             sub new_bag {
37 43     43 0 120 my ($self, $name, $opts) = @_;
38 43   50     219 $opts ||= {};
39 43         97 $opts->{store} = $self;
40 43   33     144 $opts->{name} = $name // $self->default_bag;
41 43         158 my $default_opts = $self->default_options;
42 43   100     253 my $bag_opts = $self->bag_options->{$opts->{name}} //= {};
43 43         241 $opts = {%$default_opts, %$bag_opts, %$opts};
44              
45 43   33     342 my $pkg = require_package(delete($opts->{class}) // $self->bag_class);
46 43         124 my $default_plugins = $self->default_plugins;
47 43   100     194 my $plugins = delete($opts->{plugins}) // [];
48 43 100 100     225 if (@$default_plugins || @$plugins) {
49 14         75 $pkg = $pkg->with_plugins(@$default_plugins, @$plugins);
50             }
51 43         14667 $pkg->new($opts);
52             }
53              
54             {
55             fieldhash my %bag_instances;
56              
57             sub bags {
58 149     149 1 296 my ($self) = @_;
59 149   100     2454 $bag_instances{$self} ||= {};
60             }
61              
62             sub bag {
63 149     149 1 2012695 my ($self, $name) = @_;
64 149   66     2587 $name ||= $self->default_bag;
65 149   66     1036 $self->bags->{$name} ||= $self->new_bag($name);
66             }
67             }
68              
69             1;
70              
71             __END__
72              
73             =pod
74              
75             =head1 NAME
76              
77             Catmandu::Store - Namespace for packages that can make data persistent
78              
79             =head1 SYNOPSIS
80              
81             # From the command line
82              
83             $ catmandu import JSON into MongoDB --database_name 'bibliography' < data.json
84              
85             $ catmandu export MongoDB --database_name 'bibliography' to YAML
86             $ catmandu export MongoDB --database_name 'bibliography' --query '{"PublicationYear": "1937"}'
87             $ catmandu count MongoDB --database_name 'bibliography' --query '{"PublicationYear": "1937"}'
88              
89             # From Perl
90             use Catmandu;
91              
92             my $store = Catmandu->store('MongoDB',database_name => 'bibliography');
93              
94             my $obj1 = $store->bag->add({ name => 'Patrick' });
95              
96             printf "obj1 stored as %s\n" , $obj1->{_id};
97              
98             # Force an id in the store
99             my $obj2 = $store->bag->add({ _id => 'test123' , name => 'Nicolas' });
100              
101             my $obj3 = $store->bag->get('test123');
102              
103             $store->bag->delete('test123');
104              
105             $store->bag->delete_all;
106              
107             # Some stores can be searched
108             my $hits = $store->bag->search(query => 'name:Patrick');
109              
110             =head1 DESCRIPTION
111              
112             A Catmandu::Store is a stub for Perl packages that can store data into
113             databases or search engines. The database as a whole is called a 'store'.
114             Databases also have compartments (e.g. tables) called L<Catmandu::Bag>-s.
115             Some stores can be searched using L<Catmandu::Searchable> methods.
116              
117             =head1 CONFIGURATION
118              
119             =over
120              
121             =item default_plugins
122              
123             Specify plugins that will be applied to every bag in the store.
124              
125             my $store = Catmandu::Store::MyDB->new(default_plugins => ['Datestamps']);
126              
127             =item default_bag
128              
129             The name of the bag to use if no explicit bag is given. Default is 'data'.
130              
131             my $store = Catmandu::Store::MyDB->new(default_bag => 'stuff');
132             # this will return the stuff bag
133             my $bag = $store->bag;
134              
135             =item bags
136              
137             Specify configuration for individual bags.
138              
139             my $store = Catmandu::Store::Hash->new(
140             bags => {stuff => {plugins => ['Datestamps']}});
141             # this bag will use the L<Catmandu::Plugin::Datestamps> role
142             $store->bag('stuff')
143             # this bag won't
144             $store->bag('otherbag')
145              
146             =item bag_class
147              
148             An optional custom class to use for bags. Default is C<Bag> in the store's
149             namespace. This class should consume the L<Catmandu::Bag> role.
150              
151             # this will use the Catmandu::Store::MyDB::Bag class for bags
152             Catmandu::Store::MyDB->new()
153             # this will use MyBag
154             Catmandu::Store::MyDB->new(bag_class => 'MyBag')
155              
156             =item key_prefix
157              
158             Use a custom prefix to mark the reserved or special keys that the store uses.
159             By default an underscore gets prependend. The only special key in a normal
160             store is '_id'. L<Catmandu::Plugin::Versioning> will also use '_version'. Other
161             plugins or stores may add their own special keys.
162              
163             # this store will use the my_id key to hold id's
164             Catmandu::Store::MyDB->new(key_prefix => 'my_')
165              
166             =item id_key
167              
168             Define a custom key to hold id's for all bags of this store. See C<key_prefix>
169             for the default value. Also aliased as C<id_field>. Note that this can also be
170             overriden on a per bag basis.
171              
172             =back
173              
174             =head1 METHODS
175              
176             =head2 bag($name)
177              
178             Create or retieve a bag with name C<$name>. Returns a L<Catmandu::Bag>.
179              
180             =head2 key_for($key)
181              
182             Helper method that applies C<key_prefix> to the C<$key> given.
183              
184             =head2 log
185              
186             Return the current logger. Can be used when creating your own Stores.
187              
188             E.g.
189              
190             package Catmandu::Store::Hash;
191              
192             ...
193              
194             sub generator {
195             my ($self) = @_;
196              
197             $self->log->debug("generating record");
198             ...
199             }
200              
201             See also: L<Catmandu> for activating the logger in your main code.
202              
203             =head1 SEE ALSO
204              
205             L<Catmandu::Bag>, L<Catmandu::Searchable>
206              
207             =cut