fluent-kit
fluent-kit copied to clipboard
Join based eager loading
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.
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
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)
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)