AshNeo4j.Cypher (AshNeo4j v0.3.0)

Copy Markdown View Source

AshNeo4j Cypher Functions for converting Elixir data structures to Cypher query components and running Cypher queries against a Neo4j database. Ideally has no specific knowledge of Ash

Summary

Functions

Converts a node variable, label, predicates and operator to cypher expression

Converts a node variable and labels to basic cypher node expression.

Converts a node variable and optional property map to cypher WHERE conditions and variable prefixed parameters map.

Converts a node variable, labels and optional property map to cypher properties string and variable prefixed parameters map.

Converts a node variable and optional property map to cypher properties string and variable prefixed parameters map.

Converts a node_relationship tuple to cypher clause, ignoring the label

Converts a relationship variable, label and optional direction to cypher relationship.

Converts a list of property names into a remove properties string. The list is converted to a string in the format n.key1, n.key2.

Runs some cypher

Functions

expression(variable, left, operator, right, opts \\ [])

Converts a node variable, label, predicates and operator to cypher expression

Examples

iex> AshNeo4j.Cypher.expression(:s, "name", "IN", "[$s_name_0]")
"s.name IN [$s_name_0]"
iex> AshNeo4j.Cypher.expression(:s, "name", "is_nil", true)
"s.name IS NULL"
iex> AshNeo4j.Cypher.expression(:s, "name", "is_nil", false)
"s.name IS NOT NULL"
iex> AshNeo4j.Cypher.expression(:s, "name", "contains", "$s_name_0")
"s.name CONTAINS $s_name_0"
iex> AshNeo4j.Cypher.expression(:s, "name", "contains", "$s_name_0", case_insensitive?: true)
"toLower(s.name) CONTAINS toLower($s_name_0)"
iex> AshNeo4j.Cypher.expression(:s, "name", "=", "$s_name_0", case_insensitive?: true)
"toLower(s.name) = toLower($s_name_0)"

node(variable, labels)

Converts a node variable and labels to basic cypher node expression.

Examples

iex> AshNeo4j.Cypher.node(:s, [:Actor])
"(s:Actor)"

parameterized_conditions(variable, properties \\ %{})

Converts a node variable and optional property map to cypher WHERE conditions and variable prefixed parameters map.

Examples

iex> AshNeo4j.Cypher.parameterized_conditions(:n, %{name: "Bill Nighy"})
{"n.name = $n_name", %{"n_name" => "Bill Nighy"}}
iex> AshNeo4j.Cypher.parameterized_conditions(:n, %{name: "Bill Nighy", age: 72})
{"n.name = $n_name AND n.age = $n_age", %{"n_name" => "Bill Nighy", "n_age" => 72}}

parameterized_node(variable, labels, properties \\ %{})

Converts a node variable, labels and optional property map to cypher properties string and variable prefixed parameters map.

Examples

iex> AshNeo4j.Cypher.parameterized_node(:s, [:Actor])
{"(s:Actor)", %{}}
iex> AshNeo4j.Cypher.parameterized_node(:s, [:Cinema, :Actor], %{name: "Bill Nighy"})
{"(s:Cinema:Actor {name: $s_name})", %{"s_name" =>"Bill Nighy"}}

Note: the properties map is converted to parameter names by prefixing the keys with $<variable>, and the original values are returned in a separate map for use as query parameters.

parameterized_properties(variable, properties \\ %{})

Converts a node variable and optional property map to cypher properties string and variable prefixed parameters map.

Examples

iex> AshNeo4j.Cypher.parameterized_properties(:s)
{"{}", %{}}
iex> AshNeo4j.Cypher.parameterized_properties(:s, %{name: "Bill Nighy"})
{"{name: $s_name}", %{"s_name" =>"Bill Nighy"}}

relationship(node_relationship)

Converts a node_relationship tuple to cypher clause, ignoring the label

Examples

iex> AshNeo4j.Cypher.relationship({:movies, :ACTED_IN, :outgoing})
"-[r:ACTED_IN]->"
iex> AshNeo4j.Cypher.relationship(nil)
"-[r]-"

relationship(variable, label, direction \\ nil)

Converts a relationship variable, label and optional direction to cypher relationship.

Examples

iex> AshNeo4j.Cypher.relationship(:r, :ACTED_IN, :outgoing)
"-[r:ACTED_IN]->"
iex> AshNeo4j.Cypher.relationship(:r, :ACTED_IN, :incoming)
"<-[r:ACTED_IN]-"
iex> AshNeo4j.Cypher.relationship(:r, :KNOWS)
"-[r:KNOWS]-"

remove_properties(label, names)

@spec remove_properties(atom(), maybe_improper_list()) :: binary()

Converts a list of property names into a remove properties string. The list is converted to a string in the format n.key1, n.key2.

Examples

iex> AshNeo4j.Cypher.remove_properties(:n, [:born, :bafta_winner])
"n.born, n.bafta_winner"

run(cypher, params \\ %{})

Runs some cypher

Examples

iex> cypher = "CREATE (n:Actor {name: 'Bill Nighy', born: 1949, bafta_winner: true}) RETURN n"
iex> {result, _} = AshNeo4j.Cypher.run(cypher)
iex> result
:ok
iex> cypher = "MATCH (n:Actor {name: $name}) RETURN n"
iex> params = %{name: "Bill Nighy"}
iex> {result, _} = AshNeo4j.Cypher.run(cypher, params)
iex> result
:ok

run_expecting_deletions(cypher, params \\ %{})