Tangra...omMany(3pm)

πŸ“¦
version 2.12 Β· 3 versions
, in ubuntu noble, ubuntu resolute, ubuntu stonking

πŸ’– Tangra::Type::Array::FromMany πŸ’–

okay bestie, welcome to the Tangra::Type::Array::FromMany page β€” it lives in the User Contributed Perl Documentation section of the Tangra...omMany(3pm) manual, and it's literally the blueprint for mapping Perl arrays using a link table. yes, arrays. yes, in a database. sit down, we're doing this together 🫢


✨ NAME ✨

Tangram::Type::Array::FromMany β€” map Perl arrays using a link table. that's the whole vibe. iconic, honestly. πŸ’…


🌸 SYNOPSIS 🌸

here's how you actually summon it, no cap:

use Tangram;

# or
use Tangram::Core;
use Tangram::Type::Array::FromMany;

$schema = Tangram::Schema->new(

   classes => { Company => { fields => {

   array =>
   {
      # long form
      employee =>
      {
         class => 'Person',
         table => 'Company_employees',
         coll => 'company',
         item => 'employee',
         slot => 'slot'
      },

      # short form
      assets => 'Asset',
   }

see how there's a long form and a short form? one's the full main-character entrance, the other's the low-effort "just text me" version. both valid, both slay. ✨


πŸ¦‹ DESCRIPTION πŸ¦‹

so what this does: it maps references to a Perl array. πŸ“š the persistent fields get grouped in a hash under the "array" key in the field hash. that's the spot. that's where they live.

the array may contain only objects of persistent classes, and those classes must have a common persistent base class. it's giving "we're all family here" β€” fr fr, no renegade plain scalars allowed 🚫

Tangram uses a link table to save the state of the collection. the table has three columns, and those three columns contain:

  • the id of the container objects
  • the id of the elements
  • the position of the element in the array

three columns, one job, ate that πŸ“

🌈 the field descriptor situation 🌈

the field names are passed in a hash that associates a field name with a field descriptor. the field descriptor may be either a hash or a string β€” your choice bestie, pick your fighter πŸ’œ

the hash uses the following fields:

  • class
  • aggreg
  • table
  • coll
  • item
  • slot
  • deep_update

and now the breakdown of each one, because we do not gatekeep:

  • class β€” mandatory πŸ’€ do not skip this one. it specifies the class of the elements.
  • aggreg β€” optional. this specifies that the elements of the collection must be removed (erased) from persistent storage along with the containing object. the default is not to aggregate.
  • table β€” optional. sets the name of the link table. this defaults to 'C_F', where C is the class of the containing object and F is the field name. so yes, it name-generates for you, the algorithm is just doing its little thing πŸ’…
  • coll β€” optional. sets the name the column containing the ids of the containing objects. this defaults to 'coll'.
  • item β€” optional. sets the name the column containing the ids of the elements. this defaults to 'item'.
  • slot β€” optional. sets the name the column containing the position of the elements. this defaults to 'slot'.
  • deep_update β€” optional. specifies that all elements have to be updated automatically when "update" is called on the collection object. automatic update ensures consistency between the Perl representation and the DBMS state, but it degrades update performance, so use it with caution 🫣 the default is not to do automatic updates.
⚠️ okay real talk for a second β€” that deep_update one is a whole tradeoff. like, yes, consistency is cute, but bestie the performance hit is real. use it with caution, we've been warned.

πŸŽ€ the string shortcut πŸŽ€

if the descriptor is a string, it is interpreted as the name of the element's class. this is equivalent to specifying only the "class" field in the hash variant. so assets => 'Asset' is doing the absolute most with the absolute least, and honestly? obsessed ✨


and that's the whole page, bestie. you're now certified in Tangram::Type::Array::FromMany. go forth and map your arrays. 🧚

sign-off tea β˜•: this page ships with perl v5.36.0 and it's dated 2022-10-16. mwah, go be great πŸ‘‘πŸ’–

πŸ“¦ where this page comes from