Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

En/arango db support #1429

Merged
merged 60 commits into from
Feb 6, 2025
Merged
Show file tree
Hide file tree
Changes from 42 commits
Commits
Show all changes
60 commits
Select commit Hold shift + click to select a range
61e338e
add arangoDB interface
Umang01-hash Jan 15, 2025
ab624c6
add skeleton and implement a few methods
Umang01-hash Jan 16, 2025
a8f7e67
complete the implementation of rest of the methods
Umang01-hash Jan 16, 2025
c91651a
complete the implementation of rest of the methods
Umang01-hash Jan 16, 2025
1b0bfc2
add AddArango method in app and Arango field in container
Umang01-hash Jan 17, 2025
86a5782
test db methods
Umang01-hash Jan 17, 2025
1a217c2
add arrangoDB documentation
Umang01-hash Jan 17, 2025
e102125
adding tests for arango client
Umang01-hash Jan 17, 2025
3bf455a
Merge branch 'development' of github.com:gofr-dev/gofr into en/arango…
Umang01-hash Jan 22, 2025
6f80ee7
add tests for client user and collection database methods
Umang01-hash Jan 22, 2025
8dc56c4
resolve linters
Umang01-hash Jan 22, 2025
41116e1
add tests for document and collection
Umang01-hash Jan 23, 2025
e7ab172
resolve linters
Umang01-hash Jan 23, 2025
87b2425
Merge branch 'development' of github.com:gofr-dev/gofr into en/arango…
Umang01-hash Jan 23, 2025
d12e756
--amendresolve linters
Umang01-hash Jan 23, 2025
8b22d01
resolve dependencies
Umang01-hash Jan 23, 2025
a22227a
embedd DB, Graph, and Document struct into Client struct
Umang01-hash Jan 23, 2025
cb4f9ce
resolve code climate issues
Umang01-hash Jan 23, 2025
09df2f5
Merge branch 'development' of github.com:gofr-dev/gofr into en/arango…
Umang01-hash Jan 27, 2025
7695175
Merge branch 'development' of github.com:gofr-dev/gofr into en/arango…
Umang01-hash Jan 28, 2025
b158d84
use any instead of EdgeDefinition in CreateGraph method
Umang01-hash Jan 28, 2025
2745f7f
minor refactoring and resolve review comments
Umang01-hash Jan 28, 2025
b26bf48
improve example in docs
Umang01-hash Jan 28, 2025
2c4df81
improve on querylog struct
Umang01-hash Jan 28, 2025
62229d2
update example in docs
Umang01-hash Jan 28, 2025
cbf4eba
do go mod tidy
Umang01-hash Jan 28, 2025
2b35a56
use arangodb prefix instead of arango
Umang01-hash Jan 28, 2025
340371d
rename package arangodb
Umang01-hash Jan 28, 2025
b0f4ce6
Merge branch 'development' of github.com:gofr-dev/gofr into en/arango…
Umang01-hash Jan 28, 2025
368927c
resolve linter
Umang01-hash Jan 28, 2025
473e9db
minor space fix
Umang01-hash Jan 28, 2025
a926620
Merge branch 'development' of github.com:gofr-dev/gofr into en/arango…
Umang01-hash Jan 30, 2025
5e40b11
resolve review comments 2
Umang01-hash Jan 30, 2025
24a9d5e
unexport methods not used in external interface
Umang01-hash Jan 30, 2025
d3e5c7f
revert go mod changes
Umang01-hash Jan 30, 2025
d43d3e5
resolve linter
Umang01-hash Jan 30, 2025
b13df67
remove dependency on arangodb.UserOptions in example
Umang01-hash Jan 30, 2025
a25c429
update documentation
Umang01-hash Jan 30, 2025
121aead
fix wrong password in documentation and add docker command in contrib…
Umang01-hash Jan 30, 2025
7d74a07
refactor Connect method
Umang01-hash Jan 30, 2025
e8e571f
minor changes in documentation
Umang01-hash Jan 31, 2025
4137572
remove user related method from arango interface
Umang01-hash Feb 3, 2025
42e3929
resolve review comments 3
Umang01-hash Feb 3, 2025
807756d
resovle minor error
Umang01-hash Feb 3, 2025
8d444cf
move unexported methods to arango_helper.go
Umang01-hash Feb 3, 2025
d6dffb4
move unexported methods in helper file
Umang01-hash Feb 3, 2025
252e9a9
minor typo
Umang01-hash Feb 3, 2025
a81d883
update example in documentation
Umang01-hash Feb 3, 2025
1951202
Merge branch 'development' into en/arangoDB_support
Umang01-hash Feb 3, 2025
ca4ff3b
Merge branch 'development' of github.com:gofr-dev/gofr into en/arango…
Umang01-hash Feb 4, 2025
2b3f148
Merge branch 'en/arangoDB_support' of github.com:gofr-dev/gofr into e…
Umang01-hash Feb 4, 2025
6ee237c
remove CreateEdgedocument method
Umang01-hash Feb 4, 2025
ed53edf
remove graphs and collections method from interface and add GetEdges …
Umang01-hash Feb 4, 2025
85137be
update documentation
Umang01-hash Feb 4, 2025
bac8ae5
update example
Umang01-hash Feb 4, 2025
ab6323d
Merge branch 'development' into en/arangoDB_support
Umang01-hash Feb 4, 2025
fb7bdd2
fix GetEdges method
Umang01-hash Feb 4, 2025
d73eb02
Merge branch 'en/arangoDB_support' of github.com:gofr-dev/gofr into e…
Umang01-hash Feb 5, 2025
b852982
fix import
Umang01-hash Feb 5, 2025
bb844c0
remove unwanted code
Umang01-hash Feb 5, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,11 @@ docker pull scylladb/scylla
docker run --name scylla -d -p 2025:9042 scylladb/scylla
docker pull surrealdb/surrealdb:latest
docker run --name surrealdb -d -p 8000:8000 surrealdb/surrealdb:latest start --bind 0.0.0.0:8000
docker run -d --name arangodb \
-p 8529:8529 \
-e ARANGO_ROOT_PASSWORD=rootpassword \
--pull always \
arangodb:latest



Expand Down
164 changes: 164 additions & 0 deletions docs/advanced-guide/injecting-databases-drivers/page.md
Original file line number Diff line number Diff line change
Expand Up @@ -949,3 +949,167 @@ func main() {
}

```


## ArangoDB
GoFr supports injecting `ArangoDB` that implements the following interface. Any driver that implements the interface can be
added using the `app.AddArangoDB()` method, and users can use ArangoDB across the application with `gofr.Context`.
Umang01-hash marked this conversation as resolved.
Show resolved Hide resolved

```go
type ArangoDB interface {
// ListDBs lists all databases in ArangoDB.
ListDBs(ctx context.Context) ([]string, error)
// CreateDB creates a new database in ArangoDB.
CreateDB(ctx context.Context, database string) error
// DropDB deletes an existing database in ArangoDB.
DropDB(ctx context.Context, database string) error

Umang01-hash marked this conversation as resolved.
Show resolved Hide resolved
// CreateCollection creates a new collection in a database with specified type.
CreateCollection(ctx context.Context, database, collection string, isEdge bool) error
// DropCollection deletes an existing collection from a database.
DropCollection(ctx context.Context, database, collection string) error
// TruncateCollection truncates a collection in a database.
TruncateCollection(ctx context.Context, database, collection string) error
// ListCollections lists all collections in a database.
ListCollections(ctx context.Context, database string) ([]string, error)

Umang01-hash marked this conversation as resolved.
Show resolved Hide resolved
// CreateDocument creates a new document in the specified collection.
CreateDocument(ctx context.Context, dbName, collectionName string, document interface{}) (string, error)
// GetDocument retrieves a document by its ID from the specified collection.
GetDocument(ctx context.Context, dbName, collectionName, documentID string, result interface{}) error
// UpdateDocument updates an existing document in the specified collection.
UpdateDocument(ctx context.Context, dbName, collectionName, documentID string, document interface{}) error
// DeleteDocument deletes a document by its ID from the specified collection.
DeleteDocument(ctx context.Context, dbName, collectionName, documentID string) error

// CreateEdgeDocument creates a new edge document between two vertices.
CreateEdgeDocument(ctx context.Context, dbName, collectionName string, from, to string, document interface{}) (string, error)
Umang01-hash marked this conversation as resolved.
Show resolved Hide resolved

// CreateGraph creates a new graph in a database taking graph name and *[]arangodb.EdgeDefinition as input.
CreateGraph(ctx context.Context, database, graph string, edgeDefinitions any) error
// DropGraph deletes an existing graph from a database.
DropGraph(ctx context.Context, database, graph string) error
// ListGraphs lists all graphs in a database.
ListGraphs(ctx context.Context, database string) ([]string, error)

// Query executes an AQL query and binds the results.
Query(ctx context.Context, dbName string, query string, bindVars map[string]any, result any) error

HealthCheck(context.Context) (any, error)
}
```

Users can easily inject a driver that supports this interface, providing usability without compromising the extensibility to use multiple databases.

Import the GoFr's external driver for ArangoDB:

```shell
go get gofr.dev/pkg/gofr/datasource/arangodb@latest
```
Umang01-hash marked this conversation as resolved.
Show resolved Hide resolved

### Example

```go
package main

import (
"fmt"

"gofr.dev/pkg/gofr"
"gofr.dev/pkg/gofr/datasource/arangodb"
)

type Person struct {
Name string `json:"name"`
Age int `json:"age"`
}

Umang01-hash marked this conversation as resolved.
Show resolved Hide resolved
type Friendship struct {
StartDate string `json:"startDate"`
}

func main() {
app := gofr.New()

// Configure the ArangoDB client
arangoClient := arangodb.New(arangodb.Config{
Host: "localhost",
User: "root",
Password: "root",
Port: 8529,
Umang01-hash marked this conversation as resolved.
Show resolved Hide resolved
})
app.AddArangoDB(arangoClient)

// Example routes demonstrating different types of operations
app.POST("/setup", Setup)
app.POST("/friends", CreateFriendship)

app.Run()
}

// Setup demonstrates database and collection creation
func Setup(ctx *gofr.Context) (interface{}, error) {
// Create a database
err := ctx.ArangoDB.CreateDB(ctx, "social_network")
if err != nil {
return nil, fmt.Errorf("failed to create database: %w", err)
}

// Create a regular collection for persons
err = ctx.ArangoDB.CreateCollection(ctx, "social_network", "persons", false)
if err != nil {
return nil, fmt.Errorf("failed to create persons collection: %w", err)
}

// Create an edge collection for friendships
err = ctx.ArangoDB.CreateCollection(ctx, "social_network", "friendships", true)
if err != nil {
return nil, fmt.Errorf("failed to create friendships collection: %w", err)
}

// Create a graph with edge definition
edgeDefs := arangodb.EdgeDefinition{
{
Collection: "friendships",
From: []string{"persons"},
To: []string{"persons"},
},
}

err = ctx.ArangoDB.CreateGraph(ctx, "social_network", "social_graph", &edgeDefs)
if err != nil {
return nil, fmt.Errorf("failed to create graph: %w", err)
}

return "Setup completed successfully", nil
}

// CreateFriendship demonstrates edge document creation
func CreateFriendship(ctx *gofr.Context) (interface{}, error) {
Umang01-hash marked this conversation as resolved.
Show resolved Hide resolved
var req struct {
From string `json:"from"`
To string `json:"to"`
StartDate string `json:"startDate"`
}

if err := ctx.Bind(&req); err != nil {
return nil, err
}

friendship := Friendship{
StartDate: req.StartDate,
}

edgeID, err := ctx.ArangoDB.CreateEdgeDocument(ctx, "social_network", "friendships", req.From, req.To, friendship)
if err != nil {
return nil, fmt.Errorf("failed to create friendship: %w", err)
}

return map[string]string{
"message": "Friendship created successfully",
"edgeID": edgeID,
}, nil
}
```


1 change: 1 addition & 0 deletions pkg/gofr/container/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ type Container struct {
OpenTSDB OpenTSDB
ScyllaDB ScyllaDB
SurrealDB SurrealDB
ArangoDB ArangoDB

KVStore KVStore

Expand Down
55 changes: 55 additions & 0 deletions pkg/gofr/container/datasources.go
Original file line number Diff line number Diff line change
Expand Up @@ -574,3 +574,58 @@ type ScyllaDBProvider interface {
ScyllaDB
provider
}

type ArangoDB interface {
// ListDBs lists all databases in ArangoDB.
ListDBs(ctx context.Context) ([]string, error)
// CreateDB creates a new database in ArangoDB.
CreateDB(ctx context.Context, database string) error
// DropDB deletes an existing database in ArangoDB.
DropDB(ctx context.Context, database string) error

// CreateCollection creates a new collection in a database with specified type.
CreateCollection(ctx context.Context, database, collection string, isEdge bool) error
// DropCollection deletes an existing collection from a database.
DropCollection(ctx context.Context, database, collection string) error
// TruncateCollection truncates a collection in a database.
TruncateCollection(ctx context.Context, database, collection string) error
// ListCollections lists all collections in a database.
ListCollections(ctx context.Context, database string) ([]string, error)

// CreateDocument creates a new document in the specified collection.
CreateDocument(ctx context.Context, dbName, collectionName string, document any) (string, error)
// GetDocument retrieves a document by its ID from the specified collection.
GetDocument(ctx context.Context, dbName, collectionName, documentID string, result any) error
// UpdateDocument updates an existing document in the specified collection.
UpdateDocument(ctx context.Context, dbName, collectionName, documentID string, document any) error
// DeleteDocument deletes a document by its ID from the specified collection.
DeleteDocument(ctx context.Context, dbName, collectionName, documentID string) error

// CreateEdgeDocument creates a new edge document between two vertices.
// The `document` parameter should be a map or struct containing the edge data.
// Example of `document` parameter:
// document := map[string]any{
// "attribute1": "value1",
// "attribute2": "value2",
// }
CreateEdgeDocument(ctx context.Context, dbName, collectionName string, from, to string, document any) (string, error)

// CreateGraph creates a new graph in a database taking graph name and *[]arangodb.EdgeDefinition as input.
CreateGraph(ctx context.Context, database, graph string, edgeDefinitions any) error
// DropGraph deletes an existing graph from a database.
DropGraph(ctx context.Context, database, graph string) error
// ListGraphs lists all graphs in a database.
ListGraphs(ctx context.Context, database string) ([]string, error)

// Query executes an AQL query and binds the results
Query(ctx context.Context, dbName string, query string, bindVars map[string]any, result any) error

HealthChecker
}

// ArangoProvider is an interface that extends ArangoDB with additional methods for logging, metrics, and connection management.
type ArangoProvider interface {
ArangoDB

provider
}
Umang01-hash marked this conversation as resolved.
Show resolved Hide resolved
3 changes: 3 additions & 0 deletions pkg/gofr/container/mock_container.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,9 @@ func NewMockContainer(t *testing.T, options ...options) (*Container, *Mocks) {
opentsdbMock := NewMockOpenTSDBProvider(ctrl)
container.OpenTSDB = opentsdbMock

arangoMock := NewMockArangoProvider(ctrl)
container.ArangoDB = arangoMock

var httpMock *service.MockHTTP

container.Services = make(map[string]service.HTTP)
Expand Down
Loading