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.
A struct that represents an entity.
Types
@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
@type identity() :: any()
The identity of an entity.
@type t() :: struct()
A struct that represents an entity.
@type using_opts() :: [{:identifier, identifier_opt()}]
Functions
@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