Seraph.Example.Repo.Node (Seraph v0.2.4)

Link to this section Summary

Functions

Create a Node defined via Seraph.Schema.Node or a changeset.

Same as create/2 but raise if changeset is invalid.

Create new Node or set new data to it.

Same as create_or_set/2 but raise in case of error

Deletes a Node struct using its merge keys.

Same as delete/1 but raise in case of error.

Fetches a single Node struct from the data store where the identifier key matches the given identifier value.

Same as get/2 but raise if no Node is found.

Fetch a single result from the query.

Same as get_by/2 but raise if no Node is found.

This function is the equivalent to MERGE ... ON CREATE SET... ON MATCH SET....

Same as merge/3 but raise in case of error

Preload relationships / related nodes on the given Node or Nodes. Either a relationship type field or a node list field can be passed as preload.

Update a changeset using its merge keys.

Same as set/2 but raise in case of error

Link to this section Functions

Link to this function

create(struct_or_changeset, opts \\ [])

Create a Node defined via Seraph.Schema.Node or a changeset.

It returns {:ok, struct} if the struct has been successfully created or {:error, changeset} if there was a validation error.

Example

case MyRepo.Node.create(%Person{name: "Collin Chou", role: "Seraph"}) do
  {:ok, struct} -> # succesful creation
  {:ok, changeset} -> # invalid changeset
end
Link to this function

create!(struct_or_changeset, opts \\ [])

Specs

Same as create/2 but raise if changeset is invalid.

Link to this function

create_or_set(struct_or_changeset, opts \\ [])

Specs

Create new Node or set new data to it.

If merge keys are present and not nil in given struct or changeset -> set new data otherwise -> create a new Node

Example

result =
  case MyRepo.Node.get(Person, id) do
    nil  -> %Person{id: id}   # Person not found, we build one
    person -> person          # Person exists, let's use it
  end
  |> Person.changeset(changes)
  |> MyRepo.Node.merge

case result do
  {:ok, struct}       -> # Merged with success
  {:error, changeset} -> # Something went wrong
end
Link to this function

create_or_set!(struct_or_changeset, opts \\ [])

Specs

Same as create_or_set/2 but raise in case of error

Link to this function

delete(struct_or_changeset)

Specs

Deletes a Node struct using its merge keys.

It returns {:ok, struct} if the struct has been successfully deleted or {:error, changeset} if there was a validation error.

Example

person = MyRepo.Node.get!(Person, 42)
case MyRepo.Node.delete(person) do
  {:ok, struct}       -> # Deleted with success
  {:error, changeset} -> # Something went wrong
end
Link to this function

delete!(struct_or_changeset)

Specs

Same as delete/1 but raise in case of error.

Link to this function

get(queryable, identifier_value)

Specs

Fetches a single Node struct from the data store where the identifier key matches the given identifier value.

Returns nil if no result was found.

Raise if more than one Node is found.

Example

MyRepo.Node.get(Person, 42)
Link to this function

get!(queryable, identifier_value)

Specs

Same as get/2 but raise if no Node is found.

Raise if more than one Node is found.

Link to this function

get_by(queryable, clauses)

Specs

Fetch a single result from the query.

Return nil if no result was found.

Raise if more than one entry.

Link to this function

get_by!(queryable, clauses)

Specs

Same as get_by/2 but raise if no Node is found.

Raise if more than one Node is found.

Link to this function

merge(queryable, merge_keys_data, opts)

Specs

This function is the equivalent to MERGE ... ON CREATE SET... ON MATCH SET....

It requires:

  • queryable - The queryable to merge
  • merge_keys_data: a map with the merge keys data to used to match the node
  • opts - at least one of these three options must be present:
    • :on_create: a tuple {data, changeset_fn} with the data to set on node if it's created. Given data will be validated through given changeset_fn.
    • :on_match: a tuple {data, changeset_fn} with the data to set on node if it already exists and is matched. Given data will be validated through given changeset_fn
    • :no_data a boolean. Set to true allow to not provide :on_match nor :on_create and add no properties if node is created / updated. Useful for Node without properties.

It returns {:ok, struct} if the struct has been successfully merged or {:error, changeset} if there was a validation error.

Examples

# Node creation
result = MyRepo.Node.merge(Person, %{},
                      on_create: {%{name: "Collin Chou", role: "Seraph"}, &Person.changeset/2})
case result do
  {:ok, struct}       -> # Merged with success
  {:error, changeset} -> # Something went wrong
end

# Node update
person = MyRepo.get!(Person)
result = MyRepo.Node.merge(Person, %{},
                      on_match: {%{role: "anoter roleSeraph}, &Person.changeset/2})
case result do
  {:ok, struct}       -> # Merged with success
  {:error, changeset} -> # Something went wrong
end

# Both depending on wether the node is found or not
result = MyRepo.Node.merge(Person, %{},
                      on_create: {%{name: "Collin Chou", role: "Seraph"}, &Person.changeset/2}
                      on_match: {%{role: "Another role}, &Person.role_changeset/2})
case result do
  {:ok, struct}       -> # Merged with success
  {:error, changeset} -> # Something went wrong
end
Link to this function

merge!(queryable, merge_keys_data, opts)

Specs

Same as merge/3 but raise in case of error

Link to this function

preload(struct, preloads, opts \\ [])

Specs

Preload relationships / related nodes on the given Node or Nodes. Either a relationship type field or a node list field can be passed as preload.

By default relationships and related nodes will be loaded.

In case the association was already loaded, preload won't attempt to reload it.

Options:

  • :load - Define the type of data for load:
    • :all: Loads relationships and related nodes data. (default)
    • :nodes: Loads only related nodes data
    • :relationships: Loads only relationships data
  • :force - Set to true force the reload of an already loaded relation. Default: false
  • :limit - To limit the number of preloaded data. Note that results are ordered by the Node identifier key.

Example

# Use a single atom to preload single relationship type data person = MyRepo.Node.preload(person, :acted_in)

# Use a single atom to preload only the nodes person = MyRepo.Node.preload(person, :acted_in, load: :nodes)

# Use a list of atoms to preload multiple relationship type data person = MyRepo.Node.preload(person, [:acted_in, :directed])

# Limit number of preload person = MyRepo.Node.preload(person, :acted_in, limit: 50)

# Forece preload on an alredy preloaded struct person = MyRepo.Node.preload(person, :acted_in, force: true)

Link to this function

set(changeset, opts \\ [])

Specs

Update a changeset using its merge keys.

It returns {:ok, struct} if the struct has been successfully updated or {:error, changeset} if there was a validation error.

Example

person = MyRepo.Node.get!(Person, 42)
person = Seraph.Changeset.change(person, %{role: "Not Seraph"})
case MyRepo.Node.set(person) do
  {:ok, struct}       -> # Updated with success
  {:error, changeset} -> # Something went wrong
end
Link to this function

set!(changeset, opts \\ [])

Specs

Same as set/2 but raise in case of error