whiterabbit

package module
v0.0.8 Latest Latest
Warning

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

Go to latest
Published: Apr 9, 2021 License: MIT Imports: 7 Imported by: 1

README

whiterabbit

Small library to map go struct with neo4j entities.

Connection to neo4j

first step is to open the database

db, err := whiterabbit.Open(config)
  • where config is any struct that satisfy the Config interface
  • close the database db.Close()

Getting a connection

a connection, ie a session in neo4j world can be obtain:

con, err := db.GetConnection()

sessions are meant to be short-live in neo4j world, so they shall be closed as soon as possible con.Close()

creating a node

  • nodes create have the name of the struct as label
  • every exported field of the struct is an attribute of the node

For example:

type User struct {
    Name     string
    Age      int
    password string
}
user := User{Name: "first", Age: 10}
con.CreateNode(user)

will create a Node labelled User with 2 attributes, Name and Age. password is ignored

fetching nodes

type User struct {
    whiterabbit.Model
    Name     string
    Age      int
    password string
}
con.FindAllNodes(User{})
con.FindNodesClause(User{}, map[string]interface{}{"Name": "user", "Age": 10, }, whiterabbit.StartsWith)

If the struct contains whiterabbit.Model, all node's attributes that are not matching an exported field of the struct, will be copy in whiterabbit.Model see

converting nodes

neo4j.Node can be converted to struct:

var node neo4j.Node
type User struct {
    whiterabbit.Model
    Name     string
    Age      int
    password string
}
ret, _ := ConvertNode(node, User{}))
u = ret.(User)
  • if present whiterabbit.Model will be initialized, see
  • ConvertNode has a variadic third argument representing all struct candidate for the node
white rabbit model

adding whiterabbit.Model to struct allows:

  • get neo4j node IDs
  • retrieve nodes' attributes not declared in struct
type Model struct {
	ID     int64             // neo4j node or relationship ID
	Labels map[string]string // any label not defined mapping struct
}

relations

relations, err := con.MatchRelation("is_a", Ingredient{}, Category{})

transactions

using Connection.InTransaction(f) where f is

func(con *Connection) ([]neo4j.Result, error)

transaction is rollbacked if f returns an error

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ConvertNode

func ConvertNode(node neo4j.Node, candidate interface{}, otherCandidates ...interface{}) (interface{}, error)

ConvertNode converts neo4j node into one of the candidate struct

Types

type Config

type Config interface {
	GetHost() string
	GetPort() int
	GetUser() string
	GetPassword() string
	GetEncrypted() bool
}

Config ...

type Connection

type Connection struct {
	// contains filtered or unexported fields
}

func (*Connection) Close

func (s *Connection) Close()

func (*Connection) CreateNode

func (con *Connection) CreateNode(value interface{}) (int64, neo4j.Result, error)

CreateNode ... returns: - id of the created node - neo4j.Result to be consumed when in a transaction - error

func (*Connection) DeleteByID added in v0.0.7

func (con *Connection) DeleteByID(id int64) error

DeleteByID delete any node by ID

func (*Connection) DeleteNode added in v0.0.6

func (con *Connection) DeleteNode(node interface{}) error

DeleteNode ...

func (*Connection) Execute

func (con *Connection) Execute(cypher string, params map[string]interface{}) error

Execute a cypher

func (*Connection) FindAllNodes

func (con *Connection) FindAllNodes(nodeType interface{}) ([]interface{}, error)

FindAllNodes finds all nodes of a given type

func (*Connection) FindById added in v0.0.8

func (con *Connection) FindById(id int64, candidate interface{}) (interface{}, error)

func (*Connection) FindByProperty added in v0.0.4

func (con *Connection) FindByProperty(property string, value string, candidate []interface{}) ([]interface{}, error)

FindByProperty find all node with given property containg value

func (*Connection) FindNodesClause

func (con *Connection) FindNodesClause(nodeType interface{}, where map[string]interface{}, mode SearchMode) ([]interface{}, error)

FindNodesClause finds all nodes of a given type searchMode is applied for all string

func (*Connection) GetSession

func (con *Connection) GetSession() neo4j.Session

func (*Connection) InTransaction

func (con *Connection) InTransaction(f func(con *Connection) ([]neo4j.Result, error)) error

InTransaction ... execute given function in a transaction

func (*Connection) MatchRelation

func (con *Connection) MatchRelation(name string, candidate interface{}, otherCandidate ...interface{}) ([]Relation, error)

MatchRelation ...

func (*Connection) RelationByNodeID added in v0.0.4

func (con *Connection) RelationByNodeID(id int64, candidate interface{}, otherCandidate ...interface{}) ([]Relation, error)

RelationByNodeID return all relations for a given node id

func (*Connection) SetSession

func (con *Connection) SetSession(neoSession neo4j.Session)

func (*Connection) SetUniqueConstraint added in v0.0.4

func (con *Connection) SetUniqueConstraint(label interface{}, field string, constraintName string) error

SetUniqueConstraint ...

type DB

type DB struct {
	// contains filtered or unexported fields
}

func Open

func Open(cfg Config) (*DB, error)

Open a connection to neo4j

func (*DB) Close

func (db *DB) Close() error

Close ...

func (*DB) GetConnection

func (db *DB) GetConnection() (Connection, error)

GetConnection open and return a neo4j session

type DefaultConfig

type DefaultConfig struct {
	// contains filtered or unexported fields
}

DefaultConfig for testing

func (DefaultConfig) GetEncrypted

func (df DefaultConfig) GetEncrypted() bool

GetEncrypted ...

func (DefaultConfig) GetHost

func (df DefaultConfig) GetHost() string

GetHost ...

func (DefaultConfig) GetPassword

func (df DefaultConfig) GetPassword() string

GetPassword ...

func (DefaultConfig) GetPort

func (df DefaultConfig) GetPort() int

GetPort ...

func (DefaultConfig) GetUser

func (df DefaultConfig) GetUser() string

GetUser ...

type Model

type Model struct {
	ID     int64             `json:"id"`                  // neo4j node or relationship ID
	Labels map[string]string `json:"attribute,omitempty"` // any label not defined mapping struct
}

Model go struct to add neo4j specific fields

func (Model) GetId added in v0.0.5

func (m Model) GetId() int64

GetId ...

func (*Model) SetId added in v0.0.4

func (m *Model) SetId(id int64)

SetId ...

type Relation

type Relation struct {
	Relation string
	From     interface{}
	To       interface{}
}

Relation ...

type SearchMode

type SearchMode int

SearchMode operation used for `WHERE` clauses

const (
	Exact SearchMode = 1 + iota
	StartsWith
	Contains
	EndsWith
	Regexp
	IgnoreCase
)

Directories

Path Synopsis
internal

Jump to

Keyboard shortcuts

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