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

Feature request: ORM support #20

Open
SpencerC opened this issue Mar 26, 2022 · 0 comments
Open

Feature request: ORM support #20

SpencerC opened this issue Mar 26, 2022 · 0 comments

Comments

@SpencerC
Copy link
Contributor

SpencerC commented Mar 26, 2022

Overview

The only thing better than a Go gRPC API would be if you guys made it easy to get the data out of protos and into local structures.

How it might look

type Person struct {
	ID   uuid.UUID `db:"id"`
	Name string    `db:"name"`
	Age  int64     `db:"age"`
}

// Do expensive reflection work ahead of time.
var peopleStruct = client.NewStruct(&Person{})

func main() {
	c, err := client.NewStargateClientWithConn(conn)
	if err != nil {
		log.Fatalf("error creating Stargate client: %v", err)
	}

	res, err := c.ExecuteQuery(
		&pb.Query{
			Cql: "SELECT (id, name, age) FROM people",
		},
	)
	if err != nil {
		log.Fatalf("error executing query: %v", err)
	}

	// CURRENT APPROACH
	rows := res.GetResultSet().GetRows()
	ppl := make([]*Person, 0, len(rows))
	for _, row := range rows {
		vals := row.GetValues()
		ppl = append(ppl, &Person{
			ID:   client.ToUUID(vals[0]),
			Name: client.ToString(vals[1]),
			Age: client.ToInt(vals[2]),
		})
	}
	
	/// DESIRED APPROACH
	ppl, err := peopleStruct.Scan(res) // []*People, error
	if err != nil {
		log.Fatalf("error scanning results to dataset: %v", err)
	}
}

Helpful Links

https://github.com/huandu/go-sqlbuilder#using-struct-as-a-light-weight-orm
https://gist.github.com/sosedoff/b373623a9572cf1a992486d2d87dcd85
https://stackoverflow.com/questions/10858787/what-are-the-uses-for-tags-in-go

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

No branches or pull requests

1 participant