2023 twenty-four merry days of Perl Feed

Santa's Helpers' Helpers

DBIx::Class::Helpers - 2023-12-11

In past years, Santa really was that jolly old soul everyone talks about, but for the last several months, the old boy has been roaring at his staff in frustration.

"Eight billion souls on earth now! How am I to keep my lists? The old ways just aren't working any more! I can't tell who's been naughty, and who's been nice!"

Midyear, he finally called a meeting of the entire North Pole team, elves and reindeer both. Mrs. Claus baked up a massive batch of cookies, and called the meeting to order. "Now, listen up, all of you! As you know, Santa has been in quite a state this year. The population of Earth hit 8 billion last November, and all those babies born since are automatically on the good list for their first year. But keeping up with the older children and adults has gotten to be quite the burden. He's driving me crazy, stomping around the house with those long lists dragging around behind him. So everyone, put your thinking caps on. We need innovation, now!"

The North Pole's CEO stood up, shuffled some papers on the lectern in front of him, and said, "folks, I owe you all an apology. I've been a grumpy old jerk all year, with my job overwhelming me at last. Mrs. Claus and I have spent some time talking, and she's convinced me to ask you all for your help. We need a better way to keep up with the lists of naughty and nice children and adults on earth. Don't worry about what it'll cost, I'll take care of that. But it needs to be fast, flexible, and easy for us to keep up with. If there are some bits of it that we can automate, so much the better. Does anyone have an idea we can use?"

About halfway to the back sat an elf named Otto, wearing thick glasses, and a pocket protector. He was a builder of the geeky electronic toys that had become so popular in the last thirty or forty years. He raised his hand, and when Santa called on him, he said, "Sir, if we can get a big enough computer, we could put all of them in a database. Some of us could write a program to help you manage it on your web browser."

"Elf Otto, that's a fine idea! Come up here and let's work on that. Anyone else knowledgeable in these things, please come down front to help. The rest of you, get cracking on those toy orders!"

The new North Pole Application Group, as they dubbed themselves, quickly decided to build Santa's new database manager with Perl, using Dancer2, DBIx::Class, and PostgreSQL, finding them quick and easy to use, and scalable to the size that the boss-man needed. A couple of clever elves worked with Santa to find a place to host their new application, another small group started working on the UI, a couple of grumpy older elves took up the job of testing, and Otto found himself leading a team to design the database schema, and give the UI team the tools they needed to access it.

"DBIx::Class is massive! By itself, it can do everything we need!" one member of the team exclaimed, after reading just some of the documentation.

"Yes, sure, it can," Otto told him, "but some of it is pretty darn arcane. We need helpers."

The room erupted. "But *we* are Santa's helpers!" one shouted.

"Fine, fine, yes, we are," Otto said, after calming the team. "But sometimes helpers need helpers. This is a big, big tool we're building, and we want the UI and testing teams to be able to use our part of it very easily, with code they can read and understand and maintain in the future. The database is the core of the application, so it's gotta be easy to work with. Call these Santa's-Helpers-Helpers if you want. But DBIx::Class has a lot of helpers available on CPAN. Let's look for a few that can make things easier to work with for the other teams."

And so they did. Here are a few that they found:

Test::DBIx::Class

The grumpy testing elves found this module very useful; it let them set up and run unit and integration tests in a known-clean environment. They designed the test fixtures and wrote tests to make sure every function behaved as it was supposed to.

Dancer2::Plugin::DBIx::Class and DBIx::Class::ResultSetNames

These two modules gave the elves easy access to all the tables of the database. Instead of typing long references to the name of the table to start a search, they just start it with the name of the table. Searches are quick and semantically friendly:

$schema->resultset('Person')->search({ ... });   # no need for this...
db_person->search({...}) # when you can do this.
DBIx::Class::TemporalRelations

Elf Wayne used this module to write a daily cron job for the application that would update the children older than one year, so Santa could start watching their naughty/nice ratio. The heart of it lay in a single line of code:

db_person->born_before($dt_1_year_ago)->update({ auto_nice => 0 });
DBIx::Class::Helpers and DBIx::Class::MoreHelpers

The elves found all sorts of useful search shortcuts in these two modules, like rows() to limit the number of rows in a search for paging, and group_by() and order_by() which were handy for Santa's reports. is(), is_not(), and is_any() came in very handy for boolean fields, too.

DBIx::Class::Numeric

Elf Cynthia squealed with joy when she found this module on CPAN. She was working on the part of the application that would tally up the naughty and nice things that people do during the year. She discovered that once she declared a database field as numeric, she got baked-in methods to increment and decrement counters, increase or decrease counts, and even set boundary limits on them.

Not too much later, Santa's helpers launched V1.0 of eSanta, the application for the North Pole to keep track of Santa's naughty and nice lists. Santa smiled happily as he watched data being entered from his lists automatically. Mrs. Claus was thrilled, too, as Santa would be less grumpy and have more time to spend with her.

In the small workshop the eSanta team had set up with nice laptops, a ping-pong table, and whiteboards covering the walls, work on V2.0 is already well underway. That version will include new features to help Santa in his fall field-work sessions, when he goes to malls and stores and parades and talks to the children about what they want for Christmas. It includes a module for the North Pole Post Office to enter data from Santa's letters, too, though the now-jolly fellow insists that he must still read each and every one. As the data is entered, it'll be piped down to Santa's workshops so that the elves could build the toys to fulfil those orders, and have the bundles of toys ready for Santa's whirlwind delivery on December 25th!

Gravatar Image This article contributed by: D Ruth Holloway <ruth@hiruthie.me>