aranGoDriver

package module
v0.0.0-...-cf36447 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 20, 2023 License: MIT Imports: 11 Imported by: 0

README

aranGoDriver Build Status

This project is a golang-driver for ArangoDB writen in go.
There is also an embedded-in-memory-Database to run all your tests.

Currently implemented:

If you miss something, please contact me!

Getting started

All you need is a running Arango-DB and a go-environment.

Install aranGoDriver: go get github.com/TobiEiss/aranGoDriver

Write your first aranGoDriver-Programm:

func main() {
    var session aranGoDriver.Session
    
    // Initialize a arango-Session with the address to your arangoDB.
    //
    // If you write a test use:
    // session = aranGoDriver.NewTestSession()
    //
    session = aranGoDriver.NewAranGoDriverSession("http://localhost:8529")

    // Connect to your arango-database:
	session.Connect("usnername", "secretPassword")

    // Concrats, you are connected!
    // Let's print out all your databases
    list, err := session.ListDBs()
    if err != nil {
        log.Fatal("there was a problem: ", err)
    }
    log.Println(list)

    // Create a new database
    err = session.CreateDB("myNewDatabase")
    // TODO: handle err

    // Create a new collection
    err = session.CreateCollection("myNewDatabase", "myNewCollection")
    // TODO: handle err

    // Create Document
    newDocument := make(map[string]interface{})
    newDocument["foo"] = "bar"
    arangoID, err = session.CreateDocument("myNewDatabase", "myNewCollection", newDocument)
}

Test

Test against a fake-in-memory-database:
go test
Test with a real database
go test -dbhost http://localhost:8529 -dbusername root -dbpassword password123

Usage

Connect to your ArangoDB

You need a new Session to your database with the hostname as parameter. Then connect with an existing username and a password.

session := aranGoDriver.NewAranGoDriverSession("http://localhost:8529")
session.Connect("username", "password")
Version
version, err := session.Version()
User

To use this methods you need a session as root user.

// create a new user
err := session.CreateUser("username", "password")

// delete an existing user
err := session.DropUser("username")

// grant permissions for a database
err := session.GrantDB("database", "username", "rw")
// Instead of "rw" you can also use "ro" or "none"

// grant permissions for collections
err := session.GrantCollection("database", "collection", "username", "rw") 
// Instead of "rw" you can also use "ro" or "none"
// and instead of a collection name, you can use "*" for all collections
Database
// list databases
list := session.ListDBs()
fmt.Println(list) // will print ([]string): [ _system test testDB]

// create databases
err := session.CreateDB("myNewDatabase")

// drop databases
err = session.DropDB("myNewDatabase")
Collection
// create a collection in a database
err = CreateCollection("myNewDatabase", "myNewCollection")

// drop collection from database
err = DropCollection("myNewDatabase", "myNewCollection")

// truncate database
err = TruncateCollection("myNewDatabase", "myNewCollection")

EdgeCollection:

// create a collection in a database
err = CreateEdgeCollection("myNewDatabase", "myNewEdgeCollection")
Document
// create document
testDoc["foo"] = "bar"
arangoID, err := session.CreateDocument("myNewDatabase", "myNewCollection", testDoc)

// get by id
resultAsMap, err := session.GetCollectionByID("myNewDatabase", idOfDocument)

// update Document
testDoc["bar"] = "foo"
err = session.UpdateDocument("myNewDatabase", arangoID.ID, testDoc)

EdgeDocument

arangoID, err := session.CreateDocument("myNewDatabase", "myNewCollection", testDoc)
Graphs

To create a graph, you need to define the edges and nodes for the graph. This can be done with the EdgeDefinition model.

edgeDefinition := models.EdgeDefinition{
    Collection: "myEdgeCollection",
    From:       []string{"myCollection1"},
    To:         []string{"myCollection2"}}
edgeDefinitions := []models.EdgeDefinition{edgeDefinition}

err := session.CreateGraph("myDatabase", "myGraph", edgeDefinitions)

If you want to get rid of an existing graph, you can use the DropGraph method.

err := session.DropGraph("myDatabase", "myGraph")

For an overview of your existing graphs, you can use ListGraphs.

str, b, err := session.ListGraphs("myDatabase")
Migrations

In some cases you need 'migrations'. For example, you need default-user in your database in every environment. For this case, you can use migrations. The aranGoDriver write his own memo in a migrations-Collection in the standard -system-Database of arango, and execute the migration only one time. AranGoDriver will identificate the migration by name. Take a look to the following example:

// check migrations
mig1 := aranGoDriver.Migration{
    Name: "mig1", // name of migration to identificate
    Handle: func(embeddedSession aranGoDriver.Session) {
        testMap := make(map[string]interface{})
        testMap["foo"] = "foo"
        // you can do everything with the database
        embeddedSession.CreateDocument("myDatabase", "myCollection", testMap)
    },
}
// excute migration
// Run This line before you 'main-loop' of your program
session.Migrate(mig1)
aql
// create query
query := "FOR element in testColl FILTER element.foo == 'bar' RETURN element"
response, err := session.AqlQuery("myNewDatabase", query, true, 1)

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type AqlFake

type AqlFake struct {
	MapResult []interface{}
}

type AranGoSession

type AranGoSession struct {
	// contains filtered or unexported fields
}

AranGoSession represent to Session

func NewAranGoDriverSession

func NewAranGoDriverSession(host string) *AranGoSession

NewAranGoDriverSession creates a new instance of a AranGoDriver-Session. Need a host (e.g. "http://localhost:8529/")

func (*AranGoSession) AqlQuery

func (session *AranGoSession) AqlQuery(typ interface{}, dbname string, query string, count bool, batchSize int) error

AqlQuery send a query

func (*AranGoSession) Connect

func (session *AranGoSession) Connect(username string, password string) error

Connect to arangoDB

func (*AranGoSession) CreateCollection

func (session *AranGoSession) CreateCollection(dbname string, collectionName string) error

CreateCollection creates a collection

func (*AranGoSession) CreateDB

func (session *AranGoSession) CreateDB(dbname string) error

CreateDB creates a new db

func (*AranGoSession) CreateDocument

func (session *AranGoSession) CreateDocument(dbname string, collectionName string, object interface{}) (models.ArangoID, error)

CreateDocument creates a document in a collection in a database

func (*AranGoSession) CreateEdgeCollection

func (session *AranGoSession) CreateEdgeCollection(dbname string, edgeName string) error

CreateEdgeCollection creates a edge to DB

func (*AranGoSession) CreateEdgeDocument

func (session *AranGoSession) CreateEdgeDocument(dbname string, edgeName string, from string, to string) (models.ArangoID, error)

func (*AranGoSession) CreateGraph

func (session *AranGoSession) CreateGraph(dbname string, graphName string, edgeDefinitions []models.EdgeDefinition) error

func (*AranGoSession) CreateUser

func (session *AranGoSession) CreateUser(username string, password string) error

Add a new database user

func (*AranGoSession) DropCollection

func (session *AranGoSession) DropCollection(dbname string, collectionName string) error

DropCollection deletes a collection

func (*AranGoSession) DropDB

func (session *AranGoSession) DropDB(dbname string) error

DropDB drop a database

func (*AranGoSession) DropGraph

func (session *AranGoSession) DropGraph(dbname string, graphName string) error

func (*AranGoSession) DropUser

func (session *AranGoSession) DropUser(username string) error

Delete an existing user

func (*AranGoSession) GetCollectionByID

func (session *AranGoSession) GetCollectionByID(dbname string, id string) (map[string]interface{}, error)

GetCollectionByID search collection by id

func (*AranGoSession) GrantCollection

func (session *AranGoSession) GrantCollection(dbname string, collectionName string, username string, level string) error

Set the accesslevel for an user on a collection Possible values for level are: rw, ro and none

func (*AranGoSession) GrantDB

func (session *AranGoSession) GrantDB(dbname string, username string, level string) error

Set the accesslevel for an user on a database Possible values for level are: rw, ro and none

func (*AranGoSession) ListCollections

func (session *AranGoSession) ListCollections(dbname string) ([]string, error)

func (*AranGoSession) ListDBs

func (session *AranGoSession) ListDBs() ([]string, error)

ListDBs lists all db's

func (*AranGoSession) ListGraphs

func (session *AranGoSession) ListGraphs(dbname string) (interface{}, error)

func (*AranGoSession) Migrate

func (session *AranGoSession) Migrate(migrations ...Migration) error

Migrate migrates a migration

func (*AranGoSession) Query

func (session *AranGoSession) Query(typ interface{}, methode string, route string, body interface{}) error

func (*AranGoSession) TruncateCollection

func (session *AranGoSession) TruncateCollection(dbname string, collectionName string) error

TruncateCollection truncate collections

func (*AranGoSession) UpdateDocument

func (session *AranGoSession) UpdateDocument(dbname string, id string, object interface{}) error

UpdateDocument updates an Object

func (*AranGoSession) Version

func (session *AranGoSession) Version() (Version, error)

Version returns current version

type Migration

type Migration struct {
	models.ArangoID `json:"-"`
	Name            string           `json:"name"`
	Handle          MigrationExecute `json:"-"`
	Status          MigrationStatus  `json:"status"`
}

Migration represent the whole migration

type MigrationExecute

type MigrationExecute func(Session)

MigrationExecute is the func to execute the migration

type MigrationStatus

type MigrationStatus string

MigrationStatus is like a enum and represent the status

const (
	// Started means, the migration has started
	Started MigrationStatus = "started"
	// Finished means, the migration has finished
	Finished MigrationStatus = "finished"
)

type Session

type Session interface {
	Connect(username string, password string) error
	Version() (Version, error)

	// users
	CreateUser(username string, password string) error
	DropUser(username string) error
	GrantDB(dbname string, username string, level string) error
	GrantCollection(dbname string, collectionName string, username string, level string) error

	// databases
	ListDBs() ([]string, error)
	CreateDB(dbname string) error
	DropDB(dbname string) error

	ListCollections(dbname string) ([]string, error)
	CreateCollection(dbname string, collectionName string) error
	DropCollection(dbname string, collectionName string) error
	TruncateCollection(dbname string, collectionName string) error

	CreateEdgeCollection(dbname string, edgeName string) error
	CreateEdgeDocument(dbname string, edgeName string, from string, to string) (models.ArangoID, error)

	CreateGraph(dbname string, graphName string, edgeDefinitions []models.EdgeDefinition) error
	ListGraphs(dbname string) (interface{}, error)
	DropGraph(dbname string, graphName string) error

	// GetCollectionByID search collection by id
	// returns:
	// -> result as map
	// -> error if applicable
	GetCollectionByID(dbname string, id string) (map[string]interface{}, error)
	CreateDocument(dbname string, collectionName string, object interface{}) (models.ArangoID, error)
	UpdateDocument(dbname string, id string, object interface{}) error

	// AqlQuery returns: result as array-map, error
	AqlQuery(typ interface{}, dbname string, query string, count bool, batchSize int) error

	// Query with auth
	Query(typ interface{}, methode string, route string, body interface{}) error

	Migrate(migration ...Migration) error
}

type TestSession

type TestSession struct {
	// contains filtered or unexported fields
}

func NewTestSession

func NewTestSession() *TestSession

func (*TestSession) AddAqlFake

func (session *TestSession) AddAqlFake(aql string, fake AqlFake)

func (*TestSession) AqlQuery

func (session *TestSession) AqlQuery(typ interface{}, dbname string, query string, count bool, batchSize int) error

func (TestSession) Connect

func (session TestSession) Connect(username string, password string) error

Connect test

func (*TestSession) CreateCollection

func (session *TestSession) CreateCollection(dbname string, collectionName string) error

func (*TestSession) CreateDB

func (session *TestSession) CreateDB(dbname string) error

CreateDB test create a db

func (*TestSession) CreateDocument

func (session *TestSession) CreateDocument(dbname string, collectionName string, object interface{}) (models.ArangoID, error)

func (*TestSession) CreateEdgeCollection

func (session *TestSession) CreateEdgeCollection(dbname string, edgeName string) error

func (*TestSession) CreateEdgeDocument

func (session *TestSession) CreateEdgeDocument(dbname string, edgeName string, from string, to string) (models.ArangoID, error)

func (*TestSession) CreateGraph

func (session *TestSession) CreateGraph(dbname string, graphName string, edgeDefinitions []models.EdgeDefinition) error

func (*TestSession) CreateUser

func (session *TestSession) CreateUser(username string, password string) error

func (*TestSession) DropCollection

func (session *TestSession) DropCollection(dbname string, collectionName string) error

func (*TestSession) DropDB

func (session *TestSession) DropDB(dbname string) error

func (*TestSession) DropGraph

func (session *TestSession) DropGraph(dbname string, graphName string) error

func (*TestSession) DropUser

func (session *TestSession) DropUser(username string) error

func (*TestSession) GetCollectionByID

func (session *TestSession) GetCollectionByID(dbname string, id string) (map[string]interface{}, error)

func (*TestSession) GrantCollection

func (session *TestSession) GrantCollection(dbname string, collectionName string, username string, level string) error

func (*TestSession) GrantDB

func (session *TestSession) GrantDB(dbname string, username string, level string) error

func (*TestSession) ListCollections

func (session *TestSession) ListCollections(dbname string) ([]string, error)

func (*TestSession) ListDBs

func (session *TestSession) ListDBs() ([]string, error)

func (*TestSession) ListGraphs

func (session *TestSession) ListGraphs(dbname string) (interface{}, error)

func (*TestSession) Migrate

func (tsession *TestSession) Migrate(migrations ...Migration) error

func (*TestSession) Query

func (session *TestSession) Query(typ interface{}, methode string, route string, body interface{}) error

func (*TestSession) TruncateCollection

func (session *TestSession) TruncateCollection(dbname string, collectionName string) error

func (*TestSession) UpdateDocument

func (session *TestSession) UpdateDocument(dbname string, id string, object interface{}) error

func (*TestSession) Version

func (session *TestSession) Version() (Version, error)

Version returns hard-codeed the testDB as Version

type Version

type Version struct {
	Server  string `json:"server"`
	License string `json:"license"`
}

Version represent the version and license of the ArangoDB

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL