View Source OnePiece.Commanded.Entity (OnePiece.Commanded v0.25.1)

Defines "Entity" modules.

Summary

Types

The identity key of the entity.

The identity of an entity.

t()

A struct that represents an entity.

Functions

Converts the module into an t/0.

Types

identifier_opt()

@type identifier_opt() ::
  atom()
  | {key_name :: atom(), type :: atom()}
  | {key_name :: atom(), type :: module()}

The identity key of the entity.

If it's a tuple, the type must be a module that implements the OnePiece.Commanded.ValueObject module or Ecto built-in types

identity()

@type identity() :: any()

The identity of an entity.

t()

@type t() :: struct()

A struct that represents an entity.

using_opts()

@type using_opts() :: [{:identifier, identifier_opt()}]

Functions

__using__(opts \\ [])

(macro)
@spec __using__(opts :: using_opts()) :: any()

Converts the module into an t/0.

Identifier

The identifier is used to identify the entity. It uses the @primary_key attribute to define the column and type.

Schema Field Registration

identifier is automatically registered as a field in the embedded_schema. Do not define the field in the embedded_schema yourself again.

Using

Usage

defmodule MyEntity do
  use OnePiece.Commanded.Entity, identifier: :id

  embedded_schema do
    # ...
  end
end

You can also define a custom type as the identifier:

defmodule IdentityRoleId do
  use OnePiece.Commanded.ValueObject

  embedded_schema do
    field :identity_id, :string
    field :role_id, :string
  end
end

defmodule IdentityRole do
  use OnePiece.Commanded.Entity,
    identifier: {:id, IdentityRoleId}

  embedded_schema do
    # ...
  end
end