This repository has been archived on 2024-07-22. You can view files and clone it, but cannot push or open issues or pull requests.
plug-sdk/internal/database/graph/connect.go

48 lines
1.2 KiB
Go
Raw Normal View History

2024-02-20 10:26:39 +00:00
package graph
import (
"context"
"fmt"
"strings"
"git.dragse.it/anthrove/otter-space-sdk/pkg/graph"
"git.dragse.it/anthrove/plug-template/config"
log "github.com/sirupsen/logrus"
)
var cfg config.OtterSpaceConfig
func init() {
localCfg, err := config.LoadConfig[config.OtterSpaceConfig](cfg)
if err != nil {
log.Panic(err)
}
cfg = localCfg
}
// ConnectToGraphDatabase establishes a connection to a graph database specified in the config.
// Currently, it only supports Neo4j. If the database type is not supported, it returns an error.
func ConnectToGraphDatabase(ctx context.Context) (graph.OtterSpace, error) {
var connection graph.OtterSpace
var err error
switch strings.ToLower(cfg.Type) {
case "neo4j":
connection = graph.NewGraphConnection(cfg.Debug)
err = connection.Connect(ctx, cfg.Endpoint, cfg.Username, cfg.Password)
if err != nil {
return nil, fmt.Errorf("graph: failed to connect to Neo4j: %w", err)
}
default:
return nil, fmt.Errorf("graph: unsupported database type: %s", cfg.Type)
}
log.WithFields(log.Fields{
"graph_type": cfg.Type,
"graph_endpoint": cfg.Endpoint,
},
).Info("graph: successfully connected")
return connection, nil
}