I'm looking for clear example of r.Do() and r.Branch() functions in gorethink.
1 Answers
The author of Go RethinkDB driver is very active on Github and you may get quicker answer if asking over there.
Here is an example of Do
.
res, _ := r.DB("test").Table("table").Get("ID").Do(func(issue r.Term) r.Term {
return issue.Merge(map[string]string{
"new_field": "new_value",
})
}).Run(session)
var b []interface{}
a.All(&b)
fmt.Println(b)
I think the most important thing is to ensure we declare correct type Term
. I import RethinkDB driver as r
so I used r.Term
. Then inside Do
function, you can use any command from Term: https://godoc.org/github.com/dancannon/gorethink#Term
An example of Do
and Branch
at same time:
r.DB("test").Table("issues").Insert(map[string]interface{}{
"id": 1111,
}).Do(func(result r.Term) r.Term {
return r.Branch(result.Field("inserted").Gt(0),
r.DB("test").Table("log").Insert(map[string]interface{}{
"time": r.Now(),
"result": "ok",
}),
r.DB("test").Table("log").Insert(map[string]interface{}{
"time": r.Now(),
"result": "failed",
}))
}).Run(session)
Basically, inside Do
, we have access to return value of previous funciton call of RethinkDB, and we can continue to call ReQL command on that return value as in a Map
function.
Inside Branch
, we pass 3 arguments, all are Term
type in Go RethinkDB driver. You can also refer to the official example: https://godoc.org/github.com/dancannon/gorethink#Branch
It's bit hard to get familiar with Go driver because of the nature of language and cannot fit in same dynamic style of official driver(Ruby, Python, JS). It's even more confusing in Language that we cannot chain command.