Catman...o_store(3pm)
π Catman...o_store(3pm) π
okay bestie, gather round, because today we're talking about Catmandu::Fix::add_to_store from the User Contributed Perl Documentation and it's genuinely one of those little fixes that does SO much. it's giving side-effect queen π
β¨ NAME β¨
Catmandu::Fix::add_to_store β adds matching values to a store as a side effect π
πΈ SYNOPSIS πΈ
# Add the current record to an SQLite database.
add_to_store(., DBI, data_source: "dbi:SQLite:path/data.sqlite")
# Add the journal field to a MongoDB database.
add_to_store(journal, MongoDB, database_name: catalog)
# Add all author values to a MongoDB database.
add_to_store(authors.*, MongoDB, database_name: catalog, bag: authors)
# Or, a much faster option: use a named store in a catmandu.yml file
#
# store:
# mydbi:
# package: DBI
# options:
# data_source: "dbi:SQLite:path/data.sqlite"
# mymongo:
# package: MongoDB
# options:
# database_name: catalog
add_to_store(., mydbi)
add_to_store(journal, mymongo)
add_to_store(authors.*, mymongo, bag: authors)
like look at that. four ways to do the same vibe, and one of them is way faster. iykyk π¦
π DESCRIPTION π
π« add_to_store(PATH,STORE[,store_param: store_val, ...][,bag: bag_name])
so what this does: it stores a record β or just parts of a record β into a Catmandu::Store. the values sitting at the PATH get stored as-is in the database, but heads up, they should be hashes. no cap.
if your value has an _id field in it, that's what gets used as the record identifier in the database. periodt. but if it doesn't have one? then a brand new _id gets generated and added to the database and to the original field (for later reference, so you're not just out here losing track of your records π).
okay let's do the examples because this is where it clicks, fr fr.
say you've got this YAML input:
---
_id: 001
title: test
name: foo
---
_id: 002
title: test2
name: bar
and you run this fix:
add_to_store(., DBI, data_source: "dbi:SQLite:path/data.sqlite")
what you get is a path/data.sqlite SQLite database with two records in it. each record contains the _id from the input file plus all the record fields. slay β¨
now say your YAML input has no _id, like:
---
title: test
name: foo
---
title: test2
name: bar
with the same fix:
add_to_store(., DBI, data_source: "dbi:SQLite:path/data.sqlite")
you still get a path/data.sqlite SQLite database with two records. but this time each record contains a generated _id plus all the record fields. and the current input stream gets updated so it contains that generated _id too. cute, right? π₯°
π‘ pro tip from me to you: useCatmandu::Fix::lookup_in_storeif you want to look records up in aCatmandu::Storebased on an_idkey. that's literally what it's for.
π₯ DATABASE CONNECTIONS π₯
okay so this part is important and i need you to actually read it:
every single time you call add_to_store, a new database connection gets created. every. time. π
it is much more efficient to use named stores in a catmandu.yml file instead. that file needs to contain all the connection parameters for the database. like:
store:
mystore:
package: MongoDB
options:
database_name: mydata
your catmandu.yml file should be sitting in the same directory as wherever the catmandu command is being executed. OR β and this is the glow-up β you can set that directory yourself with the -L option:
$ catmandu -L /tmp/path convert ...
that's it. that's the whole trick. named stores = fewer connections = faster = iconic π
π SEE ALSO π
Catmandu::FixCatmandu::Fix::lookup_in_store
and that's the whole page, bestie! π«Ά this one shipped with perl v5.40.1, dated 2026-01-24 β so go forth and store responsibly. mwah πππΈ
π¦ where this page comes from
- ubuntu resolute: libcatmandu-perl 1.2025-1 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu stonking: libcatmandu-perl 1.2025-1 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms