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

Open
wants to merge 18 commits into
base: development
Choose a base branch
from
Open

En/arango db support #1429

wants to merge 18 commits into from

Conversation

Umang01-hash
Copy link
Contributor

@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=rootpassword \
  --pull always \
  arangodb:latest

Example

package main

import (
    "context"
    "fmt"
    "strings"

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

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

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

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

    // Create a collection for storing Person documents
    err := arangoClient.CreateCollection(context.Background(), "_system", "people", false)
    if err != nil {
        app.Logger().Errorf("error creating collection")
    }

    // CRUD routes
    app.POST("/people", Insert)
    app.GET("/people/{id}", Get)
    app.PUT("/people/{id}", Update)
    app.DELETE("/people/{id}", Delete)

    app.Run()
}

// Insert creates a new person document in the ArangoDB collection
func Insert(ctx *gofr.Context) (interface{}, error) {
    var p Person
    err := ctx.Bind(&p)
    if err != nil {
        return nil, err
    }

    res, err := ctx.Arango.CreateDocument(ctx, "_system", "people", p)
    if err != nil {
        return nil, err
    }

    return res, nil
}

// Get retrieves a person document by ID
func Get(ctx *gofr.Context) (interface{}, error) {
    id := strings.TrimPrefix(ctx.PathParam("id"), "id=")

    var result Person
    err := ctx.Arango.GetDocument(ctx, "_system", "people", id, &result)
    if err != nil {
        return nil, err
    }

    return result, nil
}

// Update modifies an existing person document
func Update(ctx *gofr.Context) (interface{}, error) {
    id := strings.TrimPrefix(ctx.PathParam("id"), "id=")

    var p Person
    err := ctx.Bind(&p)
    if err != nil {
        return nil, err
    }

    err = ctx.Arango.UpdateDocument(ctx, "_system", "people", id, p)
    if err != nil {
        return nil, err
    }

    return fmt.Sprintf("Document with id %s updated", id), nil
}

// Delete removes a person document by ID
func Delete(ctx *gofr.Context) (interface{}, error) {
    id := strings.TrimPrefix(ctx.PathParam("id"), "id=")

    err := ctx.Arango.DeleteDocument(ctx, "_system", "people", id)
    if err != nil {
        return nil, err
    }

    return fmt.Sprintf("Document with id %s deleted", id), 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!

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
1 participant