SparrowDB. Graph
(sparrowdb v0.1.0)
Copy Markdown
High-level graph operations for RyuGraph.
This module provides Elixir-friendly functions for common graph operations, abstracting away the Cypher query language for simple use cases.
Summary
Functions
Creates a node with the given label and properties.
Creates a node, raising on error.
Creates a relationship between two nodes.
Creates a relationship between two nodes, raising on error.
Deletes a node and optionally its relationships.
Finds nodes matching the given criteria.
Finds nodes matching the given criteria, raising on error.
Gets all neighbors of a node.
Gets all neighbors of a node, raising on error.
Finds the shortest path between two nodes.
Updates properties of a node.
Updates properties of a node, raising on error.
Functions
Creates a node with the given label and properties.
Parameters
base- A connection referencelabel- The node labelproperties- A map or keyword list of properties
Examples
iex> SparrowDB.Graph.create_node(base, "Person", name: "Alice", age: 25)
{:ok, %{node: true, id: ..., label: "Person", properties: %{name: "Alice", age: 25}}}
Creates a node, raising on error.
Same as create_node/3 but raises an exception on error.
@spec create_relationship( dynamic(), String.t() | integer(), String.t() | integer(), String.t(), keyword() | map() ) :: {:ok, map()} | {:error, String.t()}
Creates a relationship between two nodes.
Parameters
base- A connection referencefrom_node_id- The ID of the source nodeto_node_id- The ID of the destination noderel_label- The relationship labelproperties- Optional properties for the relationship
Examples
iex> SparrowDB.Graph.create_relationship(base, node1_id, node2_id, "KNOWS",
...> since: 2020
...> )
{:ok, %{rel: true, id: ..., label: "KNOWS", src: ..., dst: ..., properties: %{since: 2020}}}
@spec create_relationship!( dynamic(), String.t() | integer(), String.t() | integer(), String.t(), keyword() | map() ) :: map()
Creates a relationship between two nodes, raising on error.
Same as create_relationship/5 but raises an exception on error.
@spec delete_node(dynamic(), String.t() | integer(), keyword()) :: {:ok, :deleted} | {:error, String.t()}
Deletes a node and optionally its relationships.
Parameters
base- A connection referencenode_id- The ID of the node to deleteopts- Deletion options
Options
:detach- If true, deletes the node and all its relationships (default: false)
Examples
iex> SparrowDB.Graph.delete_node(base, node_id)
{:ok, :deleted}
iex> SparrowDB.Graph.delete_node(base, node_id, detach: true)
{:ok, :deleted}
@spec find_nodes(dynamic(), String.t(), map() | String.t() | nil, keyword()) :: {:ok, [map()]} | {:error, String.t()}
Finds nodes matching the given criteria.
Parameters
base- A connection referencelabel- The node label to search forwhere_clause- Optional WHERE clause conditions as a map or stringopts- Query options
Options
:limit- Maximum number of results to return:order_by- Property to order results by:desc- If true, order results in descending order (default: false)
Examples
iex> SparrowDB.Graph.find_nodes(base, "Person")
{:ok, [%{node: true, label: "Person", properties: %{...}}, ...]}
iex> SparrowDB.Graph.find_nodes(base, "Person", %{age: 25})
{:ok, [%{node: true, label: "Person", properties: %{name: "Alice", age: 25}}]}
iex> SparrowDB.Graph.find_nodes(base, "Person", "n.age > 20", limit: 10)
{:ok, [...]}
Finds nodes matching the given criteria, raising on error.
Same as find_nodes/4 but raises an exception on error.
@spec get_neighbors(dynamic(), String.t() | integer(), keyword()) :: {:ok, [map()]} | {:error, String.t()}
Gets all neighbors of a node.
Parameters
base- A connection referencenode_id- The ID of the nodeopts- Options for the query
Options
:direction-:out(default),:in, or:both:rel_label- Only follow relationships with this label:depth- How many hops to traverse (default: 1)
Examples
iex> SparrowDB.Graph.get_neighbors(base, node_id)
{:ok, [%{node: true, ...}, ...]}
iex> SparrowDB.Graph.get_neighbors(base, node_id,
...> direction: :both,
...> rel_label: "KNOWS"
...> )
{:ok, [...]}
Gets all neighbors of a node, raising on error.
Same as get_neighbors/3 but raises an exception on error.
@spec shortest_path( dynamic(), String.t() | integer(), String.t() | integer(), keyword() ) :: {:ok, map()} | {:error, String.t()}
Finds the shortest path between two nodes.
Parameters
base- A connection referencefrom_node_id- The ID of the start nodeto_node_id- The ID of the end nodeopts- Path finding options
Options
:rel_label- Only follow relationships with this label:max_length- Maximum path length (default: no limit)
Examples
iex> SparrowDB.Graph.shortest_path(base, node1_id, node2_id)
{:ok, %{nodes: [...], rels: [...]}}
iex> SparrowDB.Graph.shortest_path(base, node1_id, node2_id,
...> rel_label: "KNOWS",
...> max_length: 3
...> )
{:ok, %{nodes: [...], rels: [...]}}
@spec update_node(dynamic(), String.t() | integer(), keyword() | map()) :: {:ok, map()} | {:error, String.t()}
Updates properties of a node.
Parameters
base- A connection referencenode_id- The ID of the node to updateproperties- Properties to set or update
Examples
iex> SparrowDB.Graph.update_node(base, node_id, age: 26, city: "New York")
{:ok, %{node: true, properties: %{age: 26, city: "New York", ...}}}
Updates properties of a node, raising on error.
Same as update_node/3 but raises an exception on error.