close

Raku::Journey

a journey through Physics, Air and Diesel

HARC Stack: Modelling

BERJAYA

This is the ninth of the HARC Stack essays. Previous <=

HARC Stack combines HTMX with raku Air, Red and Cro to supply a fresh approach to website authoring.

It may have taken nine instalments to get here, but it was worth the wait. Today we get to use the power of Red.

Component

Here’s the simple Counter component we discussed last week.

BERJAYA
The Component source is here if you would like to copy it.

Model

Here’s the same Counter refactored as a Red model:

BERJAYA
The Model source is here if you would like to copy it.

Blink?

Did you see the changes? Blink and you’ll miss them.

1. use Red

use Red:api<2>; red-defaults “SQLite”;

Use the Red library – here setting up SQLite. Other database options are available.

2. class => model

model Counter does Component::Red { ... }

Change our Counter class to a Red model via the keyword. Append ::Red to the role we mix in with does.

3. apply traits to attributes

has UInt  $.id     is serial;
has Int   $.count  is rw is column(:default{0});

Each Red model uses a regular raku class as a declarative definition of a SQL database table. Each attribute marked with a suitable trait (here serial and column) is taken as a SQL table column specification.

Here we use is rw to mark $.count as read and writeable and providing a suitable default.

Now each instance of Counter we make with eg Counter.new will get a row in the SQL table that stores the current value.

4. save incremented value

$.^save;

When method increment is called and the $.count is incremented, we need to explicitly save it back to the database. This is a Red meta method (thus the caret .^).

5. create table

Counter.^create-table;

After we declare the table this meta method will setup the database schema.

6. use .^create

my $counter = Counter.^create;

This is the way to write .new when you want to make a new Red model instance.

Trivial Upgrade

With these small changes – a trivial upgrade – you can provide any Air::Component with relational database storage in a matter of minutes.

We have seen how to:

  1. Load the Red library
  2. Make a Red model (table definition)
  3. Apply traits to attributes (table columns)
  4. Save changes
  5. Create table
  6. Create model instance

The Red Object Relational Mapper(ORM) module is doing all the hard work here and remains a natural part of the raku code flow.

See More Red

If this has whetted your appetite, take a look at the Red docs and examples to discover more:

Kudos to Fernando Correa de Oliveira for bringing us such a power tool.

Next Time on this Channel

Keep watching this channel if it feels good. Tune in next time for more Air pockets (geddit?). Even better, follow the Getting Started and try it out for yourself.

As usual, comments and feedback welcome. We are a friendly bunch over on the raku IRC chat and Discord.

~librasteve

Published by

2 responses to “HARC Stack: Modelling”

  1. […] is the tenth of the HARC Stack essays. Previous […]

    Like

  2. […] yeah pretty self explanatory. Check out previous editions (e.g. Modelling) and the Air docs for more on how the Red Object Relational Mapper (ORM) […]

    Like

Leave a comment

Design a site like this with WordPress.com
Get started