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

En/arango db support #1429

merged 60 commits into from
Feb 6, 2025

Conversation

Umang01-hash
Copy link
Member

@Umang01-hash Umang01-hash commented Jan 23, 2025

Pull Request Template

Description:

Docker Image:

docker run -d --name arangodb \
  -p 8529:8529 \
  -e ARANGO_ROOT_PASSWORD=root\
  --pull always \
  arangodb:latest

Example

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"`
}

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

	// Configure the ArangoDB client
	arangoClient := arangodb.New(arangodb.Config{
		Host:     "localhost",
		User:     "root",
		Password: "root",
		Port:     8529,
	})
	app.AddArangoDB(arangoClient)

	// Example routes demonstrating different types of operations
	app.POST("/setup", Setup)
	app.POST("/users/{name}", CreateUserHandler)
	app.POST("/friends", CreateFriendship)
	app.GET("/friends/{collection}/{vertexID}", GetEdgesHandler)

	app.Run()
}

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

	if err := createCollection(ctx, "social_network", "persons"); err != nil {
		return nil, err
	}
	if err := createCollection(ctx, "social_network", "friendships"); err != nil {
		return nil, err
	}

	// Define and create the graph
	edgeDefs := arangodb.EdgeDefinition{
		{Collection: "friendships", From: []string{"persons"}, To: []string{"persons"}},
	}

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

	return "Setup completed successfully", nil
}

// Helper function to create collections
func createCollection(ctx *gofr.Context, dbName, collectionName string) error {
	_, err := ctx.ArangoDB.CreateDocument(ctx, dbName, collectionName, nil)
	if err != nil {
		return fmt.Errorf("failed to create collection %s: %w", collectionName, err)
	}
	return nil
}

// CreateUserHandler demonstrates user management and document creation
func CreateUserHandler(ctx *gofr.Context) (interface{}, error) {
	name := ctx.PathParam("name")

	// Create a person document
	person := Person{
		Name: name,
		Age:  25,
	}
	docID, err := ctx.ArangoDB.CreateDocument(ctx, "social_network", "persons", person)
	if err != nil {
		return nil, fmt.Errorf("failed to create person document: %w", err)
	}

	return map[string]string{
		"message": "User created successfully",
		"docID":   docID,
	}, nil
}

// CreateFriendship demonstrates edge document creation
func CreateFriendship(ctx *gofr.Context) (interface{}, error) {
	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
	}

	edgeDocument := map[string]any{
		"_from":     fmt.Sprintf("persons/%s", req.From),
		"_to":       fmt.Sprintf("persons/%s", req.To),
		"startDate": req.StartDate,
	}

	// Create an edge document for the friendship
	edgeID, err := ctx.ArangoDB.CreateDocument(ctx, "social_network", "friendships", edgeDocument)
	if err != nil {
		return nil, fmt.Errorf("failed to create friendship: %w", err)
	}

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

// GetEdgesHandler demonstrates fetching edges connected to a vertex
func GetEdgesHandler(ctx *gofr.Context) (interface{}, error) {
	collection := ctx.PathParam("collection")
	vertexID := ctx.PathParam("vertexID")

	fullVertexID := fmt.Sprintf("%s/%s", collection, vertexID)

	// Prepare a slice to hold edge details
	edges := make(arangodb.EdgeDetails, 0)

	// Fetch all edges connected to the given vertex
	err := ctx.ArangoDB.GetEdges(ctx, "social_network", "social_graph", "friendships",
		fullVertexID, &edges)
	if err != nil {
		return nil, fmt.Errorf("failed to get edges: %w", err)
	}

	return map[string]interface{}{
		"vertexID": vertexID,
		"edges":    edges,
	}, nil
}

Checklist:

  • I have formatted my code using goimport and golangci-lint.
  • All new code is covered by unit tests.
  • This PR does not decrease the overall code coverage.
  • I have reviewed the code comments and documentation for clarity.

Thank you for your contribution!

Copy link
Contributor

@coolwednesday coolwednesday left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add it in the datasources' Readme and the docker image used for testing in the contributing.md file.

go.mod Outdated Show resolved Hide resolved
pkg/gofr/datasource/arango/metrics.go Outdated Show resolved Hide resolved
pkg/gofr/container/datasources.go Outdated Show resolved Hide resolved
docs/advanced-guide/injecting-databases-drivers/page.md Outdated Show resolved Hide resolved
docs/advanced-guide/injecting-databases-drivers/page.md Outdated Show resolved Hide resolved
pkg/gofr/datasource/arango/arango.go Outdated Show resolved Hide resolved
pkg/gofr/datasource/arango/arango.go Outdated Show resolved Hide resolved
docs/advanced-guide/injecting-databases-drivers/page.md Outdated Show resolved Hide resolved
pkg/gofr/datasource/arango/arango_db.go Outdated Show resolved Hide resolved
pkg/gofr/datasource/arango/logger.go Outdated Show resolved Hide resolved
pkg/gofr/container/container.go Outdated Show resolved Hide resolved
pkg/gofr/datasource/arango/arango.go Outdated Show resolved Hide resolved
pkg/gofr/datasource/arango/arango.go Outdated Show resolved Hide resolved
pkg/gofr/datasource/arango/arango.go Outdated Show resolved Hide resolved
docs/advanced-guide/injecting-databases-drivers/page.md Outdated Show resolved Hide resolved
docs/advanced-guide/injecting-databases-drivers/page.md Outdated Show resolved Hide resolved
pkg/gofr/container/datasources.go Outdated Show resolved Hide resolved
pkg/gofr/datasource/arangodb/arango.go Outdated Show resolved Hide resolved
pkg/gofr/datasource/arangodb/arango.go Outdated Show resolved Hide resolved
pkg/gofr/datasource/arangodb/logger.go Show resolved Hide resolved
@aryanmehrotra aryanmehrotra merged commit cd52265 into development Feb 6, 2025
14 checks passed
@aryanmehrotra aryanmehrotra deleted the en/arangoDB_support branch February 6, 2025 05:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

ArangoDB Datastore and Caching Support
4 participants