# `Chronicle.EventStoreSubscriptions`
[🔗](https://github.com/Cratis/Chronicle.Elixir/blob/main/lib/chronicle/event_store_subscriptions.ex#L4)

Idiomatic API for working with Chronicle event store subscriptions.

Event store subscriptions let one event store subscribe to events emitted from
another event store's outbox. They can be registered imperatively, or declared
as discoverable modules with `use Chronicle.EventStoreSubscriptions.Subscription`
and auto-registered by `Chronicle.Client`.

## Discoverable subscriptions

    defmodule MyApp.EventStoreSubscriptions.DefaultAccountEvents do
      use Chronicle.EventStoreSubscriptions.Subscription,
        source_event_store: "default"

      alias Chronicle.EventStoreSubscriptions.DefinitionBuilder

      @impl true
      def define(builder) do
        builder
        |> DefinitionBuilder.with_event_type(MyApp.Events.AccountOpened)
        |> DefinitionBuilder.with_event_type(MyApp.Events.FundsDeposited)
      end
    end

## Imperative registration

    :ok =
      Chronicle.EventStoreSubscriptions.subscribe(
        "account-events-from-default",
        "default",
        fn builder ->
          builder
          |> Chronicle.EventStoreSubscriptions.DefinitionBuilder.with_event_type(
            MyApp.Events.AccountOpened
          )
        end
      )

## Options

  * `:client` — the client name (default: `Chronicle.Client`)
  * `:namespace` — accepted for consistency with other APIs and ignored here

# `discover`

```elixir
@spec discover(keyword()) :: [Chronicle.EventStoreSubscriptions.Definition.t()]
```

Discovers event store subscription definitions from the configured client or
loaded modules.

# `get_all`

```elixir
@spec get_all(keyword()) ::
  {:ok, [Chronicle.EventStoreSubscriptions.Definition.t()]} | {:error, term()}
```

Returns all event store subscriptions currently registered with the target
event store.

Mirrors the C# and Kotlin clients' `IEventStoreSubscriptions.GetAll()`.

## Options

  * `:client` — the client name (default: `Chronicle.Client`)

# `register`

```elixir
@spec register(module(), keyword()) :: :ok | {:error, term()}
```

Registers a single discoverable event store subscription module.

# `register_discovered`

```elixir
@spec register_discovered(keyword()) :: :ok | {:error, term()}
```

Registers all discoverable event store subscriptions.

# `subscribe`

```elixir
@spec subscribe(String.t(), String.t(), keyword()) :: :ok | {:error, term()}
```

Registers an event store subscription imperatively using all available event
types.

# `subscribe`

```elixir
@spec subscribe(
  String.t(),
  String.t(),
  (Chronicle.EventStoreSubscriptions.DefinitionBuilder.t() -&gt;
     Chronicle.EventStoreSubscriptions.DefinitionBuilder.t()),
  keyword()
) :: :ok | {:error, term()}
```

Registers an event store subscription imperatively.

# `unsubscribe`

```elixir
@spec unsubscribe(String.t(), keyword()) :: :ok | {:error, term()}
```

Removes an event store subscription by identifier.

---

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