-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
145 lines (126 loc) · 4.89 KB
/
main.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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
package main
import (
"github.com/marvin-hansen/goC8"
"github.com/marvin-hansen/goC8/examples/sample_data"
"github.com/marvin-hansen/goC8/types"
)
// client config
const (
apiKey = "email.root.secretkey"
endpoint = "https://YOUR-ID-us-west.paas.macrometa.io"
fabric = "MyFabric"
timeout = 5 // http connection timeout in seconds
)
const (
delete = false
silent = false
collectionTeachers = "teachers"
collectionLectures = "lectures"
collectionTutorials = "tutorials"
edgeCollectionTeach = "teach"
edgeCollectionTutors = "tutors"
graph = "lectureteacher"
)
func main() {
println("Create new config ")
config := goC8.NewConfig(apiKey, endpoint, fabric, timeout)
println("Create new client")
c := goC8.NewClient(config)
println("Run setup")
setup(c)
println("Update graph")
addTutorials(c)
addTutorEdge(c)
addEdgeCollection(c)
addSingleEdge(c)
if delete {
println("Run teardown")
teardown(c)
}
}
// setup uses the built-in low-code utilities to create a collection & import data.
// These utils were written to accelerate developer velocity on standard API operations
func setup(c *goC8.Client) {
goC8.CreateCollection(c, fabric, collectionTeachers, types.DocumentCollectionType, false)
goC8.ImportCollectionData(c, fabric, collectionTeachers, sample_data.GetTeacherData(), silent)
goC8.CreateCollection(c, fabric, collectionLectures, types.DocumentCollectionType, false)
goC8.ImportCollectionData(c, fabric, collectionLectures, sample_data.GetLecturesData(), silent)
goC8.CreateCollection(c, fabric, edgeCollectionTeach, types.EdgeCollectionType, false)
goC8.ImportCollectionData(c, fabric, edgeCollectionTeach, sample_data.GetTeachEdgeData(), silent)
goC8.CreateGraph(c, fabric, graph, sample_data.GetUniversityGraphDefinition())
}
// addTutorials and subsequent update methods use the standard API to create a collection & import data
// Note the update function can be re-implemented with the utils used in setup.
// However, in non-standard cases direction API usage may be preferred for customization
func addTutorials(c *goC8.Client) {
exists, err := c.Collection.CheckCollectionExists(fabric, collectionTutorials)
goC8.CheckError(err, "Error CheckCollectionExists: ")
if !exists {
// if not create collection
collType := types.DocumentCollectionType
allowUserKeys := false
err = c.Collection.CreateNewCollection(fabric, collectionTutorials, allowUserKeys, collType)
goC8.CheckError(err, "Error CreateNewCollection")
// import data
jsonDocument := sample_data.GetTutorialsData()
_, err = c.Document.CreateNewDocument(fabric, collectionTutorials, silent, jsonDocument, nil)
goC8.CheckError(err, "Error CreateNewDocument")
}
}
func addTutorEdge(c *goC8.Client) {
// test if flight collection exists
exists, err := c.Collection.CheckCollectionExists(fabric, edgeCollectionTutors)
goC8.CheckError(err, "Error CheckCollectionExists")
if !exists {
// if not create edge collection
collType := types.EdgeCollectionType
err = c.Collection.CreateNewCollection(fabric, edgeCollectionTutors, false, collType)
goC8.CheckError(err, "Error CreateNewCollection")
// import data
jsonDocument := sample_data.GetTutorsEdgeData()
_, err = c.Document.CreateNewDocument(fabric, edgeCollectionTutors, silent, jsonDocument, nil)
goC8.CheckError(err, "Error CreateNewCollection")
}
}
func addEdgeCollection(c *goC8.Client) {
// check if collection exists. It should, just in case
exists, err := c.Collection.CheckCollectionExists(fabric, edgeCollectionTutors)
goC8.CheckError(err, "Error CheckCollectionExists")
if exists {
// if exists, update graph with new edge collection
collectionName := edgeCollectionTutors
sourceVertex := "teachers"
destinationVertex := "tutorials"
_, addErr := c.Graph.AddEdgeCollection(fabric, graph, collectionName, sourceVertex, destinationVertex)
if addErr != nil {
return
}
} else {
println("Can't update graph. Add edge collection: " + edgeCollectionTutors)
}
}
func addSingleEdge(c *goC8.Client) {
collectionID := "teach"
edgeID := "Bruce-CSC105"
// check if edge exits
exists, err := c.Graph.CheckEdgeExists(fabric, graph, collectionID, edgeID)
goC8.CheckError(err, "Error CheckEdgeExists")
if !exists {
// if not, add a new edge to the edge collection
// we use a custom payload here to set a custom key & custom field "online"
jsonPayload := []byte(`{
"_key": "Bruce-CSC105",
"_from": "teachers/Bruce",
"_to": "lectures/CSC105",
"online": false
}`)
_, createErr := c.Graph.CreateEdge(fabric, graph, collectionID, "", "", jsonPayload, false)
goC8.CheckError(createErr, "Error CreateEdge")
}
}
func teardown(c *goC8.Client) {
goC8.TeardownGraph(c, fabric, graph, true)
goC8.TeardownCollection(c, fabric, collectionTeachers)
goC8.TeardownCollection(c, fabric, collectionLectures)
goC8.TeardownCollection(c, fabric, edgeCollectionTeach)
}