Skip to content

Instantly share code, notes, and snippets.

@roboncode
Created April 11, 2019 15:11
Show Gist options
  • Save roboncode/f3ae10427fcfe5fb27ab1eab84a1a5de to your computer and use it in GitHub Desktop.
Save roboncode/f3ae10427fcfe5fb27ab1eab84a1a5de to your computer and use it in GitHub Desktop.
Orango Go Concept
package main
import (
"context"
"encoding/json"
"fmt"
"github.com/arangodb/go-driver"
"github.com/arangodb/go-driver/http"
"log"
"strings"
)
type ReturnBuilder struct {
One bool `json:"one,omitempty"`
}
type QueryBuilder struct {
Operation string `json:"operation,omitempty"`
Wheres interface{} `json:"where,omitempty"`
ReturnBuilder ReturnBuilder `json:"return,omitempty"`
}
func (q *QueryBuilder) Where(val interface{}) *QueryBuilder {
if q.Wheres == nil {
q.Wheres = val
} else {
}
return q
}
func (q *QueryBuilder) Return(rb ReturnBuilder) *QueryBuilder {
q.ReturnBuilder = rb
return q
}
func NewQueryBuilder(data string) *QueryBuilder {
var qb QueryBuilder
if strings.TrimSpace(data) != "" {
_ = json.Unmarshal([]byte(data), &qb)
}
return &qb
}
func (qb *QueryBuilder) ToString() string {
byt, _ := json.Marshal(qb)
return string(byt)
}
func (qb *QueryBuilder) ToAQL() string {
return ""
}
type Model struct {
qb QueryBuilder
}
func (m *Model) Find() *QueryBuilder {
return &QueryBuilder{
Operation: "find",
}
}
type User struct {
Model
Id string `json:"_key,omitempty"`
FirstName string `json:"firstName,omitempty"`
LastName string `json:"lastName,omitempty"`
Email string `json:"email,omitempty"`
}
func getUserCollection() string {
return "users"
}
func main() {
u := User{
FirstName: "Rob",
LastName: "Taylor",
}
//str := u.find().where("user.firstName == @name").toString()
str := u.Find().Where(map[string]interface{}{
"firstName": "Rob",
}).ToString()
fmt.Println("Q:", str)
qb := NewQueryBuilder(str)
str2 := qb.Return(ReturnBuilder{
One:true,
}).ToString()
fmt.Println(str2)
var ctx context.Context
conn, err := http.NewConnection(http.ConnectionConfig{
Endpoints: []string{"http://localhost:15100"},
})
if err != nil {
// Handle error
}
c, err := driver.NewClient(driver.ClientConfig{
Connection: conn,
})
if err != nil {
log.Println("Error:", err)
}
// Open "examples" database
ctx = context.Background()
db, err := c.Database(ctx, "examples")
if err != nil {
// Handle error
panic(err)
}
//ctx = context.Background()
//found, err := db.CollectionExists(ctx, "users")
//if err != nil {
// // handle error
//}
// Open "users" collection
//ctx = context.Background()
//col, err := db.Collection(ctx, "users")
//if err != nil {
// panic(err)
//}
// Create document
//user := User{
// FirstName: "Rob",
// LastName: "Taylor",
// Email: "[email protected]",
//}
//meta, err := col.CreateDocument(nil, user)
//if err != nil {
// panic(err)
//}
//log.Println("Metadata", meta.Key)
//log.Printf("Created document in collection '%s' in database '%s'\n", col.Name(), db.Name())
ctx = context.Background()
bindVars := map[string]interface{}{
"@col": getUserCollection(),
"name": "Rob",
}
fmt.Println("bind", bindVars)
cursor, err := db.Query(ctx, "FOR user IN @@col FILTER user.firstName == @name LIMIT 1 RETURN user", bindVars)
if err != nil {
// handle error
panic(err)
}
var users []User
for cursor.HasMore() {
var user User
_, err := cursor.ReadDocument(context.Background(), &user)
if err != nil {
panic(err)
}
users = append(users, user)
}
defer cursor.Close()
fmt.Println("Users:", users[0])
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment