Arc.ReadModel (cratis_arc v0.1.0)

Copy Markdown View Source

A read model container: the module that declares queries over one model type.

The model is any struct, typically a Chronicle read model. Arc infers the TypeScript shape from its fields, so the model stays a plain Chronicle read model.

defmodule MyApp.Authors.Listing.Authors do
  @moduledoc "Queries over authors."
  use Arc.ReadModel, model: MyApp.Authors.Author

  @query many: true
  def all_authors(_args, _ctx), do: Chronicle.all(MyApp.Authors.Author)

  @query params: [id: :uuid], many: false
  def by_id(%{id: id}, _ctx), do: Chronicle.read_model(MyApp.Authors.Author, id)

  @observe many: true, watch: [MyApp.Authors.Author]
  def live_authors(_args, _ctx), do: Chronicle.all(MyApp.Authors.Author)
end

Each annotated function takes (args, ctx) where args is a map of the declared, converted parameters, and returns {:ok, value} or {:error, results}.

Options of use Arc.ReadModel

  • :model - (required) the struct the queries return
  • :types - overrides for inferred field types in the generated proxy, for example types: [id: :uuid]. Arc infers a type from each struct default; a nil or "" default is a string.

@query / @observe options

  • :params - keyword list of name: type, or name: {type, optional: true}
  • :many - the query returns a list (default true); Arc applies page, pageSize, sortBy and sortDirection to lists unless the function returns an Arc.Page
  • :authorize / :allow_anonymous - override the module-level declaration
  • :http_method - :get or :query to prefer one in the generated client

@observe additionally takes:

  • :watch - Chronicle read model modules; the query re-runs when any of them changes
  • :topics - Arc.Notifier topics that re-run the query when notified

Summary

Functions

Allows anonymous callers for every query of the module.

Declares who may run every query of the module. See Arc.Authorization.

Functions

allow_anonymous()

(macro)

Allows anonymous callers for every query of the module.

authorize(requirement)

(macro)

Declares who may run every query of the module. See Arc.Authorization.