Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

db: preload embedded fields #393

Open
wants to merge 6 commits into
base: v3
Choose a base branch
from
Open

db: preload embedded fields #393

wants to merge 6 commits into from

Conversation

xiam
Copy link
Member

@xiam xiam commented Jul 20, 2017

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 xiam changed the title db: Drafting support for "preload" feature db: preload feature Jul 20, 2017
@xiam xiam changed the title db: preload feature db: preload embedded fields Jul 20, 2017
@VojtechVitek
Copy link
Member

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

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

@xiam
Copy link
Member Author

xiam commented Aug 24, 2017

@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 #394 (comment), and I have to admit that moving this to bond (which is more opinionated) sounds like a good idea.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants