ArangoXEcto.create_edge
You're seeing just the function
create_edge
, go back to ArangoXEcto module for more information.
Specs
create_edge(Ecto.Repo.t(), mod(), mod(), keyword()) :: {:ok, Ecto.Schema.t()} | {:error, Ecto.Changeset.t()}
Creates an edge between two modules
This can create an edge collection dynamically if no additional fields are required, otherwise an edge schema needs to be specified.
The collection name can be passed as an option or is obtained from the provided schema, otherwise it is generated dynamically.
Since ArangoDB does not care about the order of the from and two options in anonymous graphs, the order of the from and to attributes used in this function will work either way.
Parameters
repo
- The Ecto repo module to use for queriesfrom
- The Ecto Schema struct to use for the from vertexto
- The Ecto Schema struct to use for the to vertexopts
- Options to use
Options
Accepts the following options:
:edge
- A specific edge module to use for the edge. This is required for any additional fields on the edge. Overridescollection_name
.:fields
- The values of the fields to set on the edge. Requiresedge
to be set otherwise it is ignored.:collection_name
- The name of the collection to use.
Examples
iex> ArangoXEcto.create_edge(Repo, user1, user2)
%UserUser{_from: "users/12345", _to: "users/54321"}
Create an edge with a specific edge collection name
iex> ArangoXEcto.create_edge(Repo, user1, user2, collection_name: "friends")
%Friends{_from: "users/12345", _to: "users/54321"}
Create a edge schema and use it to create an edge relation
defmodule UserPosts do
use ArangoXEcto.Edge,
from: User,
to: Post
import Ecto.Changeset
schema "user_posts" do
edge_fields()
field(:type, :string)
end
def changeset(edge, attrs) do
edges_changeset(edge, attrs)
|> cast(attrs, [:type])
|> validate_required([:type])
end
end
iex> ArangoXEcto.create_edge(Repo, user1, user2, edge: UserPosts, fields: %{type: "wrote"})
%UserPosts{_from: "users/12345", _to: "users/54321", from: #Ecto.Association.NotLoaded<association :from is not loaded>, to: #Ecto.Association.NotLoaded<association :to is not loaded>, type: "wrote"}