我正在为其中一个微服务的实时项目设置 Go with Neo4j
我浏览了有关设置相同的文档,但它没有显示执行相同操作的最佳实践(特别是全局并在整个应用程序中传递会话实例)
这就是我正在做的设置,想知道这是否是正确的方法:
// app.go
import ""github.com/neo4j/neo4j-go-driver/neo4j""
type App struct {
Router *mux.Router
DB *sqlx.DB
Neo4j neo4j.Session // setting neo4j session globally for injection
}
// =============================
// Neo4j initialization
// =============================
driver, err2 := neo4j.NewDriver(
neo4jConfig.connstring,
neo4j.BasicAuth(neo4jConfig.username, neo4jConfig.password, ""),
func(c *neo4j.Config){
c.Encrypted = false
},
)
checkForErrors(err2, "Cannot connect to NEO4J")
defer driver.Close()
session, err3 := driver.NewSession(neo4j.SessionConfig{})
a.Neo4j = session // 👈 assigning the session instance
现在,这将作为依赖项注入到repo正在执行查询的包中
DIEA
相关分类