FCO / Red

A WiP ORM for Raku
Artistic License 2.0
70 stars 27 forks source link

To many prefetch #563

Closed FCO closed 1 year ago

FCO commented 1 year ago

Still early development. Very hard-coded and SQLite only.

➜  Red git:(to-many-prefetch) ✗ raku -I. -MRed -e '

model Bla { has $.id is serial; has $.value is column; has @.bles is relationship(*.bla-id, :model<Ble>, :prefetch) }
model Ble { has $.id is serial; has $.value is column; has $.bla-id is referencing(*.id, :model(Bla)) }
my $*RED-DB = database "SQLite";
Bla.^create-table; Ble.^create-table; Bla.^create(:value(<test1 test2>.pick)) xx 5; Ble.^create(:value(<t1 t2>.pick), :bla-id((1 .. 10).pick)) xx 20;
my $*RED-DEBUG = True; my $*RED-DEBUG-RESULT = True;

for Bla.^all { .say for .bles }

'
SQL : SELECT
   "bla".id , "bla".value , "bla_bles".json as "bla_bles"
FROM
   "bla"
    LEFT JOIN (
       SELECT
           bla_id,
           json_group_array(json_object('id', "ble".id, 'value', "ble".value, 'bla-id', "ble".bla_id)) as json
       FROM
           "ble"
       GROUP BY
           bla_id
   )
    as bla_bles ON "bla_bles".bla_id = "bla".id
BIND: []
Ble.new(id => 8, value => "t2", bla-id => "1")
Ble.new(id => 11, value => "t2", bla-id => "1")
Ble.new(id => 14, value => "t1", bla-id => "1")
Ble.new(id => 15, value => "t1", bla-id => "1")
Ble.new(id => 20, value => "t1", bla-id => "1")
Ble.new(id => 1, value => "t1", bla-id => "2")
Ble.new(id => 19, value => "t1", bla-id => "2")
Ble.new(id => 3, value => "t1", bla-id => "3")
Ble.new(id => 17, value => "t2", bla-id => "3")
Ble.new(id => 2, value => "t2", bla-id => "4")
Ble.new(id => 6, value => "t2", bla-id => "5")