Seraph.Schema.Node (Seraph v0.2.4)
Defines a Node schema.
a Node Schema is used to map a Neo4j node into an Elixir struct.
node/2
is used to map a Neo4j node into an Elixir struct and vice versa.
It allows you to have your application data decoipled from your persisted data and
to manipulate them easily.
Example
defmodule User do
use Seraph.Schema.Node
node "User" do
property :name, :string
property :age, :integer, default: 0
incoming_relationship "FOLLOWED", MyApp.Blog.User, :followers, through: MyApp.Blog.Relationships.Followed
outgoing_relationship "WROTE", MyApp.Blog.Post, :posts, through: MyApp.Blog.Relationships.Wrote
end
end
By default, a node schema will generate a identifier which is named uuid
and of type Ecto.UUID
.
This is to avoid to rely on Neo4j's internal ids for identifier purpose.
The property
macro defines a property in the node schema.
The incoming_relationship
macro defines relationship going from another node schema to the current one.
The outgoing_relationship
macro defines relationship going from the current node schema to another one.
Schemas are regular structs and can be created and manipulated directly
using Elixir's struct API:
iex> user = %User{name: "jane"}
iex> %{user | age: 30}
However, most commonly, structs are cast, validated and manipulated with the
Seraph.Changeset
module.
Schema attributes
Supported attributes for configuring the defined node schema. They must
be set after the use Seraph.Schema.Node
call and before the node/2
definition.
These attributes are:
@identifier
configures the node schema identifier. It will be used at node's creation only. It expects a tuple {name, type, options}. No options are available for the moment.@merge_keys
configure the node schema merge keys. These keys will be used when updating the node data. It expects a list of atoms. Note that the merge keys must be properties of the node schema. If they are not defined, theidentifier
will be used as merge key.
Types
The available types are:
Ecto type | Elixir type | Literal syntax in query |
---|---|---|
:id | integer | 1, 2, 3 |
:binary_id | binary | <<int, int, int, ...>> |
:integer | integer | 1, 2, 3 |
:float | float | 1.0, 2.0, 3.0 |
:boolean | boolean | true, false |
:string | UTF-8 encoded string | "hello" |
:binary | binary | <<int, int, int, ...>> |
{:array, inner_type} | list | [value, value, value, ...] |
:map | map | |
{:map, inner_type} | map | |
:decimal | Decimal | |
:date | Date | |
:time | Time | |
:time_usec | Time | |
:naive_datetime | NaiveDateTime | |
:naive_datetime_usec | NaiveDateTime | |
:utc_datetime | DateTime | |
:utc_datetime_usec | DateTime |
Reflection
Any node schema module will generate the __schema__
function that can be
used for runtime introspection of the schema:
__schema__(:primary_label)
- Returns the primary label as defined innode/2
__schema__(:identifier)
- Returns the identifier data__schema__(:merge_keys)
- Returns the list of merge keys__schema__(:properties)
- Returns the list of properties names__schema__(:relationships)
- Returns the list of all relationships data__schema__(:relationship, relationship_type)
- Returns data about the specified relationship type__schema__(:incoming_relationships)
- Returns a list of all incoming relationship names__schema__(:outgoing_relationships)
- Returns a list of all outgoing relationship names
Link to this section Summary
Functions
Defines an incoming relationship on the node schema with the given data
Defines a node schema with a primary label, properties and relationships definitions.
An additional field called __meta__
is added to the struct.
Defines an outgoing relationship on the node schema with the given data
Defines a property on the node schema with the given name and type.
Link to this section Types
Specs
Link to this section Functions
incoming_relationship(type, related_node, name, relationship_module, opts \\ [])
(macro)Defines an incoming relationship on the node schema with the given data:
type
- the relationship type (must be uppercased)related_node
- the node schema the relationship is linked fromname
- the name used for storing related nodes (when loaded)relationship_module
- Defines the Relationship module
Loaded relationship(s) will be stored in the struct with their type as key.
Options:
cardinality
- Defines the cardinality of the relationship. Can take two values::one
or:many
Defines a node schema with a primary label, properties and relationships definitions.
An additional field called __meta__
is added to the struct.
Note that primary label must be PascalCased.
outgoing_relationship(type, related_node, name, relationship_module, opts \\ [])
(macro)Defines an outgoing relationship on the node schema with the given data:
type
- the relationship type (must be uppercased)related_node
- the node schema the relationship is linked toname
- the name used for storing related nodes (when loaded)relationship_module
- Defines the Relationship module
Loaded relationship(s) will be stored in the struct with their type as key.
Options:
cardinality
- Defines the cardinality of the relationship. Can take two values::one
or:many
Defines a property on the node schema with the given name and type.
Options:
:default
- Sets the default value on the node schema and the struct. The default value is calculated at compilation time, so don't use expressions likeDateTime.utc_now
orEcto.UUID.generate
as they would then be the same for all records.:virtual
- When true, the field is not persisted to the database.