neo4j

package
v0.10.2 Latest Latest
Warning

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

Go to latest
Published: Oct 6, 2026 License: MIT Imports: 25 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package neo4j is a database/sql driver for Neo4j, which runs Cypher over the Query API of HTTP. It registers the one name "neo4j" (D60), and takes a DSN of the form neo4j://user:pass@host:7474/database?key=value (D61).

db, err := sql.Open("neo4j", "neo4j://neo4j:pass@localhost:7474/neo4j")

The driver speaks HTTP to port 7474, or HTTPS to port 7473 with tls=true. A URL that the tools of Neo4j write, such as neo4j://host:7687, names the port of Bolt, which this driver does not speak.

The server binds each argument: sql.Named("k", v) fills $k, and a positional argument n fills $n, such as $1 (D64). Each value keeps its type in typed JSON (D62 and D63). A transaction is an explicit transaction of the Query API (D65). The server does not stop a query when the client disconnects, so the key cancel says how the driver stops one when its context ends (D67). docs/NEO4J.md holds what the driver knows about the server.

Index

Constants

View Source
const (
	// AuthBasic sends the user and the password with basic authentication.
	// It is the default.
	AuthBasic = dbimp.AuthBasic
	// AuthBearer sends the password as a Bearer token, and no user. Neo4j
	// takes one only from an identity provider of single sign on, and
	// refuses the scheme without one (measured).
	AuthBearer = dbimp.AuthBearer
)

The values of the key auth, which say how the driver sends the password (D94 and D110).

View Source
const (
	// CancelTag ends each statement with a comment that names the
	// connection, and stops the statement with TERMINATE TRANSACTION. It is
	// the default.
	CancelTag = "tag"
	// CancelMetadata names the connection in txMetadata, and uses the tag on
	// a release older than 2026.04, which refuses txMetadata.
	CancelMetadata = "metadata"
	// CancelNone sends nothing, and the query runs on until it ends.
	CancelNone = "none"
)

The values of the key cancel, which say how the driver stops a query on the server when its context ends (D67 and D95).

View Source
const Name = "neo4j"

Name is the name that the driver registers with database/sql, and the scheme of its DSN.

Variables

This section is empty.

Functions

func WithOptions added in v0.6.0

func WithOptions(ctx context.Context, opts ...Option) context.Context

WithOptions returns a context that carries opts. Each statement and each transaction started with the context applies them after the options of the DSN.

Types

type Config

type Config struct {
	// Host is the host of the HTTP interface.
	Host string
	// Port is the port of the HTTP interface, 7474 by default, or 7473 with
	// TLS.
	Port int
	// TLS is true to speak HTTPS.
	TLS bool
	// User and Password are the credentials, sent as Auth says. With no user
	// and no password, the driver sends no credentials, for a server that
	// runs with authentication off.
	User     string
	Password string
	// Auth is AuthBasic or AuthBearer (D94 and D110).
	Auth string
	// Database is the database that each statement runs in, "neo4j" by
	// default.
	Database string
	// Cancel says how the driver stops a query when its context ends:
	// CancelTag, CancelMetadata or CancelNone (D67).
	Cancel string
}

Config is the configuration of a connector, which the DSN holds. The caller owns it (D7).

func ParseDSN

func ParseDSN(dsn string) (*Config, error)

ParseDSN parses a DSN of the form neo4j://user:pass@host:port/database?key=value (D27, D35 and D61).

func (*Config) FormatDSN

func (cfg *Config) FormatDSN() string

FormatDSN returns the DSN of cfg, which ParseDSN reads back as cfg.

type Connector

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

Connector opens connections to one server. It owns its transport, and every connection shares it. A caller can build one from a Config and open it with sql.OpenDB.

func NewConnector

func NewConnector(cfg Config) *Connector

NewConnector returns a Connector for cfg. The connector keeps a copy of cfg.

func (*Connector) Close

func (c *Connector) Close() error

Close closes the idle connections of the transport. database/sql calls it when the database closes.

func (*Connector) Connect

func (c *Connector) Connect(context.Context) (driver.Conn, error)

Connect satisfies driver.Connector. A connection holds no state on the server, except a transaction while one is open. Each connection has a random id, which names its statements for cancel (D67).

func (*Connector) Driver

func (c *Connector) Driver() driver.Driver

Driver satisfies driver.Connector.

type Driver

type Driver struct{}

Driver is the database/sql driver for Neo4j.

func (Driver) Open

func (Driver) Open(string) (driver.Conn, error)

Open satisfies driver.Driver. database/sql opens a connection through OpenConnector, and Open returns an error, because opening a connection needs a context.

func (Driver) OpenConnector

func (Driver) OpenConnector(dsn string) (driver.Connector, error)

OpenConnector satisfies driver.DriverContext. It parses the DSN.

type Error

type Error struct {
	// Code is the code of the error.
	Code string `json:"code"`
	// Message is the message of the error.
	Message string `json:"message"`
}

Error is one error that Neo4j reported, such as Neo.ClientError.Statement.SyntaxError (D66).

func (Error) Error

func (err Error) Error() string

Error satisfies the error interface.

type Node

type Node struct {
	// ElementID is the element id of the node.
	ElementID string
	// Labels are the labels of the node.
	Labels []string
	// Props are the properties of the node, each a Go value of D63.
	Props map[string]any
}

Node is a node of the graph (D63).

type Option added in v0.6.0

type Option = dbimp.Option[options]

Option sets an option of one statement or one transaction (D109). An option comes from the DSN, then from the context through WithOptions, then from an argument of the statement, and a later one wins. BeginTx takes no argument, so an option of a transaction comes from the DSN or through WithOptions only.

func WithCancel added in v0.6.0

func WithCancel(how string) Option

WithCancel sets how the driver stops the statement on the server when its context ends: CancelTag, CancelMetadata or CancelNone, as the key cancel of the DSN does (D67).

func WithDatabase added in v0.6.0

func WithDatabase(name string) Option

WithDatabase sets the database of the statement or the transaction, which the path of the request names. A statement of a transaction runs in the database of the transaction, so another database there fails with dbimp.ErrNotSupported.

func WithParameter added in v0.6.0

func WithParameter(name string, value any) Option

WithParameter sets any key of the body of the request by its name, such as "includeCounters" or "impersonatedUser". The value is encoded with json/v2. A key named here replaces one that the driver sets itself, as in Couchbase.

func WithReadonly added in v0.6.0

func WithReadonly(v bool) Option

WithReadonly makes the server refuse a statement that writes, with accessMode READ (measured). In a transaction, it applies to BeginTx.

func WithTimeout added in v0.6.0

func WithTimeout(d time.Duration) Option

WithTimeout sets the time that the server gives the statement, as maxExecutionTime, which counts whole seconds, so the driver rounds d up to the next second (measured by hand on 2026.09.0). The server stops a statement that runs longer, and the error arrives after its rows (recorded). 5.26.31 ignores maxExecutionTime (recorded), so on a release older than 2026.04 the statement fails with dbimp.ErrNotSupported (D109).

type Path

type Path struct {
	Nodes         []Node
	Relationships []Relationship
}

Path is a path of the graph (D63): its nodes, and the relationship between each node and the next.

type Point

type Point struct {
	SRID    int
	X, Y, Z float64
	Dims    int
}

Point is a point of Neo4j (D63), in the coordinate system that SRID names, such as 7203 for a cartesian point or 4326 for WGS 84. Dims is 2 or 3, and Z is zero for a point of two dimensions.

func (Point) String

func (p Point) String() string

String returns the point in the form of Neo4j, such as SRID=7203;POINT (1.5 2.5).

type Relationship

type Relationship struct {
	// ElementID is the element id of the relationship.
	ElementID string
	// StartElementID and EndElementID are the element ids of its nodes.
	StartElementID string
	EndElementID   string
	// Type is the type of the relationship.
	Type string
	// Props are the properties of the relationship, each a Go value of D63.
	Props map[string]any
}

Relationship is a relationship of the graph (D63).

type ResponseError

type ResponseError struct {
	// HTTPStatus is the status code of the response.
	HTTPStatus int
	// Errs are the errors, in the order that the server sent them.
	Errs []Error
}

ResponseError holds the errors of one response, with its HTTP status. The status is 202 for an error that follows some rows, and 4xx or 5xx for a request that the server refused, such as 500 for TransactionStartFailed on 5.26.31. Use errors.As to read an Error of it.

func (*ResponseError) Error

func (err *ResponseError) Error() string

Error satisfies the error interface.

func (*ResponseError) Unwrap

func (err *ResponseError) Unwrap() []error

Unwrap returns each Error, for errors.As and errors.Is.

Jump to

Keyboard shortcuts

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