Catman...o_store(3pm)

πŸ“¦
version 1.2025 Β· 4 versions
, in ubuntu resolute, ubuntu stonking

πŸ’– 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: use Catmandu::Fix::lookup_in_store if you want to look records up in a Catmandu::Store based on an _id key. 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::Fix
  • Catmandu::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