# `Trogon.Commanded.Entity`
[🔗](https://github.com/straw-hat-team/beam-monorepo/blob/trogon_commanded@v1.0.1/apps/trogon_commanded/lib/trogon/commanded/entity.ex#L1)

Defines "Entity" modules.

# `identifier_opt`

```elixir
@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 `Trogon.Commanded.ValueObject` module or [`Ecto` built-in types](https://hexdocs.pm/ecto/Ecto.Schema.html#module-types-and-casting)

# `identity`

```elixir
@type identity() :: any()
```

The identity of an entity.

# `t`

```elixir
@type t() :: struct()
```

A struct that represents an entity.

# `using_opts`

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

# `__using__`
*macro* 

```elixir
@spec __using__(opts :: using_opts()) :: any()
```

Converts the module into an `t: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 {: .info}
> `identifier` is automatically registered as a field in the `embedded_schema`.
> Do not define the field in the `embedded_schema` yourself again.

## Using

- `Trogon.Commanded.ValueObject`

## Usage

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

      embedded_schema do
        # ...
      end
    end

You can also define a custom type as the identifier:

    defmodule IdentityRoleId do
      use Trogon.Commanded.ValueObject

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

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

      embedded_schema do
        # ...
      end
    end

---

*Consult [api-reference.md](api-reference.md) for complete listing*
