View Source ArangoXEcto.Schema (ArangoX Ecto v1.3.1)
This module is a helper to automatically specify the primary key.
The primary key is the Arango _key
field but the _id field is also provided.
Schema modules should use this module by add use ArangoXEcto.Schema
to the module. The only
exception to this is if the collection is an edge collection, in that case refer to ArangoXEcto.Edge.
example
Example
defmodule MyProject.Accounts.User do
use ArangoXEcto.Schema
import Ecto.Changeset
schema "users" do
field :first_name, :string
field :last_name, :string
timestamps()
end
@doc false
def changeset(app, attrs) do
app
|> cast(attrs, [:first_name, :last_name])
|> validate_required([:first_name, :last_name])
end
end
Link to this section Summary
Functions
Creates a foreign key with correct format
Defines an incoming relationship
Defines indexs for collection dynamic creation
Defines an incoming relationship of one object
Defines an outgoing relationship of one object
Defines collection options for dynamic creation
Defines an outgoing relationship of many objects
A wrapper around the Ecto.Schema.schema/2
to add the _id field
Link to this section Functions
Creates a foreign key with correct format
Appends _id to the atom
Defines an incoming relationship
Behind the scenes this defines a many to many relationship so that Ecto can load the relationship using the built in functions.
The use of this function MUST be accompanied by a outgoing/3
definition in the other target node.
This will also define the __id__
field if it is not already defined so that ecto can map the relationship.
example
Example
defmodule MyProject.Post do
use ArangoXEcto.Schema
schema "posts" do
field :title, :string
# Will use the automatically generated edge
incoming :users, MyProject.User
# Will use the UserPosts edge
incoming :users, MyProject.User, edge: MyProject.UserPosts
end
end
Defines indexs for collection dynamic creation
The available options can be found in the ArangoXEcto.Migration.Index
module.
example
Example
To create a generic hash index you don't need to pass the type.
indexes [
[fields: [:email, :username]]
]
To create a geoJson index set the type to geo and set geoJson
to true.
indexes [
[fields: [:point], type: :geo, geoJson: true]
]
To create a two seperate indexes just supply them seperately.
indexes [
[fields: [:email]],
[fields: [:username]]
]
Defines an incoming relationship of one object
Unlike incoming/3
, this does not create a graph relation and instead places the _id
in a field. If the value
passed to the name attribute is :user
then the foreign key created on this schema will be :user_id
and will
store the full _id
of that user. By storing the full _id
, you are still able to perform full AQL queries.
This MUST be accompanied by a one_outgoing/3
definition in the other target schema.
Behind the scenes this injects the __id__
field to store the _id
value and uses the built-in Ecto belongs_to/3
function.
Options passed to the opts
attribute are passed to the belongs_to/3
definition. Refrain from overriding the
:references
and :foreign_key
attributes unless you know what you are doing.
example
Example
defmodule MyProject.Post do
use ArangoXEcto.Schema
schema "posts" do
field :title, :string
one_incoming :user, MyProject.User
end
end
Defines an outgoing relationship of one object
Unlike outgoing/3
, this does not create a graph relation and instead places the _id
in a field in the incoming
schema. This MUST be accompanied by a one_incoming/3
definition in the other target schema.
Behind the scenes this injects the __id__
field to store the _id
value and uses the built-in Ecto has_one/3
function.
Options passed to the opts
attribute are passed to the has_many/3
definition. Refrain from overriding the
:references
and :foreign_key
attributes unless you know what you are doing.
example
Example
defmodule MyProject.User do
use ArangoXEcto.Schema
schema "users" do
field :name, :string
one_outgoing :best_post, MyProject.Post
end
end
Defines collection options for dynamic creation
When using dynamic mode for collection creation you may want to add options for when the collection is created. To add options that will be applied you pass them to as a keyword list.
The available options can be found in the ArangoXEcto.Migration.Collection
module. These options also work in edge collections.
example
Example
To specify a UUID as the collection key type you just supply it as the key option type.
options [
keyOptions: %{type: :uuid}
]
Defines an outgoing relationship of many objects
Behind the scenes this defines a many to many relationship so that Ecto can load the relationship using the built in functions.
The use of this function MUST be accompanied by a incoming/3
definition in the other target node.
This will also define the __id__
field if it is not already defined so that ecto can map the relationship.
example
Example
defmodule MyProject.User do
use ArangoXEcto.Schema
schema "users" do
field :name, :string
# Will use the automatically generated edge
outgoing :posts, MyProject.Post
# Will use the UserPosts edge
outgoing :posts, MyProject.Post, edge: MyProject.UserPosts
end
end
A wrapper around the Ecto.Schema.schema/2
to add the _id field