Last active
November 20, 2016 01:09
-
-
Save spxrogers/ec843e81820c240c8b06c6cb02be8557 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
r "gopkg.in/dancannon/gorethink.v2" | |
"log" | |
) | |
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"` | |
} | |
// init DB | |
func init() { | |
log.SetFlags(0) | |
var connectError error | |
rethink, connectError = r.Connect(r.ConnectOpts{Database: "test"}) | |
if connectError != nil { | |
log.Fatalf("[Error[ failed connecting to database cluster: %s", connectError.Error()) | |
} else { | |
log.Println("[Info] connected to database cluster.") | |
} | |
r.TableDrop(tableAuthor).RunWrite(rethink) | |
r.TableCreate(tableAuthor).RunWrite(rethink) | |
r.TableDrop(tableBook).RunWrite(rethink) | |
r.TableCreate(tableBook).RunWrite(rethink) | |
} | |
var rethink *r.Session | |
const ( | |
tableAuthor = "author" | |
tableBook = "book" | |
bookID = "hp_1" | |
authorID = "jk_1" | |
) | |
func main() { | |
authorIDMergeFunc := func(p r.Term) interface{} { | |
return map[string]interface{}{ | |
"author_id": r.Table("author").Get(p.Field("author_id")), | |
} | |
} | |
jk := Author{ID: authorID, Name: "J.K. Rowling"} | |
hp := Book{ | |
ID: bookID, | |
Title: "Harry Potter", | |
Author: Author{ID: authorID}, | |
} | |
// 0: insert some control data | |
save(tableAuthor, jk, false) | |
save(tableBook, hp, false) | |
var book Book | |
// 1: try to get without merge | |
get(tableBook, bookID, &book) | |
// 2: get with merge | |
getMerge(tableBook, bookID, authorIDMergeFunc, &book) | |
hp.Title = "Harry Potter V2" | |
hp.Author.Name = "J.K. Rowling V2" | |
// 3: save updates | |
save(tableBook, hp, true) | |
// 4: get updated row | |
getMerge(tableBook, bookID, authorIDMergeFunc, &book) | |
} | |
func get(table, id string, itemRef interface{}) { | |
cursor, err := r.Table(table).Get(id).Run(rethink) | |
if err != nil { | |
log.Printf("[Get] failed getting %s from %s: %s\n", id, table, err.Error()) | |
return | |
} | |
err = cursor.One(itemRef) | |
if err != nil { | |
log.Printf("[Get] failed parsing result into struct: %s\n", err.Error()) | |
} else { | |
log.Printf("[Get] got item:\n\t%+v\n", itemRef) | |
} | |
} | |
func getMerge(table, id string, mergeFunc func(p r.Term) interface{}, itemRef interface{}) { | |
cursor, err := r.Table(table).Get(id).Merge(mergeFunc).Run(rethink) | |
if err != nil { | |
log.Printf("[GetMerge] failed getting %s from %s: %s\n", id, table, err.Error()) | |
return | |
} | |
err = cursor.One(itemRef) | |
if err != nil { | |
log.Printf("[GetMerge] failed parsing result into struct: %s\n", err.Error()) | |
} else { | |
log.Printf("[GetMerge] got item:\n\t%+v\n", itemRef) | |
} | |
} | |
func save(table string, item interface{}, conflict bool) { | |
conflictResolution := r.InsertOpts{} | |
if conflict { | |
conflictResolution = r.InsertOpts{Conflict: "update"} | |
} | |
resp, err := r.Table(table).Insert(item, conflictResolution).RunWrite(rethink) | |
if err != nil { | |
log.Printf("[Save] failed inserting into %s: %s\n", table, err.Error()) | |
} else if resp.Errors > 0 { | |
log.Printf("[Save] insert failed with %d errors. First error: %s\n", resp.Errors, resp.FirstError) | |
} else { | |
log.Printf("[Save] inserted row\n\t%+v\n", item) | |
} | |
} | |
/* | |
*** OUTPUT *** | |
************** | |
[Info] connected to database cluster. | |
[Save] inserted row | |
{ID:jk_1 Name:J.K. Rowling} | |
[Save] inserted row | |
{ID:hp_1 Title:Harry Potter Author:{ID:jk_1 Name:}} | |
[Get] failed parsing result into struct: gorethink: could not decode type string into Go value of type main.Author | |
[GetMerge] got item: | |
&{ID:hp_1 Title:Harry Potter Author:{ID:jk_1 Name:J.K. Rowling}} | |
[Save] inserted row | |
{ID:hp_1 Title:Harry Potter V2 Author:{ID:jk_1 Name:J.K. Rowling V2}} | |
[GetMerge] got item: | |
&{ID:hp_1 Title:Harry Potter V2 Author:{ID:jk_1 Name:J.K. Rowling}} | |
**/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment