gim v1.2.1 Gim.Schema View Source

Defines a schema.

Example

defmodule User do
  use Gim.Schema
  schema do
    property :name, index: :unique
    property :age, default: 0, index: true
    has_edges :author_of, Post, reflect: :authored_by
  end
end

Reflection

Any schema module will generate the __schema__ function that can be used for runtime introspection of the schema:

  • __schema__(:primary_key) - Returns the primary unique indexed property name or nil;

  • __schema__(:properties) - Returns a list of all property names;

  • __schema__(:indexes) - Returns a list of all indexed property names;

  • __schema__(:index, property) - Returns how the given property is indexed;

  • __schema__(:indexes_unique) - Returns a list of all unique indexed property names;

  • __schema__(:indexes_non_unique) - Returns a list of all non-unique indexed property names;

  • __schema__(:associations) - Returns a list of all association names;

  • __schema__(:association, assoc) - Returns the association reflection of the given assoc;

  • __schema__(:type, assoc) - Returns the type of the given association;

Furthermore, __struct__ functions are defined so structs functionalities are available.

Link to this section Summary

Functions

Defines a named (i.e. implicitly labeled) edge to a given type on the schema.

Defines a named (i.e. implicitly labeled) edge to a given type on the schema.

Defines a property with given name on the node type schema.

Defines a schema struct with a source name and property definitions.

Link to this section Functions

Link to this macro

has_edge(name, type, opts \\ [])

View Source (macro)

Defines a named (i.e. implicitly labeled) edge to a given type on the schema.

You can have zero or one edge with this name.

Example

schema do
  has_edge :authored_by, Person, reflect: :author_of
end

Options

  • :reflect - Sets the edge name on the target type to automatically add a reflected edge on the target node.
Link to this macro

has_edges(name, type, opts \\ [])

View Source (macro)

Defines a named (i.e. implicitly labeled) edge to a given type on the schema.

You can store multiple edges with this name.

Example

schema do
  has_edges :categories, Category, reflect: :publications
end

Options

  • :reflect - Sets the edge name on the target type to automatically add a reflected edge on the target node.
Link to this macro

property(name, opts \\ [])

View Source (macro)

Defines a property with given name on the node type schema.

The property is not typed, you can store any valid term.

Example

schema do
  property :uuid, index: :primary
  property :fullname, index: :unique
  property :birthday, index: true
  property :hobbies
end

Options

  • :default - Sets the default value on the schema and the struct. The default value is calculated at compilation time, so don't use expressions like DateTime.utc_now or Ecto.UUID.generate as they would then be the same for all nodes.

  • :index - When true, the property is indexed for lookups. When :unique, the property uniquely indexed, which is enforced. When :primary, the property is uniquely indexed and used as primary key.

Defines a schema struct with a source name and property definitions.