falkordb-go is a Golang client for the FalkorDB database.
Make sure to initialize a Go module:
go mod init github.com/my/repo
Simply do:
$ go get github.com/FalkorDB/falkordb-go/v2The complete falkordb-go API is documented on GoDoc.
package main
import (
"fmt"
"log"
"github.com/FalkorDB/falkordb-go/v2"
)
func main() {
db, _ := falkordb.FalkorDBNew(&falkordb.ConnectionOption{Addr: "0.0.0.0:6379"})
// db, _ := falkordb.FalkorDBNewCluster(&falkordb.ConnectionClusterOption{Addrs: []string{"0.0.0.0:6379"}})
graph := db.SelectGraph("social")
query := "CREATE (:Person {name: 'John Doe', age: 33, gender: 'male', status: 'single'})-[:VISITED]->(:Country {name: 'Japan'})"
_, err := graph.Query(query, nil, nil)
if err != nil {
log.Fatal(err)
}
query = "MATCH (p:Person)-[v:VISITED]->(c:Country) RETURN p.name, p.age, c.name"
// result is a QueryResult struct containing the query's generated records and statistics.
result, err := graph.Query(query, nil, nil)
if err != nil {
log.Fatal(err)
}
// Pretty-print the full result set as a table.
result.PrettyPrint()
// Iterate over each individual Record in the result.
fmt.Println("Visited countries by person:")
for result.Next() { // Next returns true until the iterator is depleted.
// Get the current Record.
r := result.Record()
// Entries in the Record can be accessed by index or key.
pName := r.GetByIndex(0)
fmt.Printf("\nName: %s\n", pName)
pAge, _ := r.Get("p.age")
fmt.Printf("\nAge: %d\n", pAge)
}
// Path matching example.
query = "MATCH p = (:Person)-[:VISITED]->(:Country) RETURN p"
result, err = graph.Query(query, nil, nil)
if err != nil {
log.Fatal(err)
}
fmt.Println("Pathes of persons visiting countries:")
for result.Next() {
r := result.Record()
p, ok := r.GetByIndex(0).(falkordb.Path)
fmt.Printf("%s %v\n", p, ok)
}
}Running the above produces the output:
+----------+-------+--------+
| p.name | p.age | c.name |
+----------+-------+--------+
| John Doe | 33 | Japan |
+----------+-------+--------+
Query internal execution time 1.623063
Name: John Doe
Age: 33Queries can be run with a millisecond-level timeout as described in the documentation. To take advantage of this feature, the QueryOptions struct should be used:
options := NewQueryOptions().SetTimeout(10) // 10-millisecond timeout
res, err := graph.Query("MATCH (src {name: 'John Doe'})-[*]->(dest) RETURN dest", nil, options)FalkorDB supports User Defined Functions written in JavaScript. The falkordb-go client provides methods to manage UDF libraries:
db, _ := falkordb.FalkorDBNew(&falkordb.ConnectionOption{Addr: "0.0.0.0:6379"})
// Define a UDF library
library := "StringUtils"
source := `
function UpperCaseOdd(s) {
return s.split('').map((char, i) => (i % 2 !== 0 ? char.toUpperCase() : char)).join('');
}
falkor.register('UpperCaseOdd', UpperCaseOdd);
`
// Load the UDF library
err := db.UDFLoad(library, source)
// List all loaded UDF libraries
udfs, err := db.UDFList()
// Use the UDF in a query
graph := db.SelectGraph("demo")
result, _ := graph.Query("RETURN StringUtils.UpperCaseOdd('hello')", nil, nil)
// Delete a specific UDF library
err = db.UDFDelete(library)
// Or flush all UDF libraries
err = db.UDFFlush()For more information on UDFs, see the FalkorDB UDF documentation.
A simple test suite is provided, and can be run with:
$ go testThe tests expect a FalkorDB server to be available at localhost:6379
falkordb-go is distributed under the BSD3 license - see LICENSE