Refactor: 封装获取neo4j的连接函数

This commit is contained in:
2026-04-13 14:45:14 +08:00
parent 37df78e5d2
commit 6764095ed1
2 changed files with 7 additions and 6 deletions
+3 -4
View File
@@ -2,14 +2,13 @@ package neo4j
import (
"context"
"fmt"
"github.com/neo4j/neo4j-go-driver/v5/neo4j"
"knowledge-graph-backend/config"
)
func DoConnect(cfg config.Neo4jConfig) {
func NewDriver(cfg config.Neo4jConfig) neo4j.DriverWithContext {
ctx := context.Background()
driver, err := neo4j.NewDriverWithContext(
cfg.URI,
@@ -17,11 +16,11 @@ func DoConnect(cfg config.Neo4jConfig) {
if err != nil {
panic(err)
}
defer driver.Close(ctx)
err = driver.VerifyConnectivity(ctx)
if err != nil {
panic(err)
}
fmt.Println("Connection established.")
return driver
}