Skip to content

Latest commit

 

History

History
97 lines (72 loc) · 2.29 KB

README.md

File metadata and controls

97 lines (72 loc) · 2.29 KB
title keywords description
Neo4j
neo4j
database
Connecting to a Neo4j database.

Neo4j Example

Github StackBlitz

This project demonstrates how to connect to a Neo4j database in a Go application using the Fiber framework.

Prerequisites

Ensure you have the following installed:

Setup

  1. Clone the repository:

    git clone https://github.com/gofiber/recipes.git
    cd recipes/neo4j
  2. Install dependencies:

    go get
  3. Set up your Neo4j database and update the connection string in the code.

Running the Application

  1. Start the application:
    go run main.go

Example

Here is an example of how to connect to a Neo4j database in a Fiber application:

package main

import (
    "log"
    "github.com/gofiber/fiber/v2"
    "github.com/neo4j/neo4j-go-driver/v5/neo4j"
)

func main() {
    // Neo4j connection
    uri := "neo4j://localhost:7687"
    username := "neo4j"
    password := "password"
    driver, err := neo4j.NewDriver(uri, neo4j.BasicAuth(username, password, ""))
    if err != nil {
        log.Fatal(err)
    }
    defer driver.Close()

    // Fiber instance
    app := fiber.New()

    // Routes
    app.Get("/", func(c *fiber.Ctx) error {
        session := driver.NewSession(neo4j.SessionConfig{})
        defer session.Close()

        result, err := session.Run("RETURN 'Hello, World!'", nil)
        if err != nil {
            return err
        }

        if result.Next() {
            return c.SendString(result.Record().Values[0].(string))
        }

        return c.SendStatus(500)
    })

    // Start server
    log.Fatal(app.Listen(":3000"))
}

References