2024-09-02 13:43:03 +00:00
|
|
|
package neo4j
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/neo4j/neo4j-go-driver/v5/neo4j"
|
|
|
|
n4Cfg "github.com/neo4j/neo4j-go-driver/v5/neo4j/config"
|
|
|
|
)
|
|
|
|
|
|
|
|
// NewNeo4J initializes a Neo4jInit using the provided Config and options.
|
|
|
|
// If opts is not provided, it will initialize Neo4jInit with default configuration.
|
|
|
|
func NewNeo4J(conf *Config, opts ...Option) *Client {
|
|
|
|
driverConfig := &n4Cfg.Config{
|
|
|
|
MaxConnectionLifetime: conf.MaxConnectionLifetime,
|
|
|
|
MaxConnectionPoolSize: conf.MaxConnectionPoolSize,
|
|
|
|
ConnectionAcquisitionTimeout: conf.ConnectionTimeout,
|
|
|
|
}
|
|
|
|
|
|
|
|
neo4ji := &Client{
|
|
|
|
neo4jConf: driverConfig,
|
2024-09-03 01:17:50 +00:00
|
|
|
serviceConf: Config{
|
|
|
|
URI: conf.URI,
|
|
|
|
Username: conf.Username,
|
|
|
|
Password: conf.Password,
|
|
|
|
LogLevel: conf.LogLevel,
|
|
|
|
},
|
2024-09-02 13:43:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, opt := range opts {
|
|
|
|
opt(neo4ji)
|
|
|
|
}
|
|
|
|
|
|
|
|
return neo4ji
|
|
|
|
}
|
|
|
|
|
|
|
|
// Conn initiates connection to the database and returns a Neo4j driver instance.
|
|
|
|
func (c *Client) Conn() (neo4j.DriverWithContext, error) {
|
|
|
|
auth := neo4j.BasicAuth(c.serviceConf.Username, c.serviceConf.Password, "")
|
2024-09-03 09:47:34 +00:00
|
|
|
driver, err := neo4j.NewDriverWithContext(c.serviceConf.URI, auth, func(_ *n4Cfg.Config) {})
|
2024-09-02 13:43:03 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("neo4j driver initialization error: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx := context.Background()
|
|
|
|
// Verify the connection to Neo4j.
|
|
|
|
err = driver.VerifyConnectivity(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("neo4j connectivity verification error: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return driver, nil
|
|
|
|
}
|