Variety of refactors and optimizations.
This commit is contained in:
56
neo4j.go
56
neo4j.go
@@ -5,26 +5,6 @@ import (
|
||||
"github.com/neo4j/neo4j-go-driver/v6/neo4j"
|
||||
)
|
||||
|
||||
// Interface
|
||||
|
||||
type GraphDB interface {
|
||||
MergeSubgraph(ctx context.Context, subgraph *BatchSubgraph) ([]neo4j.ResultSummary, error)
|
||||
}
|
||||
|
||||
func NewGraphDriver(driver neo4j.Driver) GraphDB {
|
||||
return &graphdb{driver: driver}
|
||||
}
|
||||
|
||||
type graphdb struct {
|
||||
driver neo4j.Driver
|
||||
}
|
||||
|
||||
func (n *graphdb) MergeSubgraph(ctx context.Context, subgraph *BatchSubgraph) ([]neo4j.ResultSummary, error) {
|
||||
return MergeSubgraph(ctx, n.driver, subgraph)
|
||||
}
|
||||
|
||||
// Functions
|
||||
|
||||
func ConnectNeo4j(ctx context.Context, uri, user, password string) (neo4j.Driver, error) {
|
||||
driver, err := neo4j.NewDriver(
|
||||
uri,
|
||||
@@ -40,3 +20,39 @@ func ConnectNeo4j(ctx context.Context, uri, user, password string) (neo4j.Driver
|
||||
|
||||
return driver, nil
|
||||
}
|
||||
|
||||
// SetNeo4jSchema ensures that the necessary indexes and constraints exist in
|
||||
// the database
|
||||
func SetNeo4jSchema(ctx context.Context, driver neo4j.Driver) error {
|
||||
schemaQueries := []string{
|
||||
`CREATE CONSTRAINT user_pubkey IF NOT EXISTS
|
||||
FOR (n:User) REQUIRE n.pubkey IS UNIQUE`,
|
||||
|
||||
`CREATE INDEX user_pubkey IF NOT EXISTS
|
||||
FOR (n:User) ON (n.pubkey)`,
|
||||
|
||||
`CREATE INDEX event_id IF NOT EXISTS
|
||||
FOR (n:Event) ON (n.id)`,
|
||||
|
||||
`CREATE INDEX event_kind IF NOT EXISTS
|
||||
FOR (n:Event) ON (n.kind)`,
|
||||
|
||||
`CREATE INDEX tag_name_value IF NOT EXISTS
|
||||
FOR (n:Tag) ON (n.name, n.value)`,
|
||||
}
|
||||
|
||||
// Create indexes and constraints
|
||||
for _, query := range schemaQueries {
|
||||
_, err := neo4j.ExecuteQuery(ctx, driver,
|
||||
query,
|
||||
nil,
|
||||
neo4j.EagerResultTransformer,
|
||||
neo4j.ExecuteQueryWithDatabase("neo4j"))
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user