AshCommanded.Commanded.Dsl (AshCommanded v0.1.0)

View Source

The DSL extension for using Commanded with Ash resources.

This extension provides the ability to define commands, events, projections, and event handlers using Ash resources, and integrates with Commanded to provide CQRS and Event Sourcing patterns.

Usage

defmodule MyApp.User do
  use Ash.Resource,
    extensions: [AshCommanded.Commanded.Dsl]
    
  commanded do
    commands do
      command :register_user do
        fields([:id, :email, :name])
        identity_field(:id)
      end
    end
    
    events do
      event :user_registered do
        fields([:id, :email, :name])
      end
    end
    
    projections do
      projection :user_registered do
        changes(%{status: :active})
      end
    end
    
    event_handlers do
      handler :user_registered_notification do
        events [:user_registered]
        action fn event, _metadata ->
          MyApp.Notifications.send_welcome_email(event.email)
          :ok
        end
      end
    end
  end
end

Summary

Functions

Return the section definitions for use in custom DSL extensions.

Get the application configuration from the DSL.

Determine if a resource uses the commanded extension.

Functions

__sections__()

@spec __sections__() :: Spark.Dsl.Section.t()

Return the section definitions for use in custom DSL extensions.

This is used in tests to create custom DSL extensions without verifiers.

application(dsl)

@spec application(Spark.Dsl.t()) :: keyword() | nil

Get the application configuration from the DSL.

Parameters

  • dsl - The DSL state.

Returns

A keyword list of application configuration options, or nil if no application configuration is defined.

Examples

iex> application(dsl)
[otp_app: :my_app, event_store: Commanded.EventStore.Adapters.InMemory]

iex> application(dsl_without_app_config)
nil

commanded(body)

(macro)

extension?(resource)

@spec extension?(Ash.Resource.t()) :: boolean()

Determine if a resource uses the commanded extension.

Examples

iex> AshCommanded.Commanded.Dsl.extension?(SomeResource)
true

iex> AshCommanded.Commanded.Dsl.extension?(OtherResource)
false