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
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
create!(struct_or_changeset, opts \\ [])
Specs
create!( Seraph.Schema.Node.t() | Seraph.Schema.Relationship.t() | Seraph.Changeset.t(), Keyword.t() ) :: Seraph.Schema.Node.t() | Seraph.Schema.Relationship.t()
Same as create/2
but raise if changeset is invalid.
create_or_set(struct_or_changeset, opts \\ [])
Specs
create_or_set(Seraph.Schema.Node.t() | Seraph.Changeset.t(), Keyword.t()) :: {:ok, Seraph.Schema.Relationship.t()} | {:error, Seraph.Changeset.t()}
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
create_or_set!(struct_or_changeset, opts \\ [])
Specs
create_or_set!( Seraph.Schema.Node.t() | Seraph.Schema.Relationship.t() | Seraph.Changeset.t(), Keyword.t() ) :: Seraph.Schema.Node.t() | Seraph.Schema.Relationship.t()
Same as create_or_set/2 but raise in case of error
delete(struct_or_changeset)
Specs
delete(Seraph.Schema.Node.t() | Seraph.Changeset.t()) :: {:ok, Seraph.Schema.Node.t()} | {:error, Seraph.Changeset.t()}
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
delete!(struct_or_changeset)
Specs
delete!(Seraph.Schema.Node.t() | Seraph.Changeset.t()) :: Seraph.Schema.Node.t()
Same as delete/1
but raise in case of error.
get(queryable, identifier_value)
Specs
get(Seraph.Repo.queryable(), any()) :: nil | Seraph.Schema.Node.t() | Seraph.Schema.Relationship.t()
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)
get!(queryable, identifier_value)
Specs
get!(Seraph.Repo.queryable(), any()) :: Seraph.Schema.Node.t() | Seraph.Schema.Relationship.t()
Same as get/2 but raise if no Node is found.
Raise if more than one Node is found.
get_by(queryable, clauses)
Specs
get_by(Seraph.Repo.queryable(), Keyword.t() | map()) :: nil | Seraph.Schema.Node.t()
Fetch a single result from the query.
Return nil if no result was found.
Raise if more than one entry.
get_by!(queryable, clauses)
Specs
get_by!(Seraph.Repo.queryable(), Keyword.t() | map()) :: Seraph.Schema.Node.t()
Same as get_by/2 but raise if no Node is found.
Raise if more than one Node is found.
merge(queryable, merge_keys_data, opts)
Specs
merge(Seraph.Repo.queryable(), map(), Keyword.t()) :: {:ok, Seraph.Schema.Node.t()} | {:error, Seraph.Changeset.t()}
This function is the equivalent to MERGE ... ON CREATE SET... ON MATCH SET...
.
It requires:
queryable
- The queryable to mergemerge_keys_data
: a map with the merge keys data to used to match the nodeopts
- 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 givenchangeset_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 givenchangeset_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
merge!(queryable, merge_keys_data, opts)
Specs
merge!(Seraph.Repo.queryable(), map(), Keyword.t()) :: Seraph.Schema.Node.t()
Same as merge/3 but raise in case of error
preload(struct, preloads, opts \\ [])
Specs
preload(Seraph.Schema.Node.t(), atom() | [atom()], Keyword.t()) :: Seraph.Schema.Node.t()
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 totrue
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)
set(changeset, opts \\ [])
Specs
set(Seraph.Changeset.t(), Keyword.t()) :: {:ok, Seraph.Schema.Node.t() | Seraph.Schema.Relationship.t()} | {:error, Seraph.Changeset.t()}
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
set!(changeset, opts \\ [])
Specs
set!(Seraph.Changeset.t(), Keyword.t()) :: Seraph.Schema.Node.t() | Seraph.Schema.Relationship.t()
Same as set/2 but raise in case of error