neo4j/neo4j-go-driver là dự án Backend trên GitHub, viết chủ yếu bằng Go, với 555 sao. Neo4j Bolt Driver for Go
Tóm tắt dựng từ metadata GitHub của chính dự án — chưa có bài review TopGit. Trang sẽ tự động cập nhật khi bài review đầy đủ được xuất bản.
VÌ SAO CHƯA CÓ REVIEW
TopGit viết bài đầy đủ cho repo có nhiều sao nhất và được yêu cầu nhiều nhất. Trang này là snapshot trong thời gian chờ — xem README gốc ở tab READ ME.
The Go driver only works with actively maintained Go versions.
Read Go official release policy to learn more.
Module version
Make sure your application has been set up to use go modules (there should be a go.mod file in your application root).
6.x
Add the driver with:
go get github.com/neo4j/neo4j-go-driver/v6
5.x
Add the driver with:
go get github.com/neo4j/neo4j-go-driver/v5
4.x
Add the driver with:
go get github.com/neo4j/neo4j-go-driver/v4
1.x
For versions 1.x of the driver (notice the absence of /v4, /v5 or /v6), run instead the following:
go get github.com/neo4j/neo4j-go-driver
Documentation
Drivers manual that describes general driver concepts in depth here.
Go package API documentation here.
Preview Features
The preview feature is a new feature that is a candidate for a future GA
status.
It enables users to try the feature out and maintainers to refine and update it.
The preview features are not considered to be experimental, temporary or unstable.
However, they may change more rapidly, without following the usual deprecation cycle.
Most preview features are expected to be granted the GA status unless some unexpected conditions arise.
Due to the increased flexibility of the preview status, user feedback is encouraged so that it can be considered before
the GA status.
Every preview feature gets a
dedicated GitHub Discussion
where users can share their initial impressions and thoughts.
Migrating from previous versions
See migration guide for information on how to migrate
from previous versions of the driver to the current one.
Minimum Viable Snippet
Connect, execute a statement and handle results
Make sure to use the configuration in the code that matches the version of Neo4j server that you run.
package main
import (
"context"
"fmt"
"github.com/neo4j/neo4j-go-driver/v6/neo4j"
)
func main() {
dbUri := "neo4j://localhost" // scheme://host(:port) (default port is 7687)
driver, err := neo4j.NewDriver(dbUri, neo4j.BasicAuth("neo4j", "letmein!", ""))
if err != nil {
panic(err)
}
// Starting with 5.0, you can control the execution of most driver APIs
// To keep things simple, we create here a never-cancelling context
// Read https://pkg.go.dev/context to learn more about contexts
ctx := context.Background()
// Handle driver lifetime based on your application lifetime requirements.
// driver's lifetime is usually bound by the application lifetime, which usually implies one driver instance per
// application
defer driver.Close(ctx) // Make sure to handle errors during deferred calls
item, err := insertItem(ctx, driver)
if err != nil {
panic(err)
}
fmt.Printf("%v\n", item)
}
func insertItem(ctx context.Context, driver neo4j.Driver) (*Item, error) {
result, err := neo4j.ExecuteQuery(ctx, driver,
"CREATE (n:Item { id: $id, name: $name }) RETURN n",
map[string]any{
"id": 1,
"name": "Item 1",
}, neo4j.EagerResultTransformer)
if err != nil {
return nil, err
}
itemNode, _, err := neo4j.GetRecordValue[neo4j.Node](result.Records[0], "n")
if err != nil {
return nil, fmt.Errorf("could not find node n")
}
id, err := neo4j.GetProperty[int64](itemNode, "id")
if err != nil {
return nil, err
}
name, err := neo4j.GetProperty[string](itemNode, "name")
if err != nil {
return nil, err
}
return &Item{Id: id, Name: name}, nil
}
type Item struct {
Id int64
Name string
}
func (i *Item) String() string {
return fmt.Sprintf("Item (id: %d, name: %q)", i.Id, i.Name)
}
Server/Driver compatibility
As a policy, patch versions will not be released except on rare occasions.
Bug fixes and updates will go into the latest minor version and users should upgrade to that.
Driver upgrades within a major version will never contain breaking API changes.
See also: https://neo4j.com/developer/kb/neo4j-supported-versions/
Connecting to a causal cluster
You just need to use neo4j as the URL scheme and set host of the URL to one of your core members of the cluster.
There are a few points that need to be highlighted:
Each Driver instance maintains a pool of connections inside, as a result, it is recommended to only use one driver per application.
It is considerably cheap to create new sessions and transactions, as sessions and transactions do not create new connections as long as there are free connections available in the connection pool.
The driver is thread-safe, while the session or the transaction is not thread-safe.
Parsing Result Values
Record Stream
A cypher execution result is comprised of a stream of records followed by a result summary.
The records inside the result can be accessed via Next()/Record() functions defined on Result. It is important to check Err() after Next() returning false to find out whether it is the end of the result stream or an error that caused the end of result consumption.
// Next returns false upon error
for result.Next(ctx) {
record := result.Record()
handleRecord(record)
}
// Err returns the error that caused Next to return false
if err = result.Err(); err != nil {
handleError(err)
}
Accessing Values in a Record
Values in a Record can be accessed either by index or by alias. The return value is an any which means you need
to convert the interface to the expected type
value := record.Values[0]
if value, ok := record.Get('field_name'); ok {
// a value with alias field_name was found
// process value
}
Value Types
The driver exposes values in the record as an any type.
The underlying types of the returned values depend on the corresponding Cypher types.
The mapping between Cypher types and the types used by this driver (to represent the Cypher type):
Cypher Type
Driver Type
null
nil
List
[]any
Map
map[string]any
Boolean
bool
Integer
int64
Float
float
String
string
ByteArray
[]byte
Node
neo4j.Node
Relationship
neo4j.Relationship
Path
neo4j.Path
Vector
neo4j.Vector[T]
Spatial Types - Point
Cypher Type
Driver Type
Point
neo4j.Point2D
Point
neo4j.Point3D
The temporal types are introduced in Neo4j 3.4 series.
You can create a 2-dimensional Point value using;
point := neo4j.Point2D {X: 1.0, Y: 2.0, SpatialRefId: srId }
For a list of supported srId values, please refer to the docs here.
Temporal Types - Date and Time
The temporal types are introduced in Neo4j 3.4 series. Given the fact that database supports a range of different temporal types, most of them are backed by custom types defined at the driver level.
The mapping among the Cypher temporal types and actual exposed types are as follows:
Cypher Type
Driver Type
Date
neo4j.Date
Time
neo4j.OffsetTime
LocalTime
neo4j.LocalTime
DateTime
time.Time
LocalDateTime
neo4j.LocalDateTime
Duration
neo4j.Duration
Receiving a temporal value as driver type:
dateValue := record.Values[0].(neo4j.Date)
All custom temporal types can be constructed from a time.Time value using <Type>Of() (DateOf, OffsetTimeOf, ...) functions.
Converting a custom temporal value into time.Time (all neo4j temporal types expose Time() function to gets its corresponding time.Time value):
dateValueAsTime := dateValue.Time()
Note:
When neo4j.OffsetTime is converted into time.Time or constructed through OffsetTimeOf(time.Time), its Location is given a fixed name of Offset (i.e. assigned time.FixedZone("Offset", offsetTime.offset)).
When time.Time values are sent through the driver, the value is stored with its zone name if Location() names a zone that resolves to the value's offset at that instant, and with its offset otherwise.
Vector Types
Vector types are introduced in Bolt 6.0 and represent an ordered collection of homogeneous numeric values.
The mapping between Cypher Vector types and the types used by this driver:
// Using GetRecordValue to extract vector from a record
recordVec, _, err := neo4j.GetRecordValue[neo4j.Vector[float64]](record, "vec")
// Using GetProperty to extract vector from a node or relationship
node := record.Values[0].(neo4j.Node)
propVec, err := neo4j.GetProperty[neo4j.Vector[float64]](node, "vec")
Logging
Logging at the driver level can be configured by setting Log field of config.Config through configuration functions that can be passed to neo4j.NewDriver function.
Console Logger
For simplicity, we provide a predefined console logger which can be constructed by log.ToConsole function. To enable console logger, you need to specify which level you need to enable (log.ERROR, log.WARNING, log.INFO and log.DEBUG which are ordered by the level of detail).
A simple code snippet that will enable console logging is as follows;
The Log field of the config.Config struct is defined to be of interface log.Logger which has the following definition:
type Logger interface {
Error(name string, id string, err error)
Warnf(name string, id string, msg string, args ...any)
Infof(name string, id string, msg string, args ...any)
Debugf(name string, id string, msg string, args ...any)
}
For a customised logging target, you can implement the above interface and pass an instance of that implementation to the Log field.
Bolt tracing
Logging Bolt messages is done at the session level and is configured independently of the logging configuration described above.
This is disabled by default.
Console Bolt logger
For simplicity, we provide a predefined console logger which can be constructed by log.BoltToConsole.
boltLogger := log.BoltToConsole()
// for ExecuteQuery
result, err := neo4j.ExecuteQuery(ctx, driver, query, params, transformer, neo4j.ExecuteQueryWithBoltLogger(boltLogger))
// for the regular session APIs (session.Run, session.BeginTransaction, session.ExecuteRead, session.ExecuteWrite)
session := driver.NewSession(ctx, neo4j.SessionConfig{BoltLogger: boltLogger})
Custom Bolt Logger
The BoltLogger field of the neo4j.SessionConfig struct is defined to be of interface log.BoltLogger which has the following definition:
neo4j/neo4j-go-driver thuộc nhóm Backend trên TopGit, cùng 4 topic GitHub. Trang Trending và Topics liệt kê các repo cùng số sao và cùng ngôn ngữ để so sánh.
Đọc thêm về neo4j/neo4j-go-driver ở đâu?
Trang TopGit này là một snapshot — tab "Readme" hiển thị nguyên văn README của repo (đã bỏ link, giữ ảnh). Repo GitHub ở github.com/neo4j/neo4j-go-driver là nguồn chính thức.
neo4j/neo4j-go-driver có bao nhiêu sao?
neo4j/neo4j-go-driver có 555 sao GitHub — tải lại trang để xem số mới nhất, hoặc xem trực tiếp github.com/neo4j/neo4j-go-driver. TopGit phản chiếu số sao của GitHub nhưng không cam kết đến từng phút.
neo4j/neo4j-go-driver có những chủ đề gì?
GitHub topics của neo4j/neo4j-go-driver: "database-driver", "driver", "golang", "neo4j". TopGit xếp repo vào nhóm Backend.
neo4j/neo4j-go-driver có phải mã nguồn mở không?
Có — neo4j/neo4j-go-driver phát hành theo license Apache-2.0, nghĩa là mã nguồn mở để đọc, fork và (tùy license) tái sử dụng. Mã: github.com/neo4j/neo4j-go-driver.
neo4j/neo4j-go-driver có website riêng không?
TopGit chưa ghi nhận URL trang chủ cho neo4j/neo4j-go-driver. Phần README ở tab phía trên thường có link demo, hoặc xem mô tả GitHub của repo.
neo4j/neo4j-go-driver còn đang phát triển không?
Commit gần nhất trên neo4j/neo4j-go-driver là 8 ngày trước (theo timestamp GitHub). Repo có 76 fork — một chỉ báo về mức độ quan tâm của cộng đồng.
Đọc đầy đủ README ở tab phía trên.
Muốn nghe thêm một ý kiến về neo4j-go-driver?
Hỏi một AI đọc được trang này — một cú bấm là có ngay nhận định về neo4j-go-driver.