File Coverage

blib/lib/Catmandu/Fix/add_to_store.pm
Criterion Covered Total %
statement 31 31 100.0
branch 1 2 50.0
condition n/a
subroutine 10 10 100.0
pod n/a
total 42 43 97.6


line stmt bran cond sub pod time code
1              
2             use Catmandu::Sane;
3 1     1   1259  
  1         3  
  1         13  
4             our $VERSION = '1.2019';
5              
6             use Catmandu;
7 1     1   6 use Catmandu::Util::Path qw(as_path);
  1         3  
  1         7  
8 1     1   633 use Moo;
  1         3  
  1         59  
9 1     1   7 use namespace::clean;
  1         2  
  1         10  
10 1     1   425 use Catmandu::Fix::Has;
  1         2  
  1         4  
11 1     1   881  
  1         2  
  1         9  
12             has path => (fix_arg => 1);
13             has store_name => (fix_arg => 1);
14             has bag_name => (fix_opt => 1, init_arg => 'bag');
15             has store_args => (fix_opt => 'collect');
16             has store => (is => 'lazy', init_arg => undef);
17             has bag => (is => 'lazy', init_arg => undef);
18              
19             with 'Catmandu::Fix::Builder';
20              
21             my ($self) = @_;
22             Catmandu->store($self->store_name, %{$self->store_args});
23 1     1   11 }
24 1         5  
  1         12  
25             my ($self) = @_;
26             defined $self->bag_name
27             ? $self->store->bag($self->bag_name)
28 1     1   11 : $self->store->bag;
29 1 50       22 }
30              
31             my ($self) = @_;
32             my $bag = $self->bag;
33             my $getter = as_path($self->path)->getter;
34             sub {
35 1     1   13 my $data = $_[0];
36 1         17 my $vals = $getter->($data);
37 1         18 $bag->add($_) for @$vals;
38             $data;
39 1     1   3 };
40 1         15 }
41 1         29  
42 1         16 1;
43 1         23  
44              
45             =pod
46              
47             =head1 NAME
48              
49             Catmandu::Fix::add_to_store - add matching values to a store as a side effect
50              
51             =head1 SYNOPSIS
52              
53             # Add the current record to an SQLite database.
54             add_to_store(., DBI, data_source: "dbi:SQLite:path/data.sqlite")
55              
56             # Add the journal field to a MongoDB database.
57             add_to_store(journal, MongoDB, database_name: catalog)
58              
59             # Add all author values to a MongoDB database.
60             add_to_store(authors.*, MongoDB, database_name: catalog, bag: authors)
61              
62             # Or, a much faster option: use a named store in a catmandu.yml file
63             #
64             # store:
65             # mydbi:
66             # package: DBI
67             # options:
68             # data_source: "dbi:SQLite:path/data.sqlite"
69             # mymongo:
70             # package: MongoDB
71             # options:
72             # database_name: catalog
73             add_to_store(., mydbi)
74             add_to_store(journal, mymongo)
75             add_to_store(authors.*, mymongo, bag: authors)
76              
77             =head1 DESCRIPTION
78              
79             =head2 add_to_store(PATH,STORE[,store_param: store_val, ...][,bag: bag_name])
80              
81             Store a record or parts of a record in a Catmandu::Store.
82             The values at the PATH will be stored as-is in the database but should be hashes.
83             If the value contains an '_id' field, then it will
84             used as record identifier in the database. If not, then a new '_id' field will
85             be generated and added to the database and original field (for later reference).
86              
87             For instance this YAML input:
88              
89             ---
90             _id: 001
91             title: test
92             name: foo
93             ---
94             _id: 002
95             title: test2
96             name: bar
97              
98             with the fix:
99              
100             add_to_store(., DBI, data_source: "dbi:SQLite:path/data.sqlite")
101              
102             will create a path/data.sqlite SQLite database with two records. Each records contains
103             the _id from the input file and all the record fields.
104              
105             For instance this YAML input:
106              
107             ---
108             title: test
109             name: foo
110             ---
111             title: test2
112             name: bar
113              
114             with the fix:
115              
116             add_to_store(., DBI, data_source: "dbi:SQLite:path/data.sqlite")
117              
118             will create a path/data.sqlite SQLite database with two records. Each records contains
119             the a generated _id and all the record fields. The current input stream will be updated
120             to contain the generated _id.
121              
122             Use L<Catmandu::Fix::lookup_in_store> to lookup records in a Catmandu::Store based
123             on an '_id' key.
124              
125             =head1 DATABASE CONNECTIONS
126              
127             For every call to a C<add_to_store> a new database connection is created. It
128             is much more effient to used named stores in a C<catmandu.yml> file. This file
129             needs to contain all the connection parameters to the database. E.g.
130              
131             store:
132             mystore:
133             package: MongoDB
134             options:
135             database_name: mydata
136              
137             The C<catmandu.yml> file should be available in the same directory as where the
138             C<catmandu> command is executed. Or, this directory can be set with the C<-L> option:
139              
140             $ catmandu -L /tmp/path convert ...
141              
142             =head1 SEE ALSO
143              
144             L<Catmandu::Fix> , L<Catmandu::Fix::lookup_in_store>
145              
146             =cut
147              
148             1;