Cqrs.BoundedContext (cqrs_tools v0.1.6) View Source

Macros to create proxy functions to commands and queries in a module.

Examples

defmodule Users do
  use Cqrs.BoundedContext

  command CreateUser
  command CreateUser, as: :create_user2

  query GetUser
  query GetUser, as: :get_user2
end

Commands

iex> {:error, {:invalid_command, state}} =
...> Users.create_user(name: "chris", email: "wrong")
...> state.errors
%{email: ["has invalid format"]}

iex> {:error, {:invalid_command, state}} =
...> Users.create_user2(name: "chris", email: "wrong")
...> state.errors
%{email: ["has invalid format"]}

iex> Users.create_user(name: "chris", email: "chris@example.com")
{:ok, :dispatched}

iex> Users.create_user2(name: "chris", email: "chris@example.com")
{:ok, :dispatched}

Queries

iex> Users.get_user!()
** (Cqrs.Query.QueryError) %{email: ["can't be blank"]}

iex> Users.get_user2!()
** (Cqrs.Query.QueryError) %{email: ["can't be blank"]}

iex> Users.get_user!(email: "wrong")
** (Cqrs.Query.QueryError) %{email: ["has invalid format"]}

iex> {:error, %{errors: errors}} = Users.get_user()
...> errors
[email: {"can't be blank", [validation: :required]}]

iex> {:error, %{errors: errors}} = Users.get_user(email: "wrong")
...> errors
[email: {"has invalid format", [validation: :format]}]

iex> {:ok, query} = Users.get_user_query(email: "chris@example.com")
...> query
#Ecto.Query<from u0 in User, where: u0.email == ^"chris@example.com">

iex> {:ok, user} = Users.get_user(email: "chris@example.com")
...> %{id: user.id, email: user.email}
%{id: "052c1984-74c9-522f-858f-f04f1d4cc786", email: "chris@example.com"}

Usage with Commanded

If you are a Commanded user, you have already registered your commands with your commanded routers. Instead of repeating yourself, you can cut down on boilerplate with the import_commands/1 macro.

Since Commanded is an optional dependency, you need to explicitly import Cqrs.BoundedContext to bring the macro into scope.

defmodule UsersEnhanced do
  use Cqrs.BoundedContext
  import Cqrs.BoundedContext

  import_commands CommandedRouter

  query GetUser
  query GetUser, as: :get_user2
end

Link to this section Summary

Functions

Creates proxy functions to dispatch this command module.

Imports all of a Command Router's registered commands.

Creates proxy functions to create and execute the give query.

Link to this section Functions

Link to this macro

command(command_module, opts \\ [])

View Source (macro)

Creates proxy functions to dispatch this command module.

Functions created

When given CreateUser

  • create_user!/0
  • create_user!/1
  • create_user!/2
  • create_user/0
  • create_user/1
  • create_user/2

Options

  • :after - A function of one arity to run with the execution result.
Link to this macro

import_commands(router, opts \\ [])

View Source (macro)

Imports all of a Command Router's registered commands.

Options

  • :only - Restrict importing to only the commands listed
  • :except - Imports commands except those listed
  • :after - a list of function names and a function of one arity to run with the execution result

Example

import_commands Example.Users.Router,
  except: [CreateUser],
  after: [
    reinstate_user: &AfterExecution.load_user/1,
    suspend_user: &AfterExecution.load_user/1
  ]
Link to this macro

query(query_module, opts \\ [])

View Source (macro)

Creates proxy functions to create and execute the give query.

Functions created

When given ListUsers

  • list_users!/0
  • list_users!/1
  • list_users!/2
  • list_users/0
  • list_users/1
  • list_users/2
  • list_users_query!/0
  • list_users_query!/1
  • list_users_query!/2
  • list_users_query/0
  • list_users_query/1
  • list_users_query/2