neox

package module
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Dec 13, 2018 License: MIT Imports: 3 Imported by: 0

README

Neox

Neo4j driver utility extensions

Examples:

// Instead of

for result.Next() {
    r := result.Record()

    value, ok := r.GetByIndex(0).(float64)
    if !ok {
        return errors.New("that was not ok")
    }
    name, ok  := r.GetByIndex(1).(string)
    if !ok {
        return errors.New("that was not ok")
    }
    isActive, ok := r.GetByIndex(2).(bool)
    if !ok {
        return errors.New("that was not ok")
    }

    user := User {
        Value: value,
        Name: name,
        isActive: isActive,
    }
}

// Use type specfic access methods
for result.Next() {
    r := result.Recordx()

    value, ok := r.GetInt("value_key")
    if !ok {
        return errors.New("apparently value_key was not an integer")
    }

    name, _ := r.GetString("username")
    isActive, _ := r.GetBool("user_active")

    user := User {
        Value: value,
        Name: name,
        isActive: isActive,
    }
}

// Or pass a struct that is using the proper db field tags

//For Example:
type User struct {
    Value int `db:"total_value"`
    Name string `db:"username"`
    IsActive bool `db:"available"`
}

// Run a match query using alias's that match the tag names in the target struct
result, _ := session.Run(`
    match(n) return n.value as total_value, n.name as username, n.active as available`, nil)

for result.Next() {
    var user User
    err := result.ToStruct(&user)
    if err != nil {
        log.Fatal("that didn't work out")
    }
}

Documentation

Overview

Package neox is a package that wraps and extends the official neo4j bolt driver with useful utilites

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrInvalidArg is returned when provided arguments are invalid
	ErrInvalidArg = errors.New("the provided destination is not a pointer to a struct")
)

Functions

This section is empty.

Types

type Args

type Args map[string]interface{}

Args can be used to pass named arguments to a cypher query

type Driver

type Driver struct {
	neo4j.Driver
}

Driver is a wrapper around the neo4j representation of connection pool(s) to a neo4j server or cluster. It's safe for concurrent use.

func NewDriver

func NewDriver(target string, auth neo4j.AuthToken, configurers ...func(*neo4j.Config)) (*Driver, error)

NewDriver tries to construct an instance of a neox.Driver, returning a non nil error if something went wrong

func (*Driver) Sessionx

func (d *Driver) Sessionx(accessMode neo4j.AccessMode, bookmarks ...string) (*Session, error)

Sessionx is an extension method that returns an instance of a neox.Session

type Record

type Record struct {
	neo4j.Record
}

Record wraps the standard implementation of a neo4j.Record adding some useful utlities

func (*Record) GetBool

func (r *Record) GetBool(key string) (value bool, ok bool)

GetBool attempts to retrieve a rune value for the provided key If the provided key does not exist, or the value is not a rune, the method returns the zero value and false

func (*Record) GetBoolAtIndex

func (r *Record) GetBoolAtIndex(index int) (value bool, ok bool)

GetBoolAtIndex retrieves the value for the record at the provided index asserting it as a bool, returning the value and boolean indicating whether the type was asserted correctly

func (*Record) GetFloat

func (r *Record) GetFloat(key string) (value float64, ok bool)

GetFloat attempts to retrieve a float value for the provided key If the provided key does not exist, or the value is not a float, the method returns the zero value and false

func (*Record) GetFloatAtIndex

func (r *Record) GetFloatAtIndex(index int) (value float64, ok bool)

GetFloatAtIndex retrieves the value for the record at the provided index asserting it as a float, returning the value and boolean indicating whether the type was asserted correctly

func (*Record) GetInt

func (r *Record) GetInt(key string) (value int, ok bool)

GetInt attempts to retrieve an integer value for the provided key If the provided key does not exist, or the value is not an integer, the method returns the zero value and false

func (*Record) GetIntAtIndex

func (r *Record) GetIntAtIndex(index int) (value int, ok bool)

GetIntAtIndex retrieves the value for the record at the provided index asserting it as an integer, returning the value and boolean indicating whether the type was asserted to be an integer

func (*Record) GetString

func (r *Record) GetString(key string) (value string, ok bool)

GetString attempts to retrieve a string value for the provided key If the provided key does not exist, or the value is not a string, the method returns the zero value and false

func (*Record) GetStringAtIndex

func (r *Record) GetStringAtIndex(index int) (value string, ok bool)

GetStringAtIndex retrieves the value for the record at the provided index asserting it as a string, returning the value and boolean indicating whether the type was asserted correctly

type Result

type Result struct {
	neo4j.Result
	// contains filtered or unexported fields
}

A Result is returned from successful calls to a Session.Runx, it exposes all of the standard driver interface methods as well as its various extensions

func (*Result) Recordx

func (r *Result) Recordx() *Record

Recordx returns a neox.Record at the current index in the the result stream

func (*Result) ToStruct

func (r *Result) ToStruct(dest interface{}) error

ToStruct attempts to assign the values of the current result record to fields of the provided struct. The argument must be a pointer to a struct or an ErrInvalidArg will be returned. ToStruct will cache results of reflecting on the provided destination type to improve performance on every subsequent call for an instance of a Result. That said, using varying struct types through the lifetime of a single result instance should be considered unsafe and will yield unstable results

type Session

type Session struct {
	neo4j.Session
}

Session is a struct that offers access to the standard neo4j driver, but offers extension methods for running cypher queries and handling neo4j results

func (*Session) Runx

func (s *Session) Runx(cypher string, args Args, configurers ...func(*neo4j.TransactionConfig)) (*Result, error)

Runx is an extension method that runs the provided cypher query with the respective args and configurers and returns a neox.Result

Jump to

Keyboard shortcuts

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