db: preload embedded fields
The idea behind preloading is being able to map results from a join into an embedded type with ease:
artistCollection := sess.Collection("artist")
publicationCollection := sess.Collection("publication")
type artistAssoc struct {
ID uint `db:"id"`
...
}
type publicationAssoc struct {
ID uint `db:"id"`
...
Author *artistAssoc `db:"author,omitempty,assoc"`
}
var publications []publicationAssoc
// Joining two db.Result objects by using Assoc.
q := publicationCollection.Find().Assoc(
artistCollection.Find("publication.author_id = artist.id"),
"author",
)
My proposal is adding an Assoc method which takes another db.Result from the same session and creates a JOIN behind the scenes. The second argument for Assoc is the name of the embedded field, in this case publicationAssoc has a Author *artistAssoc db:"author,omitempty,assoc"' field, that's why we use author as second argument for Assoc().
I'm not fully convinced this should be part of upper/db.
Same thing can be achieved with .Join().On() - am I right?
@VojtechVitek Yes, except there is no join for db.Result so it won't work with Find(). Peter has expressed similar feelings towards another magical feature https://github.com/upper/db/issues/394#issuecomment-321987750, and I have to admit that moving this to bond (which is more opinionated) sounds like a good idea.