Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
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
}