db icon indicating copy to clipboard operation
db copied to clipboard

db: preload embedded fields

Open xiam opened this issue 8 years ago • 2 comments

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().

xiam avatar Jul 20 '17 15:07 xiam

I'm not fully convinced this should be part of upper/db.

Same thing can be achieved with .Join().On() - am I right?

VojtechVitek avatar Aug 24 '17 15:08 VojtechVitek

@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.

xiam avatar Aug 24 '17 15:08 xiam