Skip to content

Commit

Permalink
Merge pull request #8 from PascalLeMerrer/GraphManagement
Browse files Browse the repository at this point in the history
Added Graph Create, Get, List and Drop.
  • Loading branch information
Fabien Herfray committed Dec 27, 2015
2 parents 637b75a + 8a03a3c commit 9b9f92b
Show file tree
Hide file tree
Showing 3 changed files with 187 additions and 0 deletions.
46 changes: 46 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,52 @@ func main() {
}
```

## Graphs
### Overview
AQL may be used for querying graph data. But to manage graphs, aroangolite offers a few specific requests:
* CreateGraph to create a graph
* ListGraphs to list available graphs
* GetGraph to get an existing graph
* DropGraph to delete a graph

### Usage
```go
db.Run(&arangolite.CreateCollection{Name: "CollectionName"})
db.Run(&arangolite.CreateCollection{Name: "RelationshipCollectionName", Type: 3})

// check graph existence
_, err := db.Run(&arangolite.GetGraph{Name: "GraphName"})

// if graph does not exist, create a new one
if err != nil {
from := make([]string, 1)
from[0] = "FirstCollectionName"
to := make([]string, 1)
to[0] = "SecondCollectionName"

edgeDefinition := arangolite.EdgeDefinition{Collection: "EdgeCollectionName", From: from, To: to}
edgeDefinitions := make([]arangolite.EdgeDefinition, 1)
edgeDefinitions[0] = edgeDefinition
db.Run(&arangolite.CreateGraph{Name: "GraphName", EdgeDefinitions: edgeDefinitions})
}

// grab the graph
graphBytes, _ := config.DB().Run(&arangolite.GetGraph{Name: "GraphName"})
graph := &arangolite.GraphData{}
json.Unmarshal(graphBytes, &graph)
fmt.Printf("Graph: %+v", graph)

// list existing graphs
listBytes, _ := db.Run(&arangolite.ListGraphs{})
list := &arangolite.GraphList{}
json.Unmarshal(listBytes, &list)
fmt.Printf("Graph list: %+v", list)

// destroy the graph we just created, and the related collections
db.Run(&arangolite.DropGraph{Name: "GraphName", DropCollections: true})

```

## Filters
### Overview

Expand Down
127 changes: 127 additions & 0 deletions graph_requests.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
package arangolite

import (
"encoding/json"
)

// GRAPH management

// EdgeDefinition is a definition of the graph edges
type EdgeDefinition struct {
Collection string `json:"collection"`
From []string `json:"from"`
To []string `json:"to"`
}

// Graph represents a graph definition.
type Graph struct {
Name string `json:"name"`
//An array of definitions for the edges
EdgeDefinitions []EdgeDefinition `json:"edgeDefinitions",omitempty`
//An array of additional vertex collections.
OrphanCollections []string `json:"orphanCollections",omitempty`
ID string `json:"_id",omitempty`
Rev string `json:"_rev",omitempty`
}

// GraphData is a container for data returned by a GET GRAPH request
type GraphData struct {
Graph Graph `json:"graph"`
}

// GraphList is a container for data returned by a LIST GRAPHS request
type GraphList struct {
Graphs []Graph `json:"graphs"`
}

// CreateGraph creates a collection in database.
type CreateGraph struct {
Name string `json:"name"`
EdgeDefinitions []EdgeDefinition `json:"edgeDefinitions",omitempty`
OrphanCollections []string `json:"orphanCollections",omitempty`
}

func (c *CreateGraph) description() string {
return "CREATE GRAPH"
}

func (c *CreateGraph) path() string {
return "/_api/gharial"
}

func (c *CreateGraph) method() string {
return "POST"
}

func (c *CreateGraph) generate() []byte {
m, _ := json.Marshal(c)
return m
}

// GetGraph gets a graph from the graph module.
type GetGraph struct {
Name string
}

func (g *GetGraph) description() string {
return "GET GRAPH"
}

func (g *GetGraph) path() string {
return "/_api/gharial/" + g.Name
}

func (g *GetGraph) method() string {
return "GET"
}

func (g *GetGraph) generate() []byte {
return nil
}

// ListGraph lists all graphs known by the graph module.
type ListGraphs struct{}

func (l *ListGraphs) description() string {
return "LIST GRAPHS"
}

func (l *ListGraphs) path() string {
return "/_api/gharial/"
}

func (l *ListGraphs) method() string {
return "GET"
}

func (l *ListGraphs) generate() []byte {
return nil
}

// DropGraph deletes an existing graph.
type DropGraph struct {
Name string
DropCollections bool
}

func (d *DropGraph) description() string {
return "DROP GRAPH"
}

func (d *DropGraph) path() string {
var queryParameters string

if d.DropCollections {
queryParameters = "?dropCollections=true"
}

return "/_api/gharial/" + d.Name + queryParameters
}

func (d *DropGraph) method() string {
return "DELETE"
}

func (d *DropGraph) generate() []byte {
return nil
}
14 changes: 14 additions & 0 deletions graph_requests_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package arangolite

import "testing"

// TestRequests covers the graph requests
func TestGraphRequests(t *testing.T) {
db := New().LoggerOptions(false, false, false).Connect("http://arangodb:8000", "dbName", "foo", "bar")

db.Run(&CreateGraph{})
db.Run(&ListGraphs{})
db.Run(&GetGraph{})
db.Run(&DropGraph{})

}

0 comments on commit 9b9f92b

Please sign in to comment.