Code Monkey home page Code Monkey logo

Comments (12)

dancannon avatar dancannon commented on May 30, 2024

This is definitely something I am planning on. I will look into getting a simple example application working soon.

from rethinkdb-go.

ekanna avatar ekanna commented on May 30, 2024

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.

dancannon avatar dancannon commented on May 30, 2024

Thanks for this, Ill add this to the repo wiki

from rethinkdb-go.

dancannon avatar dancannon commented on May 30, 2024

I am aiming to create a more full featured example application so I will leave this issue open for now.

from rethinkdb-go.

ekanna avatar ekanna commented on May 30, 2024

Great! Waiting to see your full featured example application!

from rethinkdb-go.

dancannon avatar dancannon commented on May 30, 2024

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.

elithrar avatar elithrar commented on May 30, 2024

@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.

dancannon avatar dancannon commented on May 30, 2024

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.

robert-zaremba avatar robert-zaremba commented on May 30, 2024

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.

dancannon avatar dancannon commented on May 30, 2024

Could you explain ScanFirst, Exists and ExistsMulti first? However your ScanAll method would definitely be great.

from rethinkdb-go.

robert-zaremba avatar robert-zaremba commented on May 30, 2024

I've created a separate issue for those helper functions, where I've attached the code:
#29

from rethinkdb-go.

dancannon avatar dancannon commented on May 30, 2024

Ok I will close this issue then.

from rethinkdb-go.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.