55 lines
1.4 KiB
Go
55 lines
1.4 KiB
Go
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,
|
|
}
|
|
|
|
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, "")
|
|
driver, err := neo4j.NewDriverWithContext(c.serviceConf.URI, auth, func(c *n4Cfg.Config) {})
|
|
if err != nil {
|
|
return nil, fmt.Errorf("neo4j driver initialization error: %w", err)
|
|
}
|
|
defer func(driver neo4j.DriverWithContext, ctx context.Context) {
|
|
err := driver.Close(ctx)
|
|
if err != nil {
|
|
// fmt
|
|
|
|
}
|
|
}(driver, context.Background())
|
|
|
|
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
|
|
}
|