Seraph.Example.Repo (Seraph v0.2.4)

Link to this section Summary

Functions

Fetch all results from the query.

Execute the given query against the database and return the results.

Same as execute/2 but raise in case of error.

Fetch a single result from the query.

Same as one/2 but raise ino results found.

Execute the given statement with the given params. Return the query result or an error.

Same as query/3 but raise in case of error.

Link to this section Functions

Link to this function

all(query, opts \\ [])

Fetch all results from the query.

Options:

  • with_stats - If set to true, also returns the query stats (number of created nodes, created properties, etc.). Return data will then be:

    %{results: the_query_results,
      stats: the_query_stats}
  • relationship_result - define how much the returned should be fleshed. Possible values are:

    • :contextual (default) - Relationship nodes data will be retrieved from query result
    • :no_nodes - Relationship nodes will be set to nil, even if they appear if the query result
    • :full - Realtionship nodes will be set even if their data aren't part of the query.

    Note that depending on how much data is retrieved, this option can help reduce the amount of data retrieved from database,

and therefore reduce the number of related structs to build. This can have a real impact on performance.

Example

# Fetch all users
query = match [{u, MyApp.User}],
  return [u]
MyApp.Repo.all(query)
Link to this function

execute(query, opts \\ [])

Specs

execute(Seraph.Query.t(), Keyword.t()) ::
  {:ok, [map()] | %{results: [map()], stats: map()}} | {:error, any()}

Execute the given query against the database and return the results.

Options:

  • with_stats - If set to true, also returns the query stats (number of created nodes, created properties, etc.). Return data will then be:

    %{results: the_query_results,
      stats: the_query_stats}
  • relationship_result - define how much the returned should be fleshed. Possible values are:

    • :contextual (default) - Relationship nodes data will be retrieved from query result
    • :no_nodes - Relationship nodes will be set to nil, even if they appear if the query result
    • :full - Realtionship nodes will be set even if their data aren't part of the query.

    Note that depending on how much data is retrieved, this option can help reduce the amount of data retrieved from database,

and therefore reduce the number of related structs to build. This can have a real impact on performance.

Example

# Fetch all users
query = match [{u, MyApp.User}],
  return [u]
MyApp.Repo.execute(query)
Link to this function

execute!(query, opts \\ [])

Specs

execute!(Seraph.Query.t(), Keyword.t()) ::
  [map()] | %{results: [map()], stats: map()}

Same as execute/2 but raise in case of error.

Link to this function

one(query, opts \\ [])

Fetch a single result from the query.

Returns nil if no result was found. Raises if more than one entry.

Options:

  • with_stats - If set to true, also returns the query stats (number of created nodes, created properties, etc.). Return data will then be:

    %{results: the_query_results,
      stats: the_query_stats}
  • relationship_result - define how much the returned should be fleshed. Possible values are:

    • :contextual (default) - Relationship nodes data will be retrieved from query result
    • :no_nodes - Relationship nodes will be set to nil, even if they appear if the query result
    • :full - Realtionship nodes will be set even if their data aren't part of the query.

    Note that depending on how much data is retrieved, this option can help reduce the amount of data retrieved from database,

and therefore reduce the number of related structs to build. This can have a real impact on performance.

Example

# Fetch one user
query = match [{u, MyApp.User, %{uid: 1}}],
  return [u]
MyApp.Repo.one(query)
Link to this function

one!(query, opts \\ [])

Same as one/2 but raise ino results found.

Link to this function

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

Specs

query(String.t(), map(), Keyword.t()) ::
  {:ok, [map()] | %{results: [map()], stats: map()}} | {:error, any()}

Execute the given statement with the given params. Return the query result or an error.

Options:

  • with_stats - If set to true, also returns the query stats (number of created nodes, created properties, etc.)

Example

# Without params
iex> MyRepo.query("CREATE (p:Person {name: 'Collin Chou', role: 'Seraph'}) RETURN p")
{:ok,
[
  %{
    "p" => %Bolt.Sips.Types.Node{
      id: 1813,
      labels: ["Person"],
      properties: %{"name" => "Collin Chou", "role" => "Seraph"}
    }
  }
]}

# With params
iex(15)> MyRepo.query("MATCH (p:Person {name: $name}) RETURN p.role", %{name: "Collin Chou"})
{:ok, [%{"p.role" => "Seraph"}]}

# With :with_stats option
iex(16)> MyRepo.query("MATCH (p:Person {name: $name}) DETACH DELETE p", %{name: "Collin Chou"}, with_stats: true)
{:ok, %{results: [], stats: %{"nodes-deleted" => 1}}}
Link to this function

query!(statement, params \\ %{}, opts \\ [])

Specs

query!(String.t(), map(), Keyword.t()) ::
  [map()] | %{results: [map()], stats: map()}

Same as query/3 but raise in case of error.