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

Enhancing References #386

Open
spxrogers opened this issue Nov 20, 2016 · 2 comments
Open

Enhancing References #386

spxrogers opened this issue Nov 20, 2016 · 2 comments

Comments

@spxrogers
Copy link

[see gist]

Would love to see the references feature beefed up a little. A couple wish list items:

  • A SaveReference option for InsertOpts and UpdateOpts
    • this would tell the driver to save any nested reference items as well. in the gist example this call (it's corresponding output) would reflect the changes to it's referenced Author.Name (from here) field as well.
      • Value: If I'm working with a complex, nested structure with updates to several different nested fields, I can make one gorethink call to save all changes.
  • Introduce an equivalent GetOpts that includes a GetReference option to automatically find reference fields and intelligently does the Merge operation when it runs the query.
    • Value: user no longer has to worry about constructing merge functions or ensuring that they stay up to date

I'm not entirely sure on the feasibility of these features, but i postulate (without any deep knowledge on how the driver constructs and performs the rethinkdb queries) an approach could be to introduce a new struct tag, gorethink_ref_table which would be the string value of the table name currently included in the merge function's parameter, and use that tag value to construct the relevant sub-queries to execute.

Then the structs could be updated to:

type Author struct {
	ID   string `gorethink:"id,omitempty"`
	Name string `gorethink:"name"`
}

type Book struct {
	ID     string `gorethink:"id,omitempty"`
	Title  string `gorethink:"title"`
	Author Author `gorethink:"author_id,reference" gorethink_ref:"id" gorethink_ref_table:"author"`
}

And API calls to get a book / save book & author changes from the referenced book could be simplified to:

/*** get ***/
r.Table("book").Get("aBookRowID", r.GetOpts{GetReference: true}).Run(rethink)

/*** save ***/
r.Table("book").Insert(aBookItemWithChanges).RunWrite(rethink)
r.Table("author").Insert(aBookItemWithChanges.Author).RunWrite(rethink)
// could be simplified to 
r.Table("book").Insert(aBookItemWithChanges, r.InsertOpts{SaveReference: true}).RunWrite(rethink)
@dancannon
Copy link
Collaborator

Thanks for the suggestion @spxrogers, unfortunately I do not have the time to work on this feature right now but if you (or anybody else) could submit a PR I would definitely consider merging it into the driver.

@spxrogers
Copy link
Author

Thanks for the feedback @dancannon
Unfortunately I'm also at a point with not a lot of time to work on this feature, but maybe one day :)

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

No branches or pull requests

2 participants