diff --git a/request.go b/request.go new file mode 100644 index 0000000000000000000000000000000000000000..d225cb171a49b042a8f27ef2d861dcb989538685 --- /dev/null +++ b/request.go @@ -0,0 +1,95 @@ +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 +} diff --git a/test.json b/test.json new file mode 100644 index 0000000000000000000000000000000000000000..69b4fa18c6d399b382c4b81da9cdba5be5bfb9ca --- /dev/null +++ b/test.json @@ -0,0 +1,17 @@ +{ + "_id": "Characters/JonSnow", + "_key": "JonSnow", + "_rev": "_c-44iYG--C", + "attributes": { + "age": 16, + "alive": true, + "name": "Jon", + "surname": "Snow", + "traits": [ + "A", + "B", + "C", + "F" + ] + } +}