Skip to content
Snippets Groups Projects
Commit 9c472f85 authored by Hemming,J. (Jacob)'s avatar Hemming,J. (Jacob)
Browse files

Merge branch '134-query-service-httprequest' into 'develop'

Co-authored-by: Jacob Hemming. Implementatie query request naar db

See merge request datastrophe/microservices-backbone/query-service!2
parents af55f9cb dec96db9
No related branches found
No related tags found
No related merge requests found
package main
import (
"context"
"fmt"
"encoding/json"
"io/ioutil"
driver "github.com/arangodb/go-driver"
"github.com/arangodb/go-driver/http"
)
// Document with Empty struct to retrieve all data from the DB Document
type Document map[string]interface{}
// GeneralFormat is a generalformat to send data the frontend
type GeneralFormat map[string]interface{}
//attr interface{}
//map[1 , 2 , 3 map [ .. ]]
func main() {
conn, err := http.NewConnection(http.ConnectionConfig{
Endpoints: []string{"http://localhost:8529"},
})
if err != nil {
fmt.Printf("auwa das tut weh") // Handle error
}
c, err := driver.NewClient(driver.ClientConfig{
Connection: conn,
Authentication: driver.BasicAuthentication("root", "openSesame"),
})
if err != nil {
fmt.Printf("doet het niet") // Handle error
}
ctx := context.Background()
db, err := c.Database(ctx, "_system")
if err != nil {
// handle error
}
query := "FOR d IN Characters LIMIT 10 RETURN d"
cursor, err := db.Query(ctx, query, nil)
if err != nil {
fmt.Printf("shalom") // handle error
}
defer cursor.Close()
for {
var doc Document
_, err := cursor.ReadDocument(ctx, &doc)
if driver.IsNoMoreDocuments(err) {
break
} else if err != nil {
// handle other errors
}
//fmt.Printf("%T\n", doc)
formatToJSON(doc)
}
}
func formatToJSON(doc Document) {
//b, err := json.Marshal(doc)
//if err != nil {
//handle error
//}
data := parseData(doc)
fmt.Println(data)
file, _ := json.MarshalIndent(data, "", " ")
_ = ioutil.WriteFile("test.json", file, 0644)
}
func parseData(doc Document) Document {
data := make(Document)
data["_id"] = doc["_id"]
delete(doc, "_id")
data["_key"] = doc["_key"]
delete(doc, "_key")
data["_rev"] = doc["_rev"]
delete(doc, "_rev")
data["attributes"] = doc
//delete(doc, "_key")
//data.rev = fmt.Sprintf("%v", doc["_rev"])
// delete(doc, "_rev")
// data.attr = doc
return data
}
{
"_id": "Characters/JonSnow",
"_key": "JonSnow",
"_rev": "_c-44iYG--C",
"attributes": {
"age": 16,
"alive": true,
"name": "Jon",
"surname": "Snow",
"traits": [
"A",
"B",
"C",
"F"
]
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment