View Source Ex4j.Cypher (Ex4j v0.1.0)

DSL to turn elixir code into Cypher Language.

Summary

Functions

Returns the Cypher query.

Creates an EDGE.

Creates a LIMIT clause.

Creates a MATCH clause.

Creates a RETURN clause.

Build and execute the query.

Creates a VERTEX.

Creates a WHERE clause.

Functions

@spec cypher(query :: map() | String.t()) :: String.t()

Returns the Cypher query.

Parameters

  • query: Either the dynamic query or a string cypher query

Examples

iex> query = match(User, as: :user)
iex> query = return(query, :user)
iex> cypher(query)
"MATCH (user:User) RETURN user"

iex> cypher("MATCH (user:User) RETURN user")
"MATCH (user:User) RETURN user"
Link to this function

edge(query, module, list)

View Source
@spec edge(query :: map(), module :: atom(), keyword()) :: map()

Creates an EDGE.

Parameters

  • query: The previous query
  • module: The name of the Node
  • as: The alias of the Node
  • from: Start point
  • to: End point
  • type:
    • :out (->)
    • :in (<-)
    • :any (-)

Examples

iex> query = match(Node.User, as: :user)
iex> query = vertex(query, Node.Comment, as: :comment)
iex> query = edge(query, HAS, as: :h, from: :user, to: :comment, type: :any)
iex> query = where(query, :user, "user.name = 'Tiago'"])
iex> query = where(query, :h, "h.value = 42")
iex> return(query, :comment, [:text])
%{
  match: %{user: "User"},
  vertex: %{comment: "Comment"},
  where: %{
    user: "user.name = 'Tiago'",
    h: "h.value = 42"
  },
  edge: %{
    user: %{module: HAS, type: :any, to: :comment, label: :h}
  },
  return: %{comment: [:text]}
}

Cypher

MATCH (user:User WHERE user.name = 'Tiago')-[h:HAS WHERE h.value = 42]-(comment:Comment)
RETURN comment.text
@spec limit(query :: map(), limit :: integer()) :: map()

Creates a LIMIT clause.

Parameters

  • query: The previous query
  • limit: The limit value

Examples

iex> query = match(User, as: :user)
iex> query = return(query, :user)
iex> limit(query, 10)
%{return: %{user: []}, match: %{user: "User"}, limit: 10}

Cypher

MATCH(user:User)
RETURN user
LIMIT 10
@spec match(
  module :: module(),
  keyword()
) :: map()

Creates a MATCH clause.

Parameters

  • module: The name of the Node
  • as: The alias of the Node

Examples

iex> match(Node.User, as: :user)
%{match: %{user: "User"}}

iex> query = match(Node.User, as: :user)
iex> match(query, Node.Comment, as: :comment)
%{match: %{user: "User", comment: "Comment"}}

Cypher

MATCH(user:User)
MATCH(comment:Comment)
Link to this function

match(query, module, list)

View Source
@spec match(query :: map(), module :: module(), keyword()) :: map()
Link to this function

return(query, label, props \\ [])

View Source
@spec return(query :: map(), label :: atom(), props :: list()) :: map()

Creates a RETURN clause.

Parameters

  • query: The previous query
  • label: The alias of the Node
  • props: An optional list of properties to return

Examples

iex> query = match(Node.User, as: :user)
iex> return(query, :user)
%{return: %{user: []}, match: %{user: "User"}}

iex> query = match(Node.User, as: :user)
iex> return(query, :user, [:age, name])
%{return: %{user: [:age, :name]}, match: %{user: "User"}}

iex> query = match(Node.User, as: :user)
iex> query = match(query, Node.Comment, as: :comment)
iex> query = return(query, :user, [:age, :name])
iex> return(query, :comment, [:text])
%{
    return: %{user: [:age, :name], comment: [:text]},
    match: %{user: "User", comment: "Comment"}
}

Cypher

MATCH(user:User)
MATCH(comment:Comment)
RETURN user, comment

MATCH(user:User)
MATCH(comment:Comment)
RETURN user.name, user.age, comment.text
@spec run(query :: map() | String.t()) :: any()

Build and execute the query.

Parameters

  • query: Either the dynamic query or a string cypher query

Examples

iex> query = match(Node.User, as: :user)
iex> query = return(query, :user)
iex> run(query)
[%{"user" => %Node.User{uuid: nil, name: "Tiago", age: 38, email: nil}}]

iex> query = """
              MATCH (user:User WHERE user.name = 'Tiago')
              RETURN user
             """
iex> run(query)
[%{"user" => %Node.User{uuid: nil, name: "Tiago", age: 38, email: nil}}]
Link to this function

vertex(query, module, list)

View Source
@spec vertex(query :: map(), module :: atom(), keyword()) :: map()

Creates a VERTEX.

Parameters

  • query: The previous query
  • module: The name of the Node
  • as: The alias of the Node

Examples

iex> query = match(Node.User, as: :user)
iex> query = vertex(query, Node.Comment, as: :comment)
iex> query = edge(query, HAS, as: :h, from: :user, to: :comment, type: :any)
iex> query = where(query, :user, "user.name = 'Tiago'")
iex> query = where(query, :comment, "comment.value IN [1,2,3]")
iex> query = where(query, :h, "h.total = 10")
iex> return(query, :user)
%{
    match: %{user: "User"},
    where: %{
      user:  "user.name = 'Tiago'",
      h: "h.total = 10",
      comment: "comment.value IN [1, 2, 3]"
    },
    edge: %{user: %{module: HAS, type: :any, to: :comment, label: :h}},
    vertex: %{comment: "Comment"},
    return: %{user: []}
}

Cypher

MATCH (user:User WHERE user.name = 'Tiago')-[h:HAS WHERE h.total = 10]-(comment:Comment WHERE comment.value in [1,2,3])
RETURN user
Link to this function

where(query, label, rules)

View Source
@spec where(query :: map(), label :: atom(), rules :: String.t()) :: map()

Creates a WHERE clause.

Parameters

  • query: The previous query
  • label: The alias of the Node
  • rules: The String rules

Examples

iex> query = match(Node.User, as: :user)
iex> where(query, :user, "user.name = 'Tiago' AND user.age = 38")
%{
  match: %{user: "User"},
  where: %{user: "user.name = 'Tiago' AND user.age = 38"}
}

iex> query = match(Node.User, as: :user)
iex> where(query, :user, "user.name = 'Tiago' OR user.age IN [38]")
%{
  match: %{user: "User"},
  where: %{user: "user.name = 'Tiago' OR user.age IN [38]"}
}

Cypher

MATCH(user:User WHERE user.age = 38 AND user.name = 'Tiago')
MATCH(user:User WHERE user.name = 'Tiago' OR user.age IN [38])