Comments (12)
This is definitely something I am planning on. I will look into getting a simple example application working soon.
from rethinkdb-go.
My complete example for the benefit of others like me!
package main
import (
r "github.com/dancannon/gorethink"
"fmt"
)
var session *r.Session
func init(){
var err error
session, err = r.Connect(map[string]interface{}{
"address": "localhost:28015",
"database": "test",
})
if err != nil {
fmt.Println(err)
return
}
}
// uppercase for struct fields is necessary for scanning later
type Person struct {
Id int
Name string
Place string
}
func main(){
// fetch all records from "persons" table
rows,_ := r.Table("persons").Run(session)
var persons []Person
for rows.Next(){
var p Person
err := rows.Scan(&p)
if err != nil {
fmt.Println(err)
return
}
persons = append(persons, p)
}
fmt.Println(persons)
}
from rethinkdb-go.
Thanks for this, Ill add this to the repo wiki
from rethinkdb-go.
I am aiming to create a more full featured example application so I will leave this issue open for now.
from rethinkdb-go.
Great! Waiting to see your full featured example application!
from rethinkdb-go.
Ok so I have created a simple Todo app based on http://todomvc.com. If you guys could have a look at the code and let me know if see any problems that would great.
https://github.com/dancannon/GoRethink_TodoDemo
from rethinkdb-go.
@dancannon Fantastic, thanks Dan. I'll take a look over it as I port my code (slowly!) from rethinkgo to gorethink—it's a good example of best practice for this driver.
One question that I have from it though: is there a way to marshal a result (with multiple rows) directly into a []ToDoItem{}
rather than iterating row-by-row? I can see see we have an All()
function however it doesn't seem to work like rethinkgo (I've slightly modified the code for brevity):
items := []TodoItem{}
resp, err := r.Table("items").Filter(r.Row.Field("Status").Eq("complete")).OrderBy(r.Asc("Created").Run(session).All(&items)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
...
I end up with a multiple-value ... in single-value context
error on compile. I liked this pattern as it's a useful shortcut when you want to pass a slice of structs to a handler for template rendering.
from rethinkdb-go.
I think having a method to do something like this is needed. However I am not happy with how the All
method works I think it makes more sense to have a ScanAll method which does what you mentioned.
I will get this sorted later today.
from rethinkdb-go.
In my project I have implemented Scan
, ScanAll
, ScanFirst
, Exists
, ExistsMulti
helper functions. Example usage:
var products []map[string]string
err = rdbu.ScanAll(tabProduct, &products, dbsession)
var user User
notNil, err = rdbu.Scan(tabUser.GetAllByIndex("email", "[email protected]"), &user, dbsession)
Scan*
automatically reflects the destination type. Furthermore ScanAll
asserts that destination is a pointer to slice.
I can pull request them with a test suite.
from rethinkdb-go.
Could you explain ScanFirst
, Exists
and ExistsMulti
first? However your ScanAll
method would definitely be great.
from rethinkdb-go.
I've created a separate issue for those helper functions, where I've attached the code:
#29
from rethinkdb-go.
Ok I will close this issue then.
from rethinkdb-go.
Related Issues (20)
- How to use join HOT 1
- IsEmpty result HOT 1
- v6 is opening ConnectOpts.MaxOpen connections when connecting
- Session.IsConnected not detecting disconnection HOT 8
- v6 read timeout closes connection
- Add support for Change Feed Offsets
- Connection pool performance HOT 5
- `v5` HOT 1
- Panic on session.Close()
- go mod path HOT 3
- Bug: a single string array field is returned as multiple strings. HOT 1
- data race in pool.go HOT 3
- Field("field").Field("field") nil
- Make possibility for Term to be marshalled and unmarshalled (for instance with json or yaml), so Term could be saved/loaded to/from some storage.
- Unable to connect when `TimeOut` is specified HOT 1
- Connection pool is not safe HOT 3
- Contexts not working properly in certain scenarios
- WriteResponse does not return GeneratedKeys
- Utilize MarshalJSON and UnmarshalJSON interface implementations HOT 2
- Go Lang replaces get to install. HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from rethinkdb-go.