graph

package
v0.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 21, 2022 License: Apache-2.0 Imports: 7 Imported by: 5

Documentation

Index

Constants

View Source
const (
	ID      = "@id"
	Label   = "@label"
	Labels  = "@labels"
	Type    = "@type"
	StartID = "@startId"
	EndID   = "@endId"
)
View Source
const SystemDB = "system"

Variables

View Source
var ErrEmpty = errors.New("empty")

ErrEmpty indicates that a query returned no result.

View Source
var ErrMultiple = errors.New("multiple")

ErrMultiple indicates that a query returned more Records than expected.

Functions

func Auth

func Auth(cred string) (neo4j.AuthToken, string)

Auth parses the credentials and creates a new AuthToken. The credentials shall be prefixed with a colon (":") to designate the type. If it does not contain a valid authentication scheme, it falls back to Basic authentication. If the username can be derived from the credentials, it is returned together with the AuthToken.

func IsConnected

func IsConnected() bool

IsConnected returns whether the database connection is established.

Types

type Conn

type Conn struct {
	Driver neo4j.Driver

	DBName string
	Tx     neo4j.Transaction
	Params map[string]any
	// contains filtered or unexported fields
}

Conn represents a database connection, which can open multiple Sessions.

func GetConn

func GetConn() *Conn

GetConn returns the default connection, regardless of its connection state. It panics if there is no connection.

func NewConn

func NewConn(addr string, user string, auth neo4j.AuthToken, dbName string,
	opts ...func(config *neo4j.Config)) (*Conn, error)

NewConn creates a new Neo4j Driver and returns the new Conn.

func (*Conn) Close

func (c *Conn) Close() (err error)

Close the driver and all underlying connections.

func (*Conn) Commit

func (c *Conn) Commit() (done bool, err error)

Commit commits the current Transaction. If there is no active Transaction, false is returned.

func (*Conn) GetTransaction

func (c *Conn) GetTransaction() (tx neo4j.Transaction, created bool, err error)

GetTransaction returns the current Transaction or creates a new one.

func (*Conn) Rollback

func (c *Conn) Rollback() (done bool, err error)

Rollback rolls back the current Transaction. If there is no active Transaction, false is returned.

func (*Conn) Session

func (c *Conn) Session() neo4j.Session

Session creates a new Session.

func (*Conn) UseDB

func (c *Conn) UseDB(dbName string) (err error)

UseDB permanently changes the database.

func (*Conn) Username

func (c *Conn) Username() string

Username returns the username used to connect to the database. If an error occurs, an empty string is returned.

type Mapper

type Mapper[T any] func(rec *neo4j.Record) T

Mapper is used by Template for mapping results on a per-record basis. Implementations of this type perform the actual work of mapping each Record to a type, but don't need to worry about error handling. Errors will be handled by the calling Template.

func NewRawResultMapper

func NewRawResultMapper() Mapper[map[string]any]

NewRawResultMapper creates a new Mapper that extracts all columns to key-value pairs as they are returned.

func NewResultMapper

func NewResultMapper() Mapper[Result]

NewResultMapper creates a new Mapper that converts Records to Results. For each Node and Relationship, its properties are extracted into a map. Additionally, IDs, labels and types are added so that specific modifications can be applied by the caller.

func NewSingleValueMapper

func NewSingleValueMapper[T any](idx int) Mapper[T]

NewSingleValueMapper creates a new Mapper that converts a single column into a single result value per record. The type of the result value for each record can be specified. The value for the single column will be extracted from the Record and cast to the specified target type.

type Request

type Request struct {
	Query  string
	Params map[string]any
}

Request is a Cypher query and bind parameters.

func (Request) String

func (r Request) String() string

String returns the Cypher query.

type Result

type Result db.Record

Result is an alias for Record with additional convenience methods.

func (*Result) Add

func (r *Result) Add(key string, val any)

Add appends a new key-value pair to this Result.

func (*Result) Index

func (r *Result) Index(key string) int

Index returns the position of the given key.

func (*Result) Value

func (r *Result) Value(key string) (any, bool)

Value returns the value for the given key.

type Template

type Template[T any] struct {
	// contains filtered or unexported fields
}

Template simplifies the use of Neo4j and helps to avoid common errors. It executes core Neo4j workflow, leaving application code to provide Cypher and extract results. Template executes Cypher queries or updates, initiating iteration over Results and catching errors. Callers need only to implement callback functions, giving them a clearly defined contract. All Neo4j operations performed are logged at debug level, using the Logger.

func NewTemplate

func NewTemplate[T any](conn *Conn) *Template[T]

NewTemplate creates a new Template with the given connection.

func (Template[T]) Query

func (t Template[T]) Query(r Request, m Mapper[T]) (
	list []T, summary neo4j.ResultSummary, err error)

Query executes the given Cypher with list of parameters to bind to the query, mapping each record to a value via a RowMapper. If there is no Transaction on this Session, then an explicit transaction is started and committed afterwards.

func (Template[T]) QuerySingle

func (t Template[T]) QuerySingle(
	cyp string, params map[string]any, m Mapper[T]) (val T, err error)

QuerySingle is like Query, but maps exactly one result record to a value via a Mapper. If the query does not return exactly one record, an error is returned.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL