-
Notifications
You must be signed in to change notification settings - Fork 2
/
aranGoSession.go
282 lines (230 loc) · 8.88 KB
/
aranGoSession.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
package aranGoDriver
import (
"errors"
"net/http"
"github.com/TobiEiss/aranGoDriver/aranGoConnection"
"github.com/TobiEiss/aranGoDriver/models"
)
// AranGoSession represent to Session
type AranGoSession struct {
arangoCon *aranGoConnection.AranGoConnection
}
const urlAuth = "/_open/auth"
const urlDatabase = "/_api/database"
const urlCollection = "/_api/collection"
const urlDocument = "/_api/document"
const urlCursor = "/_api/cursor"
const urlVersion = "/_api/version"
const urlGraph = "/_api/gharial"
const urlUser = "/_api/user"
const systemDB = "_system"
const migrationColl = "migrations"
// NewAranGoDriverSession creates a new instance of a AranGoDriver-Session.
// Need a host (e.g. "http://localhost:8529/")
func NewAranGoDriverSession(host string) *AranGoSession {
return &AranGoSession{aranGoConnection.NewAranGoConnection(host)}
}
// Connect to arangoDB
func (session *AranGoSession) Connect(username string, password string) error {
credentials := models.Credentials{}
credentials.Username = username
credentials.Password = password
var resultMap map[string]string
err := session.arangoCon.Query(&resultMap, http.MethodPost, urlAuth, credentials)
if err == nil {
session.arangoCon.SetJwtKey(resultMap["jwt"])
}
return err
}
func (session *AranGoSession) Query(typ interface{}, methode string, route string, body interface{}) error {
return session.arangoCon.Query(typ, methode, route, body)
}
// Version returns current version
func (session *AranGoSession) Version() (Version, error) {
var result Version
err := session.arangoCon.Query(&result, http.MethodGet, urlVersion, nil)
return result, err
}
// Add a new database user
func (session *AranGoSession) CreateUser(username string, password string) error {
body := make(map[string]interface{})
body["user"] = username
body["passwd"] = password
var response interface{}
err := session.arangoCon.Query(&response, http.MethodPost, urlUser, body)
return err
}
// Delete an existing user
func (session *AranGoSession) DropUser(username string) error {
var response interface{}
err := session.arangoCon.Query(&response, http.MethodDelete, urlUser+"/"+username, nil)
return err
}
// Set the accesslevel for an user on a database
// Possible values for level are: rw, ro and none
func (session *AranGoSession) GrantDB(dbname string, username string, level string) error {
body := make(map[string]interface{})
body["grant"] = level
var response interface{}
err := session.arangoCon.Query(&response, http.MethodPut, urlUser+"/"+username+"/database/"+dbname, body)
return err
}
// Set the accesslevel for an user on a collection
// Possible values for level are: rw, ro and none
func (session *AranGoSession) GrantCollection(dbname string, collectionName string, username string, level string) error {
body := make(map[string]interface{})
body["grant"] = level
var response interface{}
err := session.arangoCon.Query(&response, http.MethodPut, urlUser+"/"+username+"/database/"+dbname+"/"+collectionName, body)
return err
}
// ListDBs lists all db's
func (session *AranGoSession) ListDBs() ([]string, error) {
var databaseWrapper struct {
Databases []string `json:"result,omitempty"`
}
err := session.arangoCon.Query(&databaseWrapper, http.MethodGet, urlDatabase, nil)
return databaseWrapper.Databases, err
}
// CreateDB creates a new db
func (session *AranGoSession) CreateDB(dbname string) error {
body := make(map[string]string)
body["name"] = dbname
var result interface{}
err := session.arangoCon.Query(&result, http.MethodPost, urlDatabase, body)
return err
}
// DropDB drop a database
func (session *AranGoSession) DropDB(dbname string) error {
var response interface{}
err := session.arangoCon.Query(&response, http.MethodDelete, urlDatabase+"/"+dbname, nil)
return err
}
// CreateCollection creates a collection
func (session *AranGoSession) CreateCollection(dbname string, collectionName string) error {
body := make(map[string]string)
body["name"] = collectionName
var result interface{}
err := session.arangoCon.Query(&result, http.MethodPost, "/_db/"+dbname+urlCollection, body)
return err
}
// CreateEdgeCollection creates a edge to DB
func (session *AranGoSession) CreateEdgeCollection(dbname string, edgeName string) error {
body := make(map[string]interface{})
body["name"] = edgeName
body["type"] = 3
var result interface{}
err := session.arangoCon.Query(&result, http.MethodPost, "/_db/"+dbname+urlCollection, body)
return err
}
func (session *AranGoSession) CreateGraph(dbname string, graphName string, edgeDefinitions []models.EdgeDefinition) error {
body := make(map[string]interface{})
body["name"] = graphName
body["edgeDefinitions"] = edgeDefinitions
var response interface{}
err := session.arangoCon.Query(&response, http.MethodPost, "/_db/"+dbname+urlGraph, body)
return err
}
func (session *AranGoSession) ListGraphs(dbname string) (interface{}, error) {
var result interface{}
err := session.arangoCon.Query(&result, http.MethodGet, "/_db/"+dbname+urlGraph, nil)
return result, err
}
func (session *AranGoSession) DropGraph(dbname string, graphName string) error {
var response interface{}
err := session.arangoCon.Query(&response, http.MethodDelete, "/_db/"+dbname+urlGraph+"/"+graphName, nil)
return err
}
func (session *AranGoSession) CreateEdgeDocument(dbname string, edgeName string, from string, to string) (models.ArangoID, error) {
body := make(map[string]interface{})
body["_from"] = from
body["_to"] = to
var aranggoID models.ArangoID
err := session.arangoCon.Query(&aranggoID, http.MethodPost, "/_db/"+dbname+urlDocument+"/"+edgeName, body)
return aranggoID, err
}
func (session *AranGoSession) ListCollections(dbname string) ([]string, error) {
var collections []string
err := session.arangoCon.Query(&collections, http.MethodGet, "/_db/"+dbname+urlCollection, nil)
return collections, err
}
// DropCollection deletes a collection
func (session *AranGoSession) DropCollection(dbname string, collectionName string) error {
var response interface{}
err := session.arangoCon.Query(&response, http.MethodDelete, "/_db/"+dbname+urlCollection+"/"+collectionName, nil)
return err
}
// TruncateCollection truncate collections
func (session *AranGoSession) TruncateCollection(dbname string, collectionName string) error {
var response interface{}
err := session.arangoCon.Query(&response, http.MethodPut, "/_db/"+dbname+urlCollection+"/"+collectionName+"/truncate", nil)
return err
}
// CreateDocument creates a document in a collection in a database
func (session *AranGoSession) CreateDocument(dbname string, collectionName string, object interface{}) (models.ArangoID, error) {
var aranggoID models.ArangoID
err := session.arangoCon.Query(&aranggoID, http.MethodPost, "/_db/"+dbname+urlDocument+"/"+collectionName, object)
return aranggoID, err
}
// AqlQuery send a query
func (session *AranGoSession) AqlQuery(typ interface{}, dbname string, query string, count bool, batchSize int) error {
// build request
requestBody := make(map[string]interface{})
requestBody["query"] = query
requestBody["count"] = count
requestBody["batchSize"] = batchSize
var result struct {
Error bool `json:"error"`
Result interface{} `json:"result"`
}
result.Result = typ
err := session.arangoCon.Query(&result, http.MethodPost, "/_db/"+dbname+urlCursor, requestBody)
if err != nil {
return err
}
if result.Error {
return errors.New("an error occured")
}
return err
}
// GetCollectionByID search collection by id
func (session *AranGoSession) GetCollectionByID(dbname string, id string) (map[string]interface{}, error) {
var collection map[string]interface{}
err := session.arangoCon.Query(&collection, http.MethodGet, "/_db/"+dbname+urlCollection+"/"+id, nil)
return collection, err
}
// UpdateDocument updates an Object
func (session *AranGoSession) UpdateDocument(dbname string, id string, object interface{}) error {
var result interface{}
err := session.arangoCon.Query(&result, http.MethodPatch, "/_db/"+dbname+urlDocument+"/"+id, nil)
return err
}
// Migrate migrates a migration
func (session *AranGoSession) Migrate(migrations ...Migration) error {
session.CreateCollection(systemDB, migrationColl)
// helper function
findMigration := func(name string) (Migration, bool) {
migrations := []Migration{}
query := "FOR migration IN " + migrationColl + " FILTER migration.name == '" + name + "' RETURN migration"
err := session.AqlQuery(&migrations, systemDB, query, true, 1)
return migrations[0], err == nil
}
// iterate all migrations
for _, mig := range migrations {
migration, successfully := findMigration(mig.Name)
if successfully {
if migration.Status != Finished {
mig.Handle(session)
mig.Status = Finished
session.UpdateDocument(systemDB, mig.ArangoID.ID, mig)
}
} else {
mig.Status = Started
arangoID, _ := session.CreateDocument(systemDB, migrationColl, mig)
mig.Handle(session)
mig.Status = Finished
session.UpdateDocument(systemDB, arangoID.ID, mig)
}
}
return nil
}