AshCommanded.Commanded.Transformers.GenerateProjectorModules (AshCommanded v0.1.0)

View Source

Generates projector modules based on the projections defined in the DSL.

For each resource with projections, this transformer will generate a projector module that subscribes to the events and applies the corresponding projections to update the resource state.

This transformer should run after the projection module transformer.

Example

Given a resource with several projections, this transformer will generate:

defmodule MyApp.Projectors.UserProjector do
  @moduledoc "Event handler that projects events onto the User resource"
  
  use Commanded.Event.Handler,
    application: MyApp.CommandedApplication,
    name: "MyApp.Projectors.UserProjector"
    
  alias MyApp.Projections
  
  def handle(%MyApp.Events.UserRegistered{} = event, _metadata) do
    with {:ok, resource} <- Projections.UserRegistered.apply(event),
         {:ok, _result} <- perform_action(resource, Projections.UserRegistered.action()) do
      :ok
    else
      {:error, reason} -> {:error, reason}
    end
  end
  
  # More handlers for other events...
  
  defp perform_action(changeset, :create), do: Ash.create(changeset)
  defp perform_action(changeset, :update), do: Ash.update(changeset)
  defp perform_action(changeset, :destroy), do: Ash.destroy(changeset)
end

Summary

Functions

Specifies that this transformer should run after the projection module transformer.

Callback implementation for Spark.Dsl.Transformer.before?/1.

Builds a map of event names to their corresponding projections.

Builds the module name for a projector.

Transforms the DSL state to generate projector modules.

Functions

after?(arg1)

Specifies that this transformer should run after the projection module transformer.

after_compile?()

Callback implementation for Spark.Dsl.Transformer.after_compile?/0.

before?(_)

Callback implementation for Spark.Dsl.Transformer.before?/1.

build_event_projection_map(projections, events)

Builds a map of event names to their corresponding projections.

Examples

iex> build_event_projection_map(projections, events)
%{user_registered: [projection1, projection2], email_changed: [projection3]}

build_projector_module(resource_name, app_prefix)

Builds the module name for a projector.

Examples

iex> build_projector_module("User", MyApp)
MyApp.Projectors.UserProjector

build_projector_module_ast(resource_module, resource_name, projections, event_projections, event_modules, projection_modules)

Builds the AST (Abstract Syntax Tree) for a projector module.

Examples

iex> build_projector_module_ast(MyApp.User, "User", projections, event_map, event_modules, projection_modules)
{:__block__, [], [{:@, [...], [{:moduledoc, [...], [...]}]}, ...]}

transform(dsl_state)

Transforms the DSL state to generate projector modules.

Examples

iex> transform(dsl_state)
{:ok, updated_dsl_state}