Cqrs.BoundedContext (cqrs_tools v0.5.28) 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, errors}} = Users.create_user(name: "chris", email: "wrong")
...> errors
%{email: ["has invalid format"]}

iex> {:error, {:invalid_command, errors}} =  Users.create_user2(name: "chris", email: "wrong")
...> 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!(email: "wrong")
** (Cqrs.QueryError) email has invalid format

iex> {:error, errors} = Users.get_user2(%{bad: "data"})
...> errors
%{email: ["can't be blank"]}

iex> {:error, errors} = Users.get_user(email: "wrong")
...> errors
%{email: ["has invalid 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"}

Link to this section Summary

Functions

Creates proxy functions to dispatch this command module.

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

  • :then - A function of one arity to run with the execution result.
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