forked from arangodb/go-driver
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample_graph_test.go
120 lines (102 loc) · 3.22 KB
/
example_graph_test.go
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
//
// DISCLAIMER
//
// Copyright 2017 ArangoDB GmbH, Cologne, Germany
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// Copyright holder is ArangoDB GmbH, Cologne, Germany
//
// +build !auth
// This example demonstrates how to create a graph, how to add vertices and edges and how to delete it again.
package driver_test
import (
"log"
"fmt"
driver "github.com/arangodb/go-driver"
"github.com/arangodb/go-driver/http"
)
type MyObject struct {
Name string `json:"_key"`
Age int `json:"age"`
}
type MyEdgeObject struct {
From string `json:"_from"`
To string `json:"_to"`
}
func Example_createGraph() {
fmt.Println("Hello World")
// Create an HTTP connection to the database
conn, err := http.NewConnection(http.ConnectionConfig{
Endpoints: []string{"http://localhost:8529"},
})
if err != nil {
log.Fatalf("Failed to create HTTP connection: %v", err)
}
// Create a client
c, err := driver.NewClient(driver.ClientConfig{
Connection: conn,
})
// Create database
db, err := c.CreateDatabase(nil, "my_graph_db", nil)
if err != nil {
log.Fatalf("Failed to create database: %v", err)
}
// define the edgeCollection to store the edges
var edgeDefinition driver.EdgeDefinition
edgeDefinition.Collection = "myEdgeCollection"
// define a set of collections where an edge is going out...
edgeDefinition.From = []string{"myCollection1", "myCollection2"}
// repeat this for the collections where an edge is going into
edgeDefinition.To = []string{"myCollection1", "myCollection3"}
// A graph can contain additional vertex collections, defined in the set of orphan collections
var options driver.CreateGraphOptions
options.OrphanVertexCollections = []string{"myCollection4", "myCollection5"}
options.EdgeDefinitions = []driver.EdgeDefinition{edgeDefinition}
// now it's possible to create a graph
graph, err := db.CreateGraph(nil, "myGraph", &options)
if err != nil {
log.Fatalf("Failed to create graph: %v", err)
}
// add vertex
vertexCollection1, err := graph.VertexCollection(nil, "myCollection1")
if err != nil {
log.Fatalf("Failed to get vertex collection: %v", err)
}
myObjects := []MyObject{
MyObject{
"Homer",
38,
},
MyObject{
"Marge",
36,
},
}
_, _, err = vertexCollection1.CreateDocuments(nil, myObjects)
if err != nil {
log.Fatalf("Failed to create vertex documents: %v", err)
}
// add edge
edgeCollection, _, err := graph.EdgeCollection(nil, "myEdgeCollection")
if err != nil {
log.Fatalf("Failed to select edge collection: %v", err)
}
edge := MyEdgeObject{From: "myCollection1/Homer", To: "myCollection1/Marge"}
_, err = edgeCollection.CreateDocument(nil, edge)
if err != nil {
log.Fatalf("Failed to create edge document: %v", err)
}
// delete graph
graph.Remove(nil)
}