AshNeo4j.Neo4jHelper (AshNeo4j v0.3.0)

Copy Markdown View Source

AshNeo4j DataLayer Neo4j Helper

Summary

Functions

Creates a neo4j node with labels and properties

Delete all neo4j nodes and relationships

Merges a neo4j node with label and properties

Tests if two nodes are related by traversal ## Examples

Reads limited nodes from Neo4j, given label, limit and optionally properties

Reads nodes from Neo4j, given label, and optionally properties

Reads nodes from Neo4j, returning any related nodes

Creates source neo4j node with label, properties and relationships to existing nodes

Relates two nodes with a relationship type, merging relationship ## Examples

Relates two nodes unrelating the destination node from any similar relationships ## Examples

Relates two nodes unrelating the source node from any similar relationships ## Examples

Relates two nodes unrelating the source and destination node from any similar relationships ## Examples

Safely delete neo4j nodes, delete is guarded by relationships

Functions

create_node(labels, properties)

Creates a neo4j node with labels and properties

Examples

iex> {result, _} = AshNeo4j.Neo4jHelper.create_node([:Cinema, :Actor], %{name: "Bill Nighy"})
iex> result
:ok

delete_all()

@spec delete_all() ::
  {:error,
   %{:__exception__ => true, :__struct__ => atom(), optional(atom()) => any()}}
  | {:ok, any()}

Delete all neo4j nodes and relationships

Examples

iex> {result, _} = AshNeo4j.Neo4jHelper.delete_all()
iex> result
:ok

delete_nodes(label, properties \\ %{})

Delete neo4j nodes

Examples

iex> {result, _} = AshNeo4j.Neo4jHelper.delete_nodes(:Actor)
iex> result
:ok
iex> AshNeo4j.Neo4jHelper.create_node([:Actor], %{name: "Bill Nighy"})
iex> {result, _} = AshNeo4j.Neo4jHelper.delete_nodes(:Actor, %{name: "Bill Nighy"})
iex> result
:ok

merge_node(label, properties)

@spec merge_node(atom(), map()) ::
  {:error,
   %{:__exception__ => true, :__struct__ => atom(), optional(atom()) => any()}}
  | {:ok, any()}

Merges a neo4j node with label and properties

Examples

iex> {result, _} = AshNeo4j.Neo4jHelper.merge_node(:Actor, %{name: "Bill Nighy", born: 1949})
iex> result
:ok

nodes_relate_how?(source_label, source_properties, dest_label, dest_properties, edges)

@spec nodes_relate_how?(atom(), map(), atom(), map(), [tuple()]) ::
  :error | false | true

Tests if two nodes are related by traversal ## Examples

iex> AshNeo4j.Neo4jHelper.create_node([:Actor], %{name: "Keira Knightley"})
iex> AshNeo4j.Neo4jHelper.create_node([:Actor], %{name: "Bill Nighy"})
iex> AshNeo4j.Neo4jHelper.create_node([:Movie], %{title: "Love Actually"})
iex> AshNeo4j.Neo4jHelper.relate_nodes(:Actor, %{name: "Keira Knightley"}, :Movie, %{title: "Love Actually"}, :ACTED_IN, :outgoing)
iex> AshNeo4j.Neo4jHelper.relate_nodes(:Actor, %{name: "Bill Nighy"}, :Movie, %{title: "Love Actually"}, :ACTED_IN, :outgoing)
iex> AshNeo4j.Neo4jHelper.nodes_relate_how?(:Actor, %{name: "Bill Nighy"}, :Actor, %{name: "Keira Knightley"}, [ACTED_IN: :outgoing, ACTED_IN: :incoming])
true

nodes_relate_how?(source_label, source_properties, dest_label, dest_properties, edge_label, edge_direction)

@spec nodes_relate_how?(atom(), map(), atom(), map(), atom(), atom()) ::
  :error | false | true

Tests if two nodes are directly related ## Examples

iex> AshNeo4j.Neo4jHelper.create_node([:Actor], %{name: "Bill Nighy", born: 1949})
iex> AshNeo4j.Neo4jHelper.create_node([:Movie], %{title: "Love Actually"})
iex> AshNeo4j.Neo4jHelper.relate_nodes(:Actor, %{name: "Bill Nighy"}, :Movie, %{title: "Love Actually"}, :ACTED_IN, :outgoing)
iex> AshNeo4j.Neo4jHelper.nodes_relate_how?(:Actor, %{name: "Bill Nighy"}, :Movie, %{title: "Love Actually"}, :ACTED_IN, :outgoing)
true

read_limited(label, limit, properties \\ %{})

Reads limited nodes from Neo4j, given label, limit and optionally properties

Examples

iex> AshNeo4j.Neo4jHelper.create_node([:Actor], %{name: "Bill Nighy", born: 1949})
iex> {:ok, %{records: records}} = AshNeo4j.Neo4jHelper.read_limited(:Actor, 1)
iex> length(records)
1

read_nodes(label, properties \\ %{})

Reads nodes from Neo4j, given label, and optionally properties

Examples

iex> AshNeo4j.Neo4jHelper.create_node([:Actor], %{name: "Bill Nighy", born: 1949})
iex> {:ok, %{records: records}} = AshNeo4j.Neo4jHelper.read_nodes(:Actor, %{name: "Bill Nighy"})
iex> length(records)
1

read_nodes_related(label, properties \\ %{})

Reads nodes from Neo4j, returning any related nodes

Examples

iex> AshNeo4j.Neo4jHelper.create_node([:Actor], %{name: "Bill Nighy", born: 1949})
iex> AshNeo4j.Neo4jHelper.create_node([:Movie], %{title: "Love Actually"})
iex> :ok = AshNeo4j.Neo4jHelper.relate_nodes(:Actor, %{name: "Bill Nighy"}, [{:Movie, %{title: "Love Actually"}, :ACTED_IN, :outgoing, false}])
iex> {:ok, %{records: records}} = AshNeo4j.Neo4jHelper.read_nodes_related(:Actor, %{name: "Bill Nighy"})
iex> length(records)
1

relate_nodes(label, properties, relationships)

@spec relate_nodes(atom(), map(), list()) :: {:error, bitstring()} | :ok

Creates source neo4j node with label, properties and relationships to existing nodes

Examples

iex> AshNeo4j.Neo4jHelper.create_node([:Actor], %{title: "Bill Nighy"})
iex> AshNeo4j.Neo4jHelper.create_node([:Actor], %{title: "Keira Knightley"})
iex> AshNeo4j.Neo4jHelper.create_node([:Movie], %{title: "Love Actually"})
iex> AshNeo4j.Neo4jHelper.create_node([:Movie], %{title: "Bend it Like Beckham"})
iex> AshNeo4j.Neo4jHelper.create_node([:Movie], %{title: "The Immitation Game"})

iex> :ok = AshNeo4j.Neo4jHelper.relate_nodes(:Actor, %{name: "Bill Nighy"}, [{:Movie, %{title: "Love Actually"}, :ACTED_IN, :outgoing, false}])
iex> :ok = AshNeo4j.Neo4jHelper.relate_nodes(:Actor, %{name: "Keira Knightley"}, [{:Movie, %{title: "Love Actually"}, :ACTED_IN, :outgoing, false}, {:Movie, %{title: "Bend it Like Beckham"}, :ACTED_IN, :outgoing, false}])

relate_nodes(source_label, source_properties, dest_label, dest_properties, edge_label, edge_direction)

@spec relate_nodes(atom(), map(), atom(), map(), atom(), atom()) ::
  {:error,
   %{:__exception__ => true, :__struct__ => atom(), optional(atom()) => any()}}
  | {:ok, any()}

Relates two nodes with a relationship type, merging relationship ## Examples

iex> AshNeo4j.Neo4jHelper.create_node([:Actor], %{name: "Bill Nighy", born: 1949})
iex> AshNeo4j.Neo4jHelper.create_node([:Movie], %{title: "Love Actually"})
iex> {result, _} = AshNeo4j.Neo4jHelper.relate_nodes(:Actor, %{name: "Bill Nighy"}, :Movie, %{title: "Love Actually"}, :ACTED_IN, :outgoing)
iex> result
:ok

relate_nodes(source_label, source_properties, dest_label, dest_properties, edge_label, edge_direction, options)

relate_nodes_unrelating_destination(source_label, source_properties, dest_label, dest_properties, edge_label, edge_direction)

@spec relate_nodes_unrelating_destination(
  atom(),
  map(),
  atom(),
  map(),
  atom(),
  atom()
) ::
  {:error,
   %{:__exception__ => true, :__struct__ => atom(), optional(atom()) => any()}}
  | {:ok, any()}

Relates two nodes unrelating the destination node from any similar relationships ## Examples

iex> AshNeo4j.Neo4jHelper.create_node([:Fan], %{name: "Matt"})
iex> AshNeo4j.Neo4jHelper.create_node([:Movie], %{title: "Love Actually"})
iex> AshNeo4j.Neo4jHelper.create_node([:Movie], %{title: "Bend it Like Beckham"})
iex> AshNeo4j.Neo4jHelper.relate_nodes(:Fan, %{name: "Matt"}, :Movie, %{title: "Love Actually"}, :FAVOURITE, :outgoing)
iex> {result, _} = AshNeo4j.Neo4jHelper.relate_nodes_unrelating_destination(:Movie, %{title: "Bend it Like Beckham"}, :Fan, %{name: "Matt"}, :FAVOURITE, :incoming)
iex> result
:ok

relate_nodes_unrelating_source(source_label, source_properties, dest_label, dest_properties, edge_label, edge_direction)

@spec relate_nodes_unrelating_source(atom(), map(), atom(), map(), atom(), atom()) ::
  {:error,
   %{:__exception__ => true, :__struct__ => atom(), optional(atom()) => any()}}
  | {:ok, any()}

Relates two nodes unrelating the source node from any similar relationships ## Examples

iex> AshNeo4j.Neo4jHelper.create_node([:Fan], %{name: "Matt"})
iex> AshNeo4j.Neo4jHelper.create_node([:Movie], %{title: "Love Actually"})
iex> AshNeo4j.Neo4jHelper.create_node([:Movie], %{title: "Bend it Like Beckham"})
iex> AshNeo4j.Neo4jHelper.relate_nodes(:Fan, %{name: "Matt"}, :Movie, %{title: "Love Actually"}, :FAVOURITE, :outgoing)
iex> {result, _} = AshNeo4j.Neo4jHelper.relate_nodes_unrelating_source(:Fan, %{name: "Matt"}, :Movie, %{title: "Bend it Like Beckham"}, :FAVOURITE, :outgoing)
iex> result
:ok

relate_nodes_unrelating_source_and_destination(source_label, source_properties, dest_label, dest_properties, edge_label, edge_direction)

@spec relate_nodes_unrelating_source_and_destination(
  atom(),
  map(),
  atom(),
  map(),
  atom(),
  atom()
) ::
  {:error,
   %{:__exception__ => true, :__struct__ => atom(), optional(atom()) => any()}}
  | {:ok, any()}

Relates two nodes unrelating the source and destination node from any similar relationships ## Examples

iex> AshNeo4j.Neo4jHelper.create_node([:Person], %{name: "Marlo"})
iex> AshNeo4j.Neo4jHelper.create_node([:Person], %{name: "Harry"})
iex> AshNeo4j.Neo4jHelper.create_node([:Person], %{name: "Marion"})
iex> AshNeo4j.Neo4jHelper.create_node([:Person], %{name: "Robin"})
iex> AshNeo4j.Neo4jHelper.relate_nodes(:Person, %{name: "Marlo"}, :Person, %{name: "Harry"}, :PARTNER, :outgoing)
iex> AshNeo4j.Neo4jHelper.relate_nodes(:Person, %{name: "Marion"}, :Person, %{name: "Robin"}, :PARTNER, :outgoing)
iex> {result, _} = AshNeo4j.Neo4jHelper.relate_nodes_unrelating_source_and_destination(:Person, %{name: "Marlo"}, :Person, %{name: "Robin"}, :PARTNER, :outgoing)
iex> result
:ok

safe_delete_nodes(label, properties, relationships)

Safely delete neo4j nodes, delete is guarded by relationships

Examples

iex> AshNeo4j.Neo4jHelper.create_node([:Actor], %{name: "Keira Knightley"})
iex> AshNeo4j.Neo4jHelper.create_node([:Movie], %{title: "Love Actually"})
iex> AshNeo4j.Neo4jHelper.create_node([:Movie], %{title: "Bend it Like Beckham"})
iex> AshNeo4j.Neo4jHelper.relate_nodes(:Actor, %{name: "Keira Knightley"}, [{:Movie, %{title: "Love Actually"}, :ACTED_IN, :outgoing, false},
...> {:Movie, %{title: "Bend it Like Beckham"}, :ACTED_IN, :outgoing, false}])
iex> {result, _} = AshNeo4j.Neo4jHelper.safe_delete_nodes(:Actor, %{name: "Keira Knightley"}, [{:ACTED_IN, :outgoing, :Movie}, {:LIVES_AT, :outgoing, :Place}])
iex> result
:error
iex> {result, _} = AshNeo4j.Neo4jHelper.safe_delete_nodes(:Actor, %{name: "Keira Knightley"}, [{:LIVES_AT, :outgoing, :Place}])
iex> result
:ok

unrelate_nodes(source_label, source_properties, dest_label, dest_properties, edge_label, edge_direction)

@spec unrelate_nodes(atom(), map(), atom(), map(), atom(), atom()) ::
  {:error,
   %{:__exception__ => true, :__struct__ => atom(), optional(atom()) => any()}}
  | {:ok, any()}

Unrelates two nodes with a relationship type ## Examples

iex> AshNeo4j.Neo4jHelper.create_node([:Actor], %{name: "Bill Nighy", born: 1949})
iex> AshNeo4j.Neo4jHelper.create_node([:Movie], %{title: "Love Actually"})
iex> AshNeo4j.Neo4jHelper.relate_nodes(:Actor, %{name: "Bill Nighy"}, :Movie, %{title: "Love Actually"}, :ACTED_IN, :outgoing)
iex> {result, _} = AshNeo4j.Neo4jHelper.unrelate_nodes(:Actor, %{name: "Bill Nighy"}, :Movie, %{title: "Love Actually"}, :ACTED_IN, :outgoing)
iex> result
:ok

update_node(label, match_properties, set_properties, remove_properties \\ [])

@spec update_node(atom(), map(), map(), list()) ::
  {:error,
   %{:__exception__ => true, :__struct__ => atom(), optional(atom()) => any()}}
  | {:ok, any()}

Updates neo4j node properties

Examples

iex> AshNeo4j.Neo4jHelper.create_node([:Actor], %{name: "Bill Nighy"})
iex> {result, _} = AshNeo4j.Neo4jHelper.update_node(:Actor, %{name: "Bill Nighy"}, %{born: 1949})
iex> result
:ok