Neo4j.Driver (Neo4jEx v0.1.4)

View Source

Neo4j Driver for Elixir.

This module provides the main interface for connecting to and interacting with Neo4j databases. It handles connection management, authentication, and provides a high-level API for executing queries.

Usage

# Connect to Neo4j
{:ok, driver} = Neo4j.Driver.start_link("bolt://localhost:7687",
  auth: {"neo4j", "password"})

# Simple query
{:ok, results} = Neo4j.Driver.run(driver, "MATCH (n:Person) RETURN n.name", %{})

# With session
Neo4j.Driver.session(driver, fn session ->
  Neo4j.Session.run(session, "CREATE (p:Person {name: $name})", %{name: "Alice"})
end)

# Transaction
Neo4j.Driver.transaction(driver, fn tx ->
  Neo4j.Transaction.run(tx, "CREATE (p:Person {name: $name})", %{name: "Bob"})
  Neo4j.Transaction.commit(tx)
end)

Summary

Functions

Returns a specification to start this module under a supervisor.

Closes the driver and all its connections.

Closes a session.

Creates a new session.

Gets driver configuration.

Executes a Cypher query directly using the driver.

Creates a session and executes the given function with it.

Starts a new Neo4j driver.

Creates a transaction and executes the given function with it.

Functions

child_spec(init_arg)

Returns a specification to start this module under a supervisor.

See Supervisor.

close(driver)

Closes the driver and all its connections.

Parameters

  • driver: Driver process

close_session(session)

Closes a session.

Parameters

  • session: Session to close

create_session(driver)

Creates a new session.

Sessions should be closed when no longer needed using close_session/1.

Parameters

  • driver: Driver process

Returns

  • {:ok, session} on success
  • {:error, reason} on failure

get_config(driver)

Gets driver configuration.

Parameters

  • driver: Driver process

Returns

Current driver configuration map

run(driver, query, params \\ %{}, opts \\ [])

Executes a Cypher query directly using the driver.

This is a convenience method that creates a session, runs the query, and closes the session.

Parameters

  • driver: Driver process
  • query: Cypher query string
  • params: Query parameters map (default: %{})
  • opts: Query options (default: [])

Examples

{:ok, results} = Neo4j.Driver.run(driver, "MATCH (n:Person) RETURN n.name", %{})
{:ok, results} = Neo4j.Driver.run(driver, "CREATE (p:Person {name: $name})", %{name: "Alice"})

session(driver, fun)

Creates a session and executes the given function with it.

The session is automatically closed after the function completes.

Parameters

  • driver: Driver process
  • fun: Function that receives the session as an argument

Examples

result = Neo4j.Driver.session(driver, fn session ->
  Neo4j.Session.run(session, "MATCH (n:Person) RETURN count(n)")
end)

start_link(uri, opts \\ [])

Starts a new Neo4j driver.

Parameters

  • uri: Connection URI (e.g., "bolt://localhost:7687")
  • opts: Configuration options

Options

  • :auth - Authentication tuple {username, password} or map
  • :user_agent - Client user agent string
  • :max_pool_size - Maximum number of connections in pool
  • :connection_timeout - Connection timeout in milliseconds
  • :query_timeout - Query timeout in milliseconds

Examples

{:ok, driver} = Neo4j.Driver.start_link("bolt://localhost:7687",
  auth: {"neo4j", "password"})

{:ok, driver} = Neo4j.Driver.start_link("bolt://localhost:7687",
  auth: %{"scheme" => "basic", "principal" => "neo4j", "credentials" => "password"})

transaction(driver, fun)

Creates a transaction and executes the given function with it.

The transaction is automatically committed if the function succeeds, or rolled back if it raises an exception.

Parameters

  • driver: Driver process
  • fun: Function that receives the transaction as an argument

Examples

result = Neo4j.Driver.transaction(driver, fn tx ->
  Neo4j.Transaction.run(tx, "CREATE (p:Person {name: $name})", %{name: "Bob"})
  Neo4j.Transaction.run(tx, "CREATE (p:Person {name: $name})", %{name: "Carol"})
end)