fluent-kit icon indicating copy to clipboard operation
fluent-kit copied to clipboard

Join based eager loading

Open tanner0101 opened this issue 5 years ago • 3 comments

Eager loading currently works using a second query with IN. Supporting join-based eager loading would allow for eager loads to use a single query. This could also help to work around bind limits as noted in https://github.com/vapor/fluent-kit/issues/277#issuecomment-643901566.

This is also related to https://github.com/vapor/fluent-kit/issues/227 as join-based eager loading could support filtering on eager loaded values by default.

tanner0101 avatar Jul 14 '20 01:07 tanner0101

This might fix the issue where if you join and eager load (for instance when you need to filter on a property on the eager loaded value) it duplicates the results and doesn't handle that correctly. So if I have had one galaxy with 5 stars and do a query and eager load the stars but also filter on the stars with a join I'll end up with 5 copies of the galaxy all eager loaded instead of one galaxy and 5 stars

0xTim avatar Jul 30 '20 11:07 0xTim

Is this basically supported now by the joined() method?

i.e.,

let results = try await Planet.query(on: database)
    .field( /* any amount of Star or Planet fields */)
    .join(Star.self, on: \Planet.$star.$id == \Star.$id)
    .filter(Star.self, \.$name == "Sun")
    .all()
let star = try results[0].joined(Star.self)

alexcostaluiz avatar May 20 '23 02:05 alexcostaluiz

Not quite - it's definitely possible to do and get the correct behaviour but the joined() functionality won't load the models into the relationship property wrappers like eager loading will. Eager loading is still multiple queries (though just one extra query)

0xTim avatar May 30 '23 08:05 0xTim